Hi,
I am trying to write my first powershell script but am completely stumped and trawling google hasn't helped.
I am writing (trying to write I should say) a script to look through a folder tree and identify photographs where the archive bit has been set and on those files run ImageMagick to convert the file to a different size.
My script is as follows and I've included how I am running the code
# Invokation : # powershell -ExecutionPolicy RemoteSigned -File .\RI2.ps1 # ---------------------------------------- # Configuration $srcfolder = "D:\TempFiles\PictureProcessing\PicsforDisplay" $destpath = "D:\TempFiles\PictureProcessing\PicsforDisplay" $im_convert_exe = "c:\program files (x86)\ImageMagick-6.8.6-Q16\convert.exe" $src_filter = "*.jpg", "*.tif", "*.png" $options = "-resize 1920x1080^>" # ---------------------------------------- foreach ($srcitem in $(Get-ChildItem ${srcfolder} -include ${src_filter} -recurse | Where {$_.attributes -match "Archive"})) { $srcname = ${srcitem}.fullname # Perform the conversion by calling an external tool $cmdline = " `"" +${im_convert_exe} + "`"" + " `"" + ${srcname} + "`"" + " "+ ${options} + " `"" + ${srcname} + "`" " echo ${cmdline} invoke-expression -command ${cmdline} }
When I run the script I get the error :
Invoke-Expression : Unexpected token 'D:\TempFiles\PictureProcessing\PicsforDis play\Alex\2013\P1000985.JPG' in expression or statement. At D:\Temp\test\2\RI2.ps1:23 char:19+ invoke-expression <<<< -command ${cmdline}+ CategoryInfo : ParserError: (D:\TempFiles\Pi...13\P1000985.JPG: String) [Invoke-Expression], ParseException+ FullyQualifiedErrorId : UnexpectedToken,Microsoft.PowerShell.Commands.In vokeExpressionCommand
I get the error once for each file (so in this instance P1000985.jpg in the given folder).
The echo ${cmdline} entry outputs the actual text which I think is the text I am asking to be executed :
"c:\program files (x86)\ImageMagick-6.8.6-Q16\convert.exe" "D:\TempFiles\Pictu reProcessing\PicsforDisplay\Alex\P1000885.JPG" -resize 1920x1080^> "D:\TempFile s\PictureProcessing\PicsforDisplay\Alex\P1000885.JPG"
and if I copy that (and remove the newlines) it runs perfectly.
I'm not sure if that is enough for anyone to help me - am I doing something incredibly dumb? Clearly I'm trying to run a dos command