Batch Syntax Help

December 29, 2008 at 07:18:31
Specs: XP, P4 & 1GB
after few days playing with batch, i encounter a few syntax problem. please help me.

1. IF Else statement, how do i write the following line in batch (preferably one line code)
if condition1==true or condition2==true (
do statement1
) else (
do statement2

2. whats the difference between &, &&, |, || syntax/operators?

3. FOR /F "tokens=*" %%L IN (%1) DO SET LastLine=%%L
i used this syntax to get the last line of txt file, but after some time my txt file grows to 17meg and takes longer to execute. is there any workaround in this?

4. set f=.\somepath\morepath\filename.txt
how do i retrieve the relative path name (not full path name)?

5. how to write this one line code in batch without else statement.
10>1 ? true:false

6. how to do bitwise operation in batch??
eg. 1 SHL 2 = 4

i still have some more problems that i encounter, maybe later i'll post again when i remind it.

See More: Batch Syntax Help

Report •

December 29, 2008 at 15:53:08

There is no or separator in the if statements, || comes very close but I will get to that later. If statements can be nested so my suggestion would be to use:

if condition1==true (do command) else if condition2==true do command


& is a command separator


Both commands will be executed independantly and regardless of each others outcomes.

&& is an "and" type operator, if command1 returns an errorlevel of 0 commannd2 will be executed otherwise it will not.


|| is an "or" type separator, if the command1 returns an errorlevel greater than zero command2 will be executed, otherwise it will not.


The trap to watch out for with || and && is that some commands either set unexpected errorlevel or none at all. I know the below would work with else, it is an example of an outcome other than what may be expected, because the command in question doesn't set an errorlevel:

if 11==66 echo ee||echo ii

| is a pipe it redirects the standard output of one command into the standard input of another.

ver|find /i "version"


The for /f loop is the only inbuilt method to read lines, you could try third party utilities like sfk or or the gnu utils for windows. In both cases the tail command seems to be the one of interest.

If the pathname is part of the current directory you can move one direction back with

command ..\filename

This can recur for as many directories as there is on the current directory.

command ..\..\..\filename

If you want the root of the drive then it is also possible to use:

command \filename


I don't think you can....


Set /a handles all the math, and sometimes that can be a pain. Numbers are limited to the 32bits on 32bit windows and I assume 64bits on 64bit windows. No floating point math is peformed i.e "set /a 10/3 = 3". Logical shifts are on << and >> and any operation containing them must be enclosed in double quotes because the greater and less than symbols are used to redirect input/output. So you statement is:

set /a "1<<2"

You can also set a variable to the output:

set /a var1="1<<2"

Report •

December 30, 2008 at 05:01:50
thank you for the help/tutorial. now i get a better understanding on batch programming.

on no.4, this is my problem:
SET f=".\Superset\S\S_%sTimeStamp%.csv"

set dirpath= -->\\how-to get relativepath of %f% ".\Superset\S"

set output="%dirpath%\zip\"

i know how to get the fullpathname, but its not clean on screen when i echo some message.
what i need is to get the relative dirpath from a variable. in this case:
Clean Output:
zipped .\Superset\S\S_081230.csv to .\Superset\S\zip\

Not Clean Output:
zipped .\Superset\S\S_081230.csv to C:\documents and settings\usernamehere\Desktop\New Folder\Test\Superset\S\Zip\

Report •

December 31, 2008 at 21:01:56
I don't think there is any inbuilt way to do this. You could parse parse %cd% to get the result. I had a stab at it and it works but you still need to add your application name. You could easily substitute a fully specified path name/your variable in place of %cd%.

set cdir="%cd%"
for %%a in (" =" "\= " "&=a") do set cdir=!cdir:%%~a!
set cdir=%cdir:"=%
for %%a in (%cdir%) do set /a tok+=1
if !tok!==1 (set rpath=.\&&goto output)
if !tok! lss 3 (set toks=!tok!) else set /a otok=tok-1&&set toks=!otok!,!tok!
for /f "tokens=%toks% delims=\" %%a in ("%cd%") do (
if "%toks:,=%"=="%toks%" (set rpath=.\%%a\) else set rpath=.\%%a\%%b\
echo !RPATH!

It's not the most elegent solution though.....

Report •

Related Solutions

December 31, 2008 at 21:33:31
Here is a slightly better version, this one takes the path with the file name.

set cdir="x:\path\dir\folder\file name.ext"
set cdir2=!cdir!
for %%a in (" =" "\= " "&=a") do set cdir=!cdir:%%~a!
set cdir=%cdir:"=%
for %%a in (%cdir%) do set /a tok+=1
set /a tok-=3
if !tok! lss 1 (
set rpath=.!cdir2:~3,-1!
) else (
for /f "tokens=%tok%,* delims=\" %%a in ("%cdir2%") do set rpath=.\%%b
set rpath=!rpath:"=!
echo !RPATH!

Report •

January 3, 2009 at 04:49:41
i was expecting there was somekind of built-in function to get relative pathname rather than using set path=%~dps1.

the code rocks, now digesting the code so i can be at least half as good batch coder as you. =)

thanks a lot jugado, and a big thank you for sharing the knowledge.

Report •

January 3, 2009 at 15:05:37
Nope no built in, the whole list of modifiers can be found with "for /?".

Report •

Ask Question