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, if you have managed to answer your own question, please add your own answer and accept it as the correct one. Commented Mar 2, 2013 at 21:05
  • 1
    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
    Commented Mar 2, 2013 at 21:55
  • @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
    Commented Mar 4, 2013 at 14:23

3 Answers 3


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"
  • 3
    If running from command line (as opposed to a batch file), replace each %%f with %f
    – ashleedawg
    Commented Nov 16, 2020 at 12:29

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.


I had a similar task - to remove last 3 pages from each pdf in a dir from the cmd

for %f in (*.pdf) do ( qpdf "%f" --pages . 1-r4 -- --replace-input)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .