Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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:false6. how to do bitwise operation in batch??
eg. 1 SHL 2 = 4i still have some more problems that i encounter, maybe later i'll post again when i remind it.

1.
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 command2.
& is a command separator
command1&command2
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.
command1&&command2|| is an "or" type separator, if the command1 returns an errorlevel greater than zero command2 will be executed, otherwise it will not.
command1||command2The 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"3.
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.
4.
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 \filename5.
I don't think you can....
6.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"

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\S_%sTimeStamp%.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\S_081230.zipNot Clean Output:
zipped .\Superset\S\S_081230.csv to C:\documents and settings\usernamehere\Desktop\New Folder\Test\Superset\S\Zip\S_081230.zip

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%.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
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\
)
:output
echo !RPATH!
endlocal
pauseIt's not the most elegent solution though.....

Here is a slightly better version, this one takes the path with the file name.
@ECHO OFF&SETLOCAL ENABLEDELAYEDEXPANSION
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!
endlocal
pause

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.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |