views:

518

answers:

2

Anyone happen to have a sample script for recursing a given directory in a filesystem with Powershell? Ultimately what I'm wanting to do is create a script that will generate NSIS file lists for me given a directory. Something very similar to what was done here with a BASH script.

+4  A: 

This is a "paraphrase" port of that bash script.

$path = "c:\path\to\program"
$installFiles = "installfiles_list.txt"
$uninstFiles = "uninstfiles_list.txt"
$files = get-childitem -path $path -recurse | where-object { ! $_.psIsContainer } # won't include dirs
$filepath = $files | foreach-object { $_.FullName }
$filepath | set-content $installFiles -encoding ASCII
$filepath[($filepath.length-1)..0] | set-content $uninstFiles -encoding ASCII
halr9000
+3  A: 

As halr9000 demonstrated, you can use the -recurse switch parameter of the Get-ChildItem cmdlet to retrieve all files and directories under a specified path.

It looks like the bash script you linked to in your question saves out the directories as well, so here is a simple function to return both the files and directories in a single result object:

function Get-InstallFiles {
  param( [string]$path )

  $allItems = Get-ChildItem -path $path -recurse
  $directories = $allItems | ? { $_.PSIsContainer } | % { $_.FullName }
  $installFiles = $allItems | ? { -not $_.PSIsContainer } | % { $_.FullName }
  $uninstallFiles = $installFiles[-1..-$installFiles.Length]

  $result = New-Object PSObject
  $result | Add-Member NoteProperty Directories $directories
  $result | Add-Member NoteProperty InstallFiles $installFiles
  $result | Add-Member NoteProperty UninstallFiles $uninstallFiles
  return $result
}

Here is how you could use it to create the same install/uninstall text files from halr9000's example, including uninstall directories:

$files = Get-InstallFiles 'C:\some\directory'
$files.InstallFiles | Set-Content 'installfiles.txt'
$files.UninstallFiles + $files.Directories | Set-Content 'uninstallfiles.txt'
Emperor XLII