8

What's the fastest way using either DOS scripting or PowerShell to run this simple command on a directory and all its subdirectories:

 convert filename.jpg -resize 620x620 "R:\processed\filename.jpg"

DOS Batch script for single directory:

 FOR %%a in (*.jpg) DO convert %%a -resize 620x620 "R:\processed\%%a"

I want to run this recursively on a directory structure and have the output match the input hierarchy. I figured PowerShell was the easiest way, but I was unable to learn PowerShell in the 5 minutes I have to do this task!

Note: not that it's relevant, but convert is from ImageMagick.

1 Answer 1

32

In PowerShell:

Use the -recurse switch and pipe to foreach. For e.g.:

dir -recurse -include *.jpg | %{convert  $_.FullName -resize 620x620 "R:\processed\$_"}

(Note that the % sign is an alias of foreach-object).

Sign up to request clarification or add additional context in comments.

4 Comments

"R:\processed\ $_.Name" should be "R:\processed\$($_.Name)" to get the file name in the string.
@Emperor. Oops you're right. Actually you can use "R:\processed\$_" to get the same results.
currently this ends up trying to save in "R:\processed\R:\unprocessed\1.jpg because it doesnt take the relative path from the current directory. can this easily be found? using $_ is too much but $_.Name does not contain the path
Interesting, it works for me. $_.FullName returns the full path. $_.Name returns just the filename. $_ by itself should be the same as $_.Name since the string conversion of a FileInfo object is $_.Name. Maybe you can try @Emperor suggestion: "R:\processed\$($_.Name)"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.