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

Setup:

  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

Usage:

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

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

Advertisements

2 thoughts on “[Quicktip] Totalcommander, batch-files and foreach

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s