Forum Discussion
Passing parameters to an .exe program in a powershell loop
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.
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 }
- rm1954Copper ContributorI 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.
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 }
- rm1954Copper ContributorHarm_Veenstra's suggestion works. Thanks so much!