Search code examples
batch-filefor-loopforfiles

How to start a program with parameters for every files in directory?


I'd like to run a program which takes an input file and an output file with a parameter. I'd like to do so, for every file in my folder.

I tried with a for loop and forfiles, but I can't manage to make it work.

The syntax for the application is:

gdcmconv [options] file-in file-out

Here is my code:

set INPUTPATH="C:\input\"
set OUTPUTPATH="C:\Output\"

@echo on

for /D %%G in (%INPUTPATH%) do (
echo '%%~nxG'
"C:\Program Files\GDCM 2.8\bin\gdcmconv.exe" -X %%~nxG %%~nxG)

also tried:

forfiles /p %INPUTPATH% /s /m *.bat /c "cmd /c "C:\Program Files\GDCM 2.8\bin\gdcmconv.exe //X %INPUTPATH%@file %OUTPUTPATH%@file"

It seems it does not get the @file or %%~nxG as input or output


Solution

  • The solution you request is as follows:

    @echo off
    
    set INPUTPATH="C:\input\"
    set OUTPUTPATH="C:\Output\"
    
    for /R "%INPUTPATH%" %%A IN (*.bat) do (
        rem echo '%%~nxG'
        "C:\Program Files\GDCM 2.8\bin\gdcmconv.exe" -X %%~nxG %%~nxG
    )
    

    for /R loops through all subfolders.

    The rest of the code searches in these subfolders for .bat files, and process them as you requested.