Tag Archives: batch

Logical operators AND, OR in batch

There is no method to combine operators in windows BATCH command shell.
But we can easily emulate it!

For AND operator use next snippet:

if %var% GEQ 1 (
    if %var% LEQ 10 (
        // do...
    )
)

For OR operator we have to use additional variable:

set result=false

if %var% LEQ 0 set result=true
if %var% GEQ 10 set result=true

if "%result%" EQU "true" (
   // do...
)

Thanx to http://stackoverflow.com/a/2143396

Run multiple bat files from single bat file

Windows batch file allows to run another batch file inside of it. There are 2 common way to do it:
1. Using CALL command:
bat_call

2. Using START command:
bat_start

The difference between them is the START command runs asynchronously, i.e. it doesn’t wait for finishing previous execution!
Instead of CALL command which is ok for sequential execution.

Thanx to http://stackoverflow.com/a/1104009

Passing command line arguments to ant script file

ant-promt
You may know such ant task as Input – http://ant.apache.org/manual/Tasks/input.html.
It promts user to enter some stuff. But what if you want to run script without promt but using command line arguments only?
Ant presents the awesome solution: just use -D option!
See more at http://ant.apache.org/manual/running.html

Consider we have ant script run.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>

<project name="someProject" default="someTask" basedir=".">
    <taskdef resource="net/sf/antcontrib/antlib.xml" />

    <target name="someTask">
        <input message="Enter first argument" addproperty="arg.first" />
        <input message="Enter second argument" addproperty="arg.second" />
        
        <!-- your code here... -->
        <copy file="arg.first" tofile="${arg.second}" />
    </target>
</project>

To run it in batch mode use:

  ant -buildfile run.xml someTask -Darg.first=111.txt -Darg.second=111_copy.txt

Thanx to http://stackoverflow.com/a/14309773

Using SETLOCAL and ERRORLEVEL together in Batch

bat-file-icon
Interesting thing about using SETLOCAL and ERRORLEVEL together.
Consider two files:

first.bat

@echo off

echo %ERRORLEVEL% in first bat before setlocal

setlocal

echo Some actions with error finished here...

set ERRORLEVEL=1
echo %ERRORLEVEL% in first bat.

exit /b %ERRORLEVEL%

second.bat

@echo off

echo %ERRORLEVEL% in second bat before setlocal

setlocal

echo %ERRORLEVEL% in second bat after setlocal

Exec in series:

C:\first.bat && second.bat

You’ll see the next output:

0 in first bat before setlocal
Some actions with error finished here...
1 in first bat.
1 in second bat before setlocal
0 in second bat after setlocal

More info http://ss64.com/nt/setlocal.html

Right way to define time spent in Batch

patrick
Here is an example of how to calculate the time spent during batch script execution:

REM Start timer
call z_timer_start.bat

REM Action
...

REM Stop timer
call z_timer_finish.bat

And here are the scripts.

z_timer_start.bat

@echo off

REM Settings
set start_time=%time%

REM Get start time
for /f %%i in ('z_strtoint %start_time:~0,2%') do set /a hours_before=%%i
for /f %%i in ('z_strtoint %start_time:~3,2%') do set /a minutes_before=%%i
for /f %%i in ('z_strtoint %start_time:~6,2%') do set /a seconds_before=%%i

z_timer_finish.bat

@echo off

REM Settings
set finish_time=%time%
set debug=0

REM Get finish time
for /f %%i in ('z_strtoint %finish_time:~0,2%') do set /a hours_after=%%i
for /f %%i in ('z_strtoint %finish_time:~3,2%') do set /a minutes_after=%%i
for /f %%i in ('z_strtoint %finish_time:~6,2%') do set /a seconds_after=%%i

REM Make calc
set /a before=%hours_before% * 3600 + %minutes_before% * 60 + %seconds_before%
set /a after=%hours_after% * 3600 + %minutes_after% * 60 + %seconds_after%
set /a timediff=%after%-%before%

REM Show result
echo.
echo Total time spent: %timediff% seconds

REM Show debug
if "%debug%" == "1" (
	echo.
	echo Timer debug-info:
	
	echo.
	echo start_time:  %start_time%
	echo finish_time: %finish_time%
	
	echo.
	echo hours_before:   %hours_before%
	echo minutes_before: %minutes_before%
	echo seconds_before: %seconds_before%
	
	echo.
	echo hours_after:  %hours_after%
	echo minutes_after: %minutes_after%
	echo seconds_after: %seconds_after%
	
	echo.
	echo before: %before%
	echo after:  %after%
	echo timediff: %timediff%
)

And use z_strtoint from here remove-leading-zeros-from-string-in-batch/

You can download all files here: batch_timer

Remove leading zeros from string in Batch

sacsC
It is often need to convert string to int in batch. But:

set /a int=0100
echo Int: %int%

Will result

Int: 64

It ‘s because lead zero – seen as a HEX (0xNUMBER)

To solve use next:

@echo off
setlocal enableextensions enabledelayedexpansion

REM Removes leading "0" from string

REM Settings
set z_strtoint=%1

REM Init
set strterm=___ENDOFSTRING___
set tmp=%z_strtoint%%strterm%

REM Action
:loop
set char=%tmp:~0,1%
set tmp=%tmp:~1%
REM echo char: %char%
if [!char!] EQU [0] set z_strtoint=!z_strtoint:~1!
if [!char!] NEQ [0] goto endloop
if [!tmp!] NEQ [!strterm!] goto loop
:endloop

REM Show result
echo %z_strtoint%

So, exec:

z_strtoint 0100
100

Save result of program to variable in Batch

Construct_Sequential

set cmd=application
set arg1=0101
for /f %%i in ('%cmd% %arg1%') do set result=%%i
echo =%result%

Thanx to http://stackoverflow.com/a/2340018