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

14

3

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).

Robert

Posted 2012-05-14T03:03:13.653

Reputation: 371

Robert, if you have managed to answer your own question, please add your own answer and accept it as the correct one. – Julian Knight – 2013-03-02T21:05:12.737

I don’t think that is quite what you want, the end of it ("%%f" "%%f") will just place the filename (with fully qualified path) twice. You probably want to use something like "%%f" "%%~dpnf.gif" to change the extension of the output file. – Synetech – 2013-03-02T21:55:43.473

@JulianKnight - thanks, I actually attempted to way back when, but I think there was a forced wait in order to do so. Kind of forgot about the question since then. – Robert – 2013-03-04T14:23:41.587

Answers

13

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"
 )

Robert

Posted 2012-05-14T03:03:13.653

Reputation: 371

5

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.

Bye

Posted 2012-05-14T03:03:13.653

Reputation: 301