[Quicktip] Totalcommander, batch-files and foreach

You want to call a cmdline-tool, but this tool only supports only one file per invocation? But you have to process hundreds of files? With the following approach (in combination with the Totalcommander) you can select many files and handle each file sequentially.


  1. create a batchfile named foreach.bat
    @echo off
    REM call it via 'foreach.bat filewithfilenames'
    SET filewithfilenames=%1
    for /f %%f IN (%filewithfilenames%) do (
        REM insert your code here, the current iteration item is %%f
        echo %%f
  2. create a startermenu entry in totalcommander
    • name: foreach...
    • command: foreach.bat
    • parameter: %L


  • mark all files to be adressed in Totalcommander
  • call the newly created startermenu entry “foreach...

When calling the startermenu-entry the Totalcommander will save the filenames of the selected files in a temp-file and will provide the filename of the temp-file as parameter. That is the magic of the parameter %L. (Perhaps such a scheme is also available in Speedcommander, Freecommander and other tools like that?! I do not know…)

Now this temp-file can easily be read using a special syntax of the “for-loop” of the Windows command line (cmd). ┬áType in "for /?" in your cmdline for more details on this syntax and see the example above (<code>%%f</code> in line 6).