0

I want to trigger a windows command over multiple files inside a directory at the same time. 'FOR' loop and other recursive methods are triggering the commands one after another. I need an alternative that can run the same command on all files at the same time. The present code I have is

@echo off
call :scan
goto :eof
:scan
for %%f in (*.txt) do *mycommand* -i %%f
for /D %%d in (*) do (
    cd %%d
    call :scan
    cd ..
)
exit /b
flba
  • 379
  • 1
  • 11
MANOJ
  • 1
  • 4

1 Answers1

0

The easiest way to have slow tasks running in parallel on Windows is by using the start command and calling another batch file, e.g.:

scan.bat

@echo off
echo Processing %1
dir %1
exit

main.bat

for %%f in (*.txt) do start scan.bat %%f

This will create a new window for each scan.bat instance. If you want to use the current window, use start /b scan.bat. The exit command at the end of scan.bat is important so that the command processor exists (so that the window is closed).

In case you want to limit the number of tasks running in parallel, you should use something more powerful like gnu make (which can be used on Windows with cygwin or mingw; see Limiting the number of spawned processes in batch script for a solution using batch files (from @LotPings).

flba
  • 379
  • 1
  • 11
  • 1
    Why should there be a difference in calling an external batch file instead of an internal sub routine? He just has to `start *mycommand* -i %%f` but is in danger to exhaust resources => [limiting-the-number-of-spawned-processes-in-batch-script](https://stackoverflow.com/questions/25172069/limiting-the-number-of-spawned-processes-in-batch-script) –  Sep 03 '19 at 10:45
  • I agree that this is dangerous as I noted in my answer. The problem of calling the command directly, like ```start dir```, is that the window stays open (the ```exit``` is important). – flba Sep 03 '19 at 10:48
  • The `exit` is not needed if you run the batch file by `start "" [/B] cmd /C scan.bat ...`... – aschipfl Sep 03 '19 at 14:08