views:

529

answers:

1

I've got a simple Bash command to resize some images automatically on a low-traffic website using ImageMagick - I'd like to convert this to a PowerShell command so I don't have to install Cygwin on my webserver. Can anyone lend their PSh skills here?

ls | xargs -I {} rconvert "{}" -resize 128x128\> "{}"
+5  A: 

Your best bet is to pipe the output of ls to the foreach-object commandlet like this (% is an alias for foreach-object):

ls | %{rconvert $_ -resize 128x128}

Edit: ls outputs a list of FileInfo or DirectoryInfo objects onto the pipeline. To filter out the DirectoryInfo objects, use the where-object filter (? is alias):

ls | ?{-not $_.PSIsContainer} | %{rconvert $_ -resize 128x128}

If you need to access the full path name in your command, use the FullName property of the FileInfo object ($_ by itself will be resolved to $_.Name which is just the filename):

ls | ?{-not $_.PSIsContainer} | %{rconvert $_.FullName -resize 128x128}
zdan
I think you should change $_ to $_.FullName
BeWarned
You're probably right, that's more robust, but the command is being run from the current working directory, so it should work.
zdan
You have an extra "{" in the second and third command that needs to be removed (before the "%").
JasonMArcher
@JasonMArcher thanks! Duly fixed.
zdan