Command Line loop to run command on all files in a directory (plus sub directories, if possible

I'm assuming this is fairly easy to do, but I have zero experience with Windows's command line utilities. Basically, I need to iterate over all files in a directory (great if it can do sub-directories, but I can run it on each of the 5 directories if need be), get the name as a variable, and have it run

"C:\Program Files\ImageMagick-6.7.6-Q16\convert.exe" -compress LZW -colorspace Gray -colors 32 file_var file_var

I saw Dynamically name files in a command prompt for loop. Would I be able to use that (swapping the SET... with the above command)? The space on the computer in question is beyond limited so I can't perform a backup prior to running this at this stage (bad, I know).

3

2 Answers

Weird, there was a response that had the recursive part.

Well, per How to Loop Through Files Matching Wildcard in Batch File, I was able to achieve this. Here is how it was performed:

 cd path_to_root for /R %%f in (*.tif) do ( "C:\Program Files\ImageMagick-6.7.6-Q16\convert.exe" -compress LZW -colorspace Gray -colors 32 "%%f" "%%f" )
1

Open PowerShell

$files = Get-ChildItem -Recurse
foreach ($file in $files){ c:\windows\System32\notepad.exe $file.FullName
}

Get-ChildItem retrieves a list of files as objects from the current subdirectory. "-recurse" will include sub-directories. This places it into an array $Files.

The foreach loop cycles through each file and calls notepad with the commandline argument of the full file-name path to each file.

CAUTION: Test the above code in a directory with a few small text files, as it will open up an instance of Notepad for each file.

That should give you an idea of how to go about what you're looking to do.

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like