batch file scripting essentials

I found that from time to time I need to work with windows batch file because scripting is still a tiny part of a developer's life. most of time I found myself searching web about how to do something even if it is small task in batch file. I am tired of doing this now so I decided to put a summary on what I should know to do common tasks. The below summary meets >80% of my needs. I keep it in a good place whenever I need to write batch file I will just take a quick look at what I put together. easy and simple.

 

~ MAGIC

The following syntax does correctly expand to the path of the current batch script.

 %~dp0                                                             //the path of current batch file. It ends with '\'

set testtools=%~dp0tools                            //

set testmode=%1                                          // the first parameter

set testtarget=%~f2                                      // the second parameter (full path to the file)

set testtargetdir=%~dp2                             // the second parameter (directory only)

 

The magic variables %n contains the arguments used to invoke the file: %0 is the path to the bat-file itself, %1 is the first argument after, %2 is the second and so on. Since the arguments are often file paths, there is some additional syntax to extract parts of the path. ~d is drive, ~p is the path (without drive), ~n is the file name. They can be combined so ~dp is drive+path.  %~dp0 is therefore pretty useful in a bat: it is the folder in which the executing bat file resides.

 

You can also get other kinds of meta info about the file: ~t is the timestamp, ~z is the size.

  

IF STATEMENT

IF EXIST filename ...

IF %value% LSS 10 ...

IF /I NOT "%string1%"=="string2" ...

IF NOT ERRORLEVEL 1 ...

 

IF %1 LSS 10 (

IF %2 GTR 0 (

ECHO %%1 is less than 10 AND %%2 is greater than 0

)

)

 

Operator Meaning

EQU equal to

NEQ not equal to

LSS less than

LEQ less than or equal to

GTR greater than

GEQ greater than or equal to

 

FOR LOOP

/D Indicates that the set contains directories.

/R Causes the command to be executed recursively through the sub-directories of an indicated parent directory

/L Loops through a command using starting, stepping, and ending parameters indicated in the set.

/F Parses files or command output in a variety of ways

 

 

for %%X in (set) do (command)

for %%X in (file1 file2 file3) do command

For %%X in (eenie meenie miney moe) do (echo %%X)

for %%X in (*.jpg) do command

for %%X in (*.jpg *.gif *.png *.bmp) do command

 

for /l %%X in (start, step, end) do command

for /l %%X in (1,1,99) do (echo %%X >> E:\numbers.txt)

 

Working with directories

for /d %%X in (directorySet) do command

for /d %%X in (C:\*) do echo %%X

Recursing through sub-directories

for /r C:\pictures %%X in (*.jpg) do (echo %%X >> E:\listjpg.txt)

 

for /f [options] %%X in (source) do command

 

MISC

set /p name= What is your name?  //prompt user to input