0

How to can I to write this linux-like loop in the Windows 7 command line?

for docker_path in `ls | grep "docker$"`
do
    cd $docker_path
    mvn -B -f pom.xml clean deploy -Pdocker
    cd ..
done

I need found all *docker/ directory, exec there mvn-command and return into patern directory, but for Windows7 system.

Piknik
  • 43
  • 1
  • 6

1 Answers1

1

On Windows command line use:

for /D %I in (*docker) do @pushd "%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd

Use the following command line if mvn is not an executable, but a *.bat or *.cmd file:

for /D %I in (*docker) do @pushd "%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd

Well, on command prompt it is not really necessary to use command CALL to run a batch file in a loop together with two other commands as done here.

The first command line for usage in a batch file:

@for /D %%I in (*docker) do @pushd "%%I" & mvn.exe -B -f pom.xml clean deploy -Pdocker & popd

In a batch file the loop variable I must be referenced with two percent signs instead of just one as on command prompt. The loop variable can be only a single character.

The second command line for usage in a batch file:

for /D %%I in (*docker) do @pushd "%%I" & call mvn.bat -B -f pom.xml clean deploy -Pdocker & popd

In a batch file it is necessary to use command CALL to call another batch file and continue with next command respectively command line in current batch file after execution of the other batch file finished.

Windows command processor cmd.exe continues execution of current batch file on the other batch file on not using command CALL and exits batch file processing once the execution of other batch file finished without further processing command lines in current batch file.

See answer on How to call a batch file that is one level up from the current directory? for details on the existing methods to run a batch file from within a batch file.

The single command line can be also coded with multiple lines:

@echo off
for /D %%I in (*docker) do (
    pushd "%%I"
    mvn.exe -B -f pom.xml clean deploy -Pdocker
    popd
)

And the same multi-line batch file solution in case of mvn is a batch file.

@echo off
for /D %%I in (*docker) do (
    pushd "%%I"
    call mvn.bat -B -f pom.xml clean deploy -Pdocker
    popd
)

Command FOR with option /D searches with wildcard pattern *docker for non hidden directories in current directory of which directory name ends with the string docker.

It is advisable on Windows to reference a file to execute with complete name of file, i.e. file name + file extension. This makes it clear for Windows command processor as well as every reader of the code if the executed file is an executable or a script file which makes a difference as it can be seen here.

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.

  • call /?
  • echo /?
  • for /?
  • popd /?
  • pushd /?

See also:

Mofi
  • 46,139
  • 17
  • 80
  • 143