Windows batch file with loop through subfolders

前端 未结 1 481
自闭症患者
自闭症患者 2021-01-15 00:27

I didnt succeed writing an approriate batch file and would appreciate some help.

Let\'s say I have an exe in D:\\Test\\ called script.exe. To execute this script.exe

1条回答
  •  逝去的感伤
    2021-01-15 01:09

    For direct execution from within a command prompt window:

    for /R "D:\Test" %I in (*.bin) do @D:\Test\script.exe -i "%I"
    

    And the same command line for usage in a batch file:

    @for /R "D:\Test" %%I in (*.bin) do @D:\Test\script.exe -i "%%I"
    

    The command FOR searches recursive in directory D:\Test and all subdirectories for files matching the wildcard pattern *.bin.

    The name of each found file is assigned with full path to case-sensitive loop variable I.

    FOR executes for each file the executable D:\Test\script.exe with first argument -i and second argument being the name of found file with full path enclosed in double quotes to work for any *.bin file name even those containing a space or one of these characters: &()[]{}^=;!'+,`~.

    @ at beginning of entire FOR command line just tells Windows command interpreter cmd.exe not to echo the command line after preprocessing before execution to console window as by default.

    @ at beginning of D:\Test\script.exe avoids the output of this command to console executed on each iteration of the loop.

    Most often the echo of a command line before execution is turned off at beginning of the batch file with @echo off as it can be seen below.

    @echo off
    for /R "D:\Test" %%I in (*.bin) do D:\Test\script.exe -i "%%I"
    

    For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

    • echo /?
    • for /?

    0 讨论(0)
提交回复
热议问题