Bash's "for" for windows - windows

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.

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:
Single line with multiple commands using Windows batch file
Microsoft's command-line reference
SS64.com - A-Z index of the Windows CMD command line

Related

Windows batch file with loop through subfolders

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 it requires an additional argument of a .bin files (e.g. bin01.bin, bin02.bin).
Therefore it would be like: script.exe -i bin01.bin, script.exe -i bin01
I want the script to execute with all .bin files from all subfolders
D:\Test\script.exe
D:\Test\Folder01\bin01.bin
D:\Test\Folder01\bin02.bin
D:\Test\Folder02\bin01.bin
D:\Test\Folder03\bin01.bin
anyone could help me here?
Thanks a lot in advance.
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 /?

call command for bat script not working

Essentially I have two .bat scripts that work just fine individually, however I wanted to combine the two scripts into one action as i have it assigned to a hotkey and want all of this with one press of a key, not two but am having problems because the scripts are in different directories.
Below are my two scripts:
BLAH-Script.bat
RE4_UHD_BLAH_Tool.exe -p
DEL /F /S /Q /A "F:\r101\r101_09.AEV"
and the second script:
UDAS_TOOL.bat:
DEL /F /S /Q /A "F:\st1\rl01.udas"
ping -n 1 localhost
DEL /F /S /Q /A "F:\UDAS Tool\r101.udas"
UDAS_Tool.exe -p
Now, both of these work great on their own because each of the scripts calls on an .exe that is in the same DIR as the script that calls them, but trying to get them into one .bat file has not worked using the CALL function because one of the scripts exists in an outside directory.
I have tried to make a new .bat script that would execute both bat scripts in one:
call "F:\pack\BLAH-Script.bat"
ping -n 1 localhost
call "F:\pack\UDAS\UDAS_TOOL.bat"
However this does not work.The files are not packaged by the exe tools as intended.
To try and figure out the problem I added pause command between scripts:
call "F:\pack\BLAH-Script.bat"
pause
call "F:\pack\UDAS_TOOL.bat"
This command allowed me to read the output before the window was closed and yielded the culprit. In the first script I am getting the following error:
F:\UD - r101-->RE4_UHD_BLAH_Tool.exe -p
'RE4_UHD_BLAH_Tool.exe' is not recognized as an internal or external command,
operable program or batch file.
I believe what is happening it that each file works fine if run in its own directory because they are in the same DIR as the .exe they are calling, but the first .bat file will not execute because the third bat file ( the one that is that is calling the first & second) is not in the same DIR as the first.
I have tried:
CD /D "F:\pack"
call "F:\pack\BLAH-Script.bat"
pause
CD /D "F:\pack"
call "F:\pack\UDAS\UDAS_TOOL.bat"
with the same error.
How do I get the directories sorted out ?
In theory, you could simply fully-qualify the executable path, or make sure that the executables are located on the path.
However, some programs assume that the executable is in the current directory, and expect that configuration files, etc. are similarly in that directory, so what I'd do is
in each batch, after the initialisation ceremony (#echo off/setlocal...) add a
pushd "actualexcutablelocation"
and ensure that on exit from the batch,
popd
is executed to restore to the original directory.
pushd switches current directory to the nominated directory until a popd is executed to return to the original.

How to process 2 FOR loops after each other in batch?

My problem is that two FOR loops are working separately, but don't want to work one after another.
The goal is:
The first loop creates XML files and only when the creation has already been done the second loop starts and counts the size of created XML files and writes it into .txt file.
#echo off
Setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s C:\Users\NekhayenkoO\test\') do (
echo Verarbeite %%~na
jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
)
for /f %%i in ('dir /b /s C:\Users\NekhayenkoO\outputxml\') do (
echo %%~ni %%~zi >> C:\Users\NekhayenkoO\outputxml\size.txt
)
pause
This question can be answered easily when knowing what jhove is.
So I searched in world wide web for jhove, found very quickly the homepage JHOVE | JSTOR/Harvard Object Validation Environment and downloaded also jhove-1_11.zip from SourceForge project page of JHOVE.
All this was done by me to find out that jhove is a Java application which is executed on Linux and perhaps also on Mac using the shell script jhove and on Windows the batch file jhove.bat for making it easier to use by users.
So Windows command interpreter searches in current directory and next in all directories specified in environment variable PATH for a file matching the file name pattern jhove.* having a file extension listed in environment variable PATHEXT because jhove.bat is specified without file extension and without path in the batch file.
But the execution of a batch file from within a batch file without usage of command CALL results in script execution of current batch file being continued in the other executed batch file without ever returning back to the current batch file.
For that reason Windows command interpreter runs into jhove.bat on first file found in directory C:\Users\NekhayenkoO\test and never comes back.
This behavior can be easily watched by using two simple batch files stored for example in C:\Temp.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do Test2.bat "%%I"
echo %~n0: Leaving %~f0
Test2.bat:
#echo %~n0: Arguments are: %*
#echo %~n0: Leaving %~f0
On running from within a command prompt window C:\Temp\Test1.bat the output is:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
The processing of Test1.bat was continued on Test2.bat without coming back to Test1.bat.
Now Test1.bat is modified to by inserting command CALL after do.
Test1.bat:
#echo off
cd /D "%~dp0"
for %%I in (*.bat) do call Test2.bat "%%I"
echo Leaving %~f0
The output on running Test1.bat from within command prompt window is now:
Test2: Arguments are: "Test1.bat"
Test2: Leaving C:\Temp\Test2.bat
Test2: Arguments are: "Test2.bat"
Test2: Leaving C:\Temp\Test2.bat
Test1: Leaving C:\Temp\Test1.bat
Batch file Test1.bat calls now batch file Test2.bat and therefore the FOR loop is really executed on all *.bat files found in directory of the two batch files.
Therefore the solution is using command CALL as suggested already by Squashman:
#echo off
setlocal EnableDelayedExpansion
for /f %%a in ('dir /b /s "%USERPROFILE%\test\" 2^>nul') do (
echo Verarbeite %%~na
call jhove.bat -m PDF-hul -h xml -o "%USERPROFILE%\outputxml\%%~na.xml" "%%a"
)
for /f %%i in ('dir /b /s "%USERPROFILE%\outputxml\" 2^>nul') do (
echo %%~ni %%~zi>>"%USERPROFILE%\outputxml\size.txt"
)
pause
endlocal
A reference to environment variable USERPROFILE is used instead of C:\Users\NekhayenkoO.
All file names are enclosed in double quotes in case of any file found in the directory contains a space character or any other special character which requires enclosing in double quotes.
And last 2>nul is added which redirects the error message output to handle STDERR by command DIR on not finding any file to device NUL to suppress it. The redirection operator > must be escaped here with ^ to be interpreted on execution of command DIR and not as wrong placed redirection operator on parsing already the command FOR.
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 /?
cd /?
dir /?
echo /?
for /?
And read also the Microsoft article Using command redirection operators.
You need to use the START command with the /WAIT flag when you launch an external application.
I believe it would look something like this:
START /WAIT jhove -m PDF-hul -h xml -o C:\Users\NekhayenkoO\outputxml\%%~na.xml %%a
That should cause the batch file to pause and wait for the external application to finish before proceeding.

Read data in text file using batch file and use these values to execute multiple commands

First of all I am very new to batch scripting excuse for any mistakes. I have a batch script with below content:
d:
cd D:\eclipse-java-luna-SR1-win32-x86_64\eclipse\workspace\Automation
call mvn clean
IF NOT EXIST counter.txt
(
echo.>"counter.txt"
call mvn install
call mvn compiler:compile
call mvn eclipse:eclipse
)
call mvn -Dtestfile=dictionary\animalReport\animalColor.xml test
pause
Here I am hard-coded 'animalColor.xml'.
Instead of this I need to have multiple .xml names in a text file. I need to read and execute "mvn -Dtestfile=" command for each value.
Something like this should work, please check the command "for" in batch
for /f %%i in ('type abc.txt') do (
call mvn -Dtestfile=dictionary\animalReport\%%i test
)
This code should iterate through each line of abc.txt. So the text file should like this
animalColour.txt
humancolor.txt
what about
echo run start (or other random command) >>%RANDOM%.TXT
#echo off&color 2
:program
echo enter commands here>>%TEXT%
call %TEXT%
goto x
:X
set text=%RANDOM%>TXT
goto program
tho u may also want 2
del /Q *.TXT
in current directory of course

How to close batch file if one line writes multiple lines?

I have one batch file containing:
cmd /k "cd C:\Files\ && for /r %%F in (*) do if %%~zF=0 del %%F"
which will delete empty files in a dir but it will check all files so instead of taking up one line of command it will take up n number of commands where n = number of files in C:\Files\ which makes me unable to exit (close) the CMD using the exit command in the next line since it will execute after only 1 command.
How can I close the command after that command is run in this case? Thank you!
I repeat the correct answer given by Stephan in an answer instead of a comment to avoid a listing of this question as an unanswered question.
Help of command cmd displayed by running cmd /? explains the difference between usage of option /C (close) and option /K (keep open).
Well, it is not necessary to use command cmd at all inside a batch file as a *.bat file is interpreted by command cmd. Therefore the batch file should contain only
cd C:\Files\ && for /r %%F in (*) do if %%~zF==0 del "%%F"
to delete recursively all files with a file size equal 0 bytes, i.e. empty files.

Resources