Home > OS >  Powershell send directory contents to a program
Powershell send directory contents to a program

Time:04-04

My goal is to translate .wav files in a directory using an executable which converts them to the proper audio format. Once these files are created, I then need to move everything out of the directory to another directory destination. What should happen is once the file executes the program (CommandLineConverter.exe) creates a new file in the correct audio format.

I tried Get-ChildItem pointing to the executable. Early on I ran into problems with the script accepting (x86). However, that seems resolved now and I don't any errors on debugging. The Move-Item is commented out for now because I'm trying to get the files to convert. Currently the script is not converting the files. There is no output that I can see.

I considered as an alternative just opening every item in the directory ($path) as I set the default for wave files to open in the CommandLineConverter.exe; however, that script didn't go well either.

Anyway here is the script that I have so far for sending directory contents to the executable which does not appear to be working as intended. Any help to point me in the right direction would be appreciated.

$path= "C:\Execuscribe\ExecuDropBox\Dropbox (ExecuScribe)\Conduent\Verizon"
$destination= "C:\Execuscribe\ExecuDropBox\Dropbox (ExecuScribe)\Converted"
Get-ChildItem $path | ForEach-Object 'C:\Program Files (x86)\Verint\Playback\CommandLineConvertor.exe'
#   {
        
 #       Move-Item -Path $file.fullname -Destination $Destination
#    }

CodePudding user response:

RetiredGeek's helpful answer contains the crucial pointer:

  • You must place the call to your executable inside a script block ({ ... }) passed to ForEach-Object ...

  • ... and, because the executable's path is (of necessity) quoted, you must invoke via &, the call operator.

  • Additionally, you must pass each input file's full path as an explicit argument to your executable, via the automatic $_ variable, which reflects the pipeline input object at hand.

    • In Windows PowerShell, you must use $_.FullName to ensure that the input file's full path is passed; fortunately, this is no longer necessary in PowerShell (Core) 7 , where just $_ is enough.

However, with respect to the renaming part, I suggest retrieving the conversion's output file via Get-Item, which you can then pipe to a single Move-Item call placed in a new, final pipeline segment:

Get-ChildItem -LiteralPath $path | ForEach-Object { 
  & 'C:\Program Files (x86)\Verint\Playback\CommandLineConvertor.exe' $_.FullName  
  Get-Item -LiteralPath (Join-Path $_.DirectoryName ($_.BaseName   '_Conv.wav'))
} | Move-Item -Destination $destination

Note:

  • -LiteralPath is used for robustness, to ensure that all paths are treated literally rather than as wildcard expressions, as would happen if you used -Path instead (which may be positionally implied, such as in your Get-ChildItem $path call).

  • $_.DirectoryName is the input file's directory path. $_.BaseName '_Conv.wav' takes the input file name's base name (the file name without its extension) and appends _Conv.wav to identify the conversion output file name. The two pieces of information are then joined with Join-Path to form the output file's full path.

  • Note that passing the conversion output file path to Get-Item -LiteralPath isn't strictly necessary, but it again ensures binding to (Move-Item's) -LiteralPath parameter. While Move-Item also accepts path strings as pipeline input, they bind to the -Path parameter, and are therefore again subject to interpretation as wildcard expressions, which notably would cause failures with literal paths containing [ characters (e.g., file[1].wav).

CodePudding user response:

Kirk,

I think you want something along this line:

$path= "C:\Execuscribe\ExecuDropBox\Dropbox (ExecuScribe)\Conduent\Verizon"
$destination= "C:\Execuscribe\ExecuDropBox\Dropbox (ExecuScribe)\Converted"
Get-ChildItem $path  -Filter "*.wav" | 
ForEach-Object {
   &  'C:\Program Files (x86)\Verint\Playback\CommandLineConvertor.exe' $($_.FullName)

  $MIArgs = @{Path = $($_.fullname) 
              Destination = "$Destination"}
   Move-Item @MIArgs
}

Add the -Filter parameter to Get-ChildItem to only retrieve .wav files. Call the program specifying each file as it comes down the pipe. I don't know how the converter's arguments are specified but the code included just contains the fully qualified filename you can add any identifiers that are required. Copy the converted file to the new location you'll have to replace NewExt with the output extension of the converted file.

This is untested code!

  • Related