Home > Back-end >  Map an app to all folders/files in a directory recursively
Map an app to all folders/files in a directory recursively

Time:12-12

I'm hoping to use Powershell to:

  • Recurse through directories, of various depths, with the current PS script's directory being root
  • Apply an app to those directories (which in turn will process the files within it)
  • Which also requires an input of a new subdir to make base

The command I want to apply to each directory takes the format: c:\xrv\fileprocess.exe -folderIn [dir] -foldorOut [newdir]

I'm at a bit of a loss on how to make this happen. I've tried to hash together something from snippets online but I never seem to have success. I've kind of made a part hodgepodge snippet part pseudo code attempt to explain what I'm trying to do below.

I'd also note that the paths are looong!

Thanks all!

Clear-Host
$StartDirectory = '\.'
echo $StartDirectory
$files = Get-ChildItem -LiteralPath $StartDirectory -Recurse -Directory `|
foreach($dir in $dirs){
c:\xrv\fileprocess.exe -folderIn $dir -foldorOut "$dir\pd"
}

CodePudding user response:

I have tried to not change too much from your code. try this: if the $dir\pd dosent exist, does the program create it? if not then you would have to include a mkdir $dir\pd inside the foreach loop.

Clear-Host
$StartDirectory = '\.'
$exe = 'c:\xrv\fileprocess.exe'
Write-Host $StartDirectory
$dirs = Get-ChildItem -LiteralPath $StartDirectory -Recurse -Directory
foreach ($dir in $dirs)
{
   &$exe -folderIn $dir -foldorOut "$dir\pd"
}
  • Related