The purpose of this howto is to explain how to efficently use command line arguments in batch.
1. The well-known part:
Each time a batch file is run with arguments, the first argument can be referred by %1, the second one by %2,the third one by %3 and so on until the nineth. If there are more than nine arguments you will have to use the SHIFT command.
Let's start with an example:
| Source: MyBatch1.bat | |
|
@ECHO OFF REM Print the first argument ECHO argument 1: %1 REM Print the fourth argument ECHO argument 4: %4 |
|
| From a dos prompt (cmd.exe), change the directory to get where MyBatch1.bat is located (CD /D Directory_of_MyBatch1.bat will do the job) and run the following: | |
| MyBatch1 "arg 1" "arg 2" "arg 3" "arg 4" "arg 5" | |
| The resulting output is as follows: | |
|
argument 1: "arg 1" argument 4: "arg 4" |
2. The rather well-known part
Each time a batch is run, %0 designates the invocated batch file (this is a quite interesting feature - see below).
Example:
Source: MyBatch2.bat
|
@ECHO OFF ECHO %0 |
From a dos prompt (cmd.exe), change the directory to get where MyBatch2.bat is located (CD /D Directory_of_MyBatch2.bat will do the job) and run the following:
| MyBatch2 |
The resulting output is as follows:
| MyBatch2 |
3. The not so well-known part (but very, very usefull)
Most of these features allow you to extract information from a file passed as an argument.
Some modifiers will help to extract information from a file passed as argument (let's consider a file passed as the first argument):
- %~1 removes any double quotes (not limited to files)
- %~f1 refers to the full path filename
- %~d1 refers to the drive where the file is located
- %~p1 refers to the path (without the drive, filename and extension)
- %~n1 refers to the filename (no path, no drive, no extension)
- %~x1 refers to the file extension (with a dot)
- %~s1 refers to a full short path filename (FAT16 compatible)
- %~a1 refers to the file attributes
- %~t1 refers to the file date/time
- %~z1 refers to the file size
Example:
Source: MyBatch3.bat
|
@ECHO OFF REM %1 ECHO file name without any transformation: %1 REM %~1 removes any double quotes (not limited to files) ECHO file name without double quotes: %~1 REM %~f1 refers to the full path filename ECHO full path file name: %~f1 REM %~d1 refers to the drive where the file is located ECHO file drive: %~d1 REM %~p1 refers to the path (without the drive, filename and extension) ECHO file path: %~p1 REM %~n1 refers to the filename (no path, no drive, no extension) ECHO file name: %~n1 REM %~x1 refers to the file extension (with a dot) ECHO file extension: %~x1 REM %~s1 refers to a full short path filename (FAT16 compatible) ECHO short file name: %~s1 REM %~a1 refers to the file attributes ECHO file attributes: %~a1 REM %~t1 refers to the file date/time ECHO file date and time: %~t1 REM %~z1 refers to the file size ECHO file size: %~z1 REM Refers to a log file located in the same directory and whose filename is the same as the one provided as first argument ECHO log file: %~dpn1.log |
From a dos prompt (cmd.exe), change the directory to get where MyBatch3.bat is located (CD /D Directory_of_MyBatch3.bat will do the job) and run the following:
| MyBatch3 "MyBatch2.bat" |
The resulting output is as follows:
|
file name without any transformation: "MyBatch2.bat" file name without double quotes: MyBatch2.bat full path file name: C:\Documents and Settings\nicetuna\MyBatch2.bat file drive: C: file path: \Documents and Settings\nicetuna\ file name: MyBatch2 file extension: .bat short file name: C:\DOCUME~1\nicetuna\MyBatch2.bat file attributes: --a------ file date and time: 14/10/2009 01:22 file size: 35 log file: C:\Documents and Settings\nicetuna\MyBatch2.log |
4. Some tricks, I often use:
These tricks will show you how
-To deal with spaces within path
-To create a log file with the same name and in the same directory as the batch file
-To remove surrounding quotes
4.1. Let's prepare the environment
Open a DOS prompt (cmd.exe) and type:
| md "c:\My Batches" |
Notice the space in "c:\My Batches".
4.2. Now let's create the batch "C:\My Batches\trick1.bat" (have a look to all the comments):
|
@ECHO OFF REM Retrieve the directory of the batch called SET BATCHDIR=%~dp0 REM The log file will have the same name and REM will be located in the same directory as the batch called SET LOGFILE=%~dpn0.log REM delete the log file if already exists - notice the usage of REM the double-quotes IF EXIST "%LOGFILE%" DEL "%LOGFILE%" REM Log the start of the batch ECHO %DATE% %TIME%: %0 started >>"%LOGFILE%" REM Display BATCHDIR into the logfile ECHO BATCHDIR=%BATCHDIR% >>"%LOGFILE%" REM Display the parameter 1 into the logfile - notice the usage of %%1 ECHO %%1=%1 >>"%LOGFILE%" REM Remove any surrounding double-quotes from the parameter 1 SET PARAM1=%~1 REM Display PARAM1 into the logfile ECHO PARAM1=%PARAM1% >>"%LOGFILE%" REM Log the stop of the batch ECHO %DATE% %TIME%: %0 stopped >>"%LOGFILE%" REM Go to the end of the batch - It is generally a good practice to do so GOTO :EOF |
4.3. From a DOS prompt (cmd.exe) type:
| "C:\My Batches\trick1.bat" "with surrounding double-quotes" |
4.4. Open the resulting log "C:\My Batches\trick1.log".
It should display something like:
|
31/10/2009 2:10:59,94: "C:\My Batches\trick1.bat" started BATCHDIR=C:\My Batches\ %1="with surrounding double-quotes" PARAM1=with surrounding double-quotes 31/10/2009 2:10:59,94: "C:\My Batches\trick1.bat" stopped |
Conclusion
We are now at the term of this article and I hope that the most important aspects of the batch arguments have been addressed.
If you need further explanation or want me to cover a specific subject fill the contact form.
Coming soon:
Batch advanced - HOWTO CALL
Batch advanced - HOWTO FOR
