SOLVED

Passing parameters to an .exe program in a powershell loop

%3CLINGO-SUB%20id%3D%22%5C%26quot%3Blingo-sub-3139972%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3EPassing%20parameters%20to%20an%20.exe%20program%20in%20a%20powershell%20loop%26lt%3B%5C%2Flingo-sub%26gt%3B%3CLINGO-BODY%20id%3D%22%5C%26quot%3Blingo-body-3139972%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CP%3EI%20am%20a%20complete%20newcomer%20to%20powershell.%26nbsp%3B%20I%E2%80%99m%20not%20sure%20if%20this%20is%20the%20proper%20forum%20for%20powershell%20novices%20who%20are%20not%20IT%20professionals%20to%20get%20help%2C%20but%20I%E2%80%99m%20giving%20it%20a%20try.%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EI%E2%80%99m%20trying%20to%20build%20a%20script%20that%20will%20cycle%20through%20all%20my%20flac%20format%20music%20files%20and%20check%20their%20integrity%20using%20the%20executable%20program%20flac.exe.%26nbsp%3B%26nbsp%3B%20I%E2%80%99ve%20a%20good%20idea%20how%20to%20do%20this%2C%20but%20I%E2%80%99m%20having%20problems%20calling%20flac.exe%20from%20within%20a%20powershell%20loop.%26nbsp%3B%20I%20can%20execute%20flac.exe%20fine%20if%20not%20within%20a%20loop.%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3ESpecifically%2C%20this%20set%20of%20commands%20works%20fine%3A%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E--------code%20that%20works----------%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%24xdir%20%3D%20%E2%80%9Ce%3A%5C%5Cchecking_music_collection_for_errors%5C%5C10_000_Maniacs%5C%5C(1993)_Mtv_Unplugged%5C%5C%E2%80%9D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3ESet-Location%20%24xdir%20%E2%80%9C%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eflac%20%E2%80%93t%20good.flac%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E----------------------------%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EBut%20if%20I%20loop%20through%20all%20the%20flac%20files%20in%20%24xdir%20and%20pass%20them%20to%20flac.exe%2C%20as%20in%20the%20code%20directly%20below%2C%20the%20%E2%80%9C-t%E2%80%9D%20option%20in%20the%20flac%20command%20(which%20instructs%20the%20program%20to%20test%20the%20file%20and%20not%20re-encode%20it)%20is%20not%20understood%20and%20is%20ignored.%26nbsp%3B%20Because%20the%20option%20is%20ignored%2C%20the%20flac%20program%20thinks%20I%20want%20to%20re-encode%20the%20file%20but%20doesn%E2%80%99t%20because%20the%20flac%20file%20already%20exists.%26nbsp%3B%20The%20filename%20is%20properly%20passed%20to%20the%20flac%20program.%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E--------code%20that%20does%20not%20work----------------%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%24xdir%20%3D%20%E2%80%9Ce%3A%5C%5Cchecking_music_collection_for_errors%5C%5C10_000_Maniacs%5C%5C(1993)_Mtv_Unplugged%5C%5C%E2%80%9D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%24files%20%3D%20Get-ChildItem%20%24xdir%20*.flac%20-recurse%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eforeach%20(%24f%20in%20%24files)%7B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eecho%20%26nbsp%3B%3D%20%24f.FullName%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3Eflac%20%E2%80%93t%26nbsp%3B%20%24f.FullName%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%7D%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E--------------------%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20greatly%20appreciated.%26nbsp%3B%20Thanks.%26nbsp%3B%26nbsp%3B%26lt%3B%5C%2FP%26gt%3B%26lt%3B%5C%2Flingo-body%26gt%3B%3CLINGO-LABS%20id%3D%22%5C%26quot%3Blingo-labs-3139972%5C%26quot%3B%22%20slang%3D%22%5C%26quot%3Ben-US%5C%26quot%3B%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%26lt%3B%5C%2Flingo-label%26gt%3B%26lt%3B%5C%2Flingo-labs%26gt%3B%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3C%2FLINGO-SUB%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3139972%22%20slang%3D%22en-US%22%3EPassing%20parameters%20to%20an%20.exe%20program%20in%20a%20powershell%20loop%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3139972%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20a%20complete%20newcomer%20to%20powershell.%26nbsp%3B%20I%E2%80%99m%20not%20sure%20if%20this%20is%20the%20proper%20forum%20for%20powershell%20novices%20who%20are%20not%20IT%20professionals%20to%20get%20help%2C%20but%20I%E2%80%99m%20giving%20it%20a%20try.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%E2%80%99m%20trying%20to%20build%20a%20script%20that%20will%20cycle%20through%20all%20my%20flac%20format%20music%20files%20and%20check%20their%20integrity%20using%20the%20executable%20program%20flac.exe.%26nbsp%3B%26nbsp%3B%20I%E2%80%99ve%20a%20good%20idea%20how%20to%20do%20this%2C%20but%20I%E2%80%99m%20having%20problems%20calling%20flac.exe%20from%20within%20a%20powershell%20loop.%26nbsp%3B%20I%20can%20execute%20flac.exe%20fine%20if%20not%20within%20a%20loop.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESpecifically%2C%20this%20set%20of%20commands%20works%20fine%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E--------code%20that%20works----------%3C%2FP%3E%3CP%3E%24xdir%20%3D%20%E2%80%9Ce%3A%5Cchecking_music_collection_for_errors%5C10_000_Maniacs%5C(1993)_Mtv_Unplugged%5C%E2%80%9D%3C%2FP%3E%3CP%3ESet-Location%20%24xdir%20%E2%80%9C%3C%2FP%3E%3CP%3Eflac%20%E2%80%93t%20good.flac%3C%2FP%3E%3CP%3E----------------------------%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBut%20if%20I%20loop%20through%20all%20the%20flac%20files%20in%20%24xdir%20and%20pass%20them%20to%20flac.exe%2C%20as%20in%20the%20code%20directly%20below%2C%20the%20%E2%80%9C-t%E2%80%9D%20option%20in%20the%20flac%20command%20(which%20instructs%20the%20program%20to%20test%20the%20file%20and%20not%20re-encode%20it)%20is%20not%20understood%20and%20is%20ignored.%26nbsp%3B%20Because%20the%20option%20is%20ignored%2C%20the%20flac%20program%20thinks%20I%20want%20to%20re-encode%20the%20file%20but%20doesn%E2%80%99t%20because%20the%20flac%20file%20already%20exists.%26nbsp%3B%20The%20filename%20is%20properly%20passed%20to%20the%20flac%20program.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E--------code%20that%20does%20not%20work----------------%3C%2FP%3E%3CP%3E%24xdir%20%3D%20%E2%80%9Ce%3A%5Cchecking_music_collection_for_errors%5C10_000_Maniacs%5C(1993)_Mtv_Unplugged%5C%E2%80%9D%3C%2FP%3E%3CP%3E%24files%20%3D%20Get-ChildItem%20%24xdir%20*.flac%20-recurse%3C%2FP%3E%3CP%3Eforeach%20(%24f%20in%20%24files)%7B%3C%2FP%3E%3CP%3Eecho%20%26nbsp%3B%3D%20%24f.FullName%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3Eflac%20%E2%80%93t%26nbsp%3B%20%24f.FullName%26nbsp%3B%3C%2FP%3E%3CP%3E%7D%3C%2FP%3E%3CP%3E--------------------%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20help%20would%20be%20greatly%20appreciated.%26nbsp%3B%20Thanks.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3139972%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
New Contributor

I am a complete newcomer to powershell.  I’m not sure if this is the proper forum for powershell novices who are not IT professionals to get help, but I’m giving it a try. 

 

I’m trying to build a script that will cycle through all my flac format music files and check their integrity using the executable program flac.exe.   I’ve a good idea how to do this, but I’m having problems calling flac.exe from within a powershell loop.  I can execute flac.exe fine if not within a loop.

 

Specifically, this set of commands works fine:

 

--------code that works----------

$xdir = “e:\checking_music_collection_for_errors\10_000_Maniacs\(1993)_Mtv_Unplugged\”

Set-Location $xdir “

flac –t good.flac

----------------------------

 

But if I loop through all the flac files in $xdir and pass them to flac.exe, as in the code directly below, the “-t” option in the flac command (which instructs the program to test the file and not re-encode it) is not understood and is ignored.  Because the option is ignored, the flac program thinks I want to re-encode the file but doesn’t because the flac file already exists.  The filename is properly passed to the flac program. 

 

--------code that does not work----------------

$xdir = “e:\checking_music_collection_for_errors\10_000_Maniacs\(1993)_Mtv_Unplugged\”

$files = Get-ChildItem $xdir *.flac -recurse

foreach ($f in $files){

echo  = $f.FullName    

flac –t  $f.FullName 

}

--------------------

 

Any help would be greatly appreciated.  Thanks.  

4 Replies
I should have mentioned that I'm using Powershell ISE, and strangely ordinary Powershell 5.1 behaves differently. Specifically, I am unable to get the flac command to work in ordinary Powershell even if not in a loop.
best response confirmed by rm1954 (New Contributor)
Solution

@rm1954 
You could try setting the argument (-t) as a variable, so 

$xdir = “e:\checking_music_collection_for_errors\10_000_Maniacs\(1993)_Mtv_Unplugged\”
$files = Get-ChildItem $xdir *.flac -recurse
$parameters="-t"
foreach ($f in $files){
echo  = $f.FullName    
flac.exe $parameters $f.FullName 
}
Harm_Veenstra's suggestion works. Thanks so much!
You're welcome! Please mark my reply as solution to mark it as solved