Windows Command Line
Batch Parameters
The following 2 tables are based on the Microsoft XP batch reference [1].
Modifier | Description | Example ("c:\usr\appl\test.cmd") |
---|---|---|
%~1 | Expands %1 and removes any surrounding quotation marks (""). | %~0 : c:\usr\appl\test.cmd |
%~f1 | Expands %1 to a fully qualified path name. | %~f0 : c:\usr\appl\test.cmd |
%~d1 | Expands %1 to a drive letter. | %~d0 : c: |
%~p1 | Expands %1 to a path. | %~p0 : \usr\appl\ |
%~n1 | Expands %1 to a file name. | %~n0 : test |
%~x1 | Expands %1 to a file extension. | %~x0 : .cmd |
%~s1 | Expanded path contains short names only. | %~s0 : c:\usr\appl\test.cmd |
%~a1 | Expands %1 to file attributes. | %~a0 : --a------ |
%~t1 | Expands %1 to date and time of file. | %~t0 : 10/23/2009 16:19 |
%~z1 | Expands %1 to size of file. | %~z0 : 274 |
%~$PATH:1 | Searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found, this modifier expands to the empty string. | %~$PATH:0 : Path |
The commands can be combined, some examples:
Modifier | Description | Example ("c:\usr\appl\test.cmd") |
---|---|---|
%~dp1 | Expands %1 to a drive letter and path. | %~dp0 : C:\usr\Appl\ |
%~nx1 | Expands %1 to a file name and extension. | %~nx0 : Test.cmd |
%~dp$PATH:0 | Searches the directories listed in the PATH environment variable for %1 and expands to the drive letter and path of the first one found. | %-dp$PATH:0 : 0 |
%~ftza1 | Expands %1 to a dir-like output line. | %~ftaza0 : --a------ 10/23/2009 16:28 384 C:\usr\Appl\Test.cmd |
If then else
Nowadays cmd scripting has the option:
if @%PARAMETER%==@ ( Call :CreateParameter ) else ( Call :ParameterExists ) ...
Since execution of the lines is based on the length of the command line and the whole if..then..else..endif is one command line be careful NOT to create too long lines. If lines are too long the cryptical error Can not install .
Labels
Labels has to begin with a colon. If you get randomly the error message:
The system cannot find the batch label specified - <name of the label>
and this label does exist then possibly you are using: [2]
- a non-windows end-of-line (i.e. UNIX style).
- the label jumping-to must span a block boundary.
The reason of these strange errors can drive people mad [3]. There are a lot of examples and solutions but believe me. it is always the not Windows EOL. Check your favorite editor!
Subroutines
Subroutines are very useful. The following subroutine is an example which shows a lot of tip and tricks. The first examples makes usage of string manipulations [4], the second one is simplier and more straightforward to the goal [5]
Please note the usage of the EnableDelayedExpamsion. Without this settings the batch file does not work coprrect. See the settings of cmd /v:on option for more explanation (Use cmd /? ).
... SetLocal EnableDelayedExpansion ... :DeQuote 01 Set _DeQuoteVar=%1 02 Call Set _DeQuoteString=%%!_DeQuoteVar!%% 03 if [!_DeQuoteString:~0^,1!]==[^"] ( 04 if [!_DeQuoteString:~-1!]==[^"] ( 05 Set _DeQuoteString=!_DeQuoteString:~1,-1! 06 ) Else (Goto :EOF) 07 ) Else (Goto :EOF) 08 Set !_DeQuoteVar!=!_DeQuoteString! 09 Set _DeQuoteVar= 10 Set _DeQuoteString= 11 Goto :EOF
01: Sets the variable DeQuoteVar to the 1st paramater of the function
02: Call Set is used to replace one variable with values from another.
03: Removes the preceding quote. The !..! is used for the EnableDelayedExpansion.
04: Removes the ending quote, again needs EnableDelayedExpansion.
:DeQuote 01 setlocal 02 set theString=%~1 03 endlocal&set ret=%theString% 04 goto :eof
01: makes it local
02: The tilde reomoves both quoteson the 1st parameter
03: Ends the local and returns the value
04: Performs the return
In general a function can be used: [6]
:: ----------------------------------------------- :: What's the function doing... :: Parms and description :: Version number and Author :<Function Name> <Argument list> setlocal ENABLEEXTENSIONS <Body of function> endlocal&<Set return values> goto :EOF :: -----------------------------------------------
Example:
:: ----------------------------------------------- :: Removes the quotes around a string :: param %1 The string to be stripped :: return String without quotes :: 1.0.1.0 - Author :DeQuote setlocal set theString=%~1 endlocal&set ret=%theString% goto :eof :: -----------------------------------------------
Example CMD-Files
Unix la in Windows
The Unix command la (ls -Alh) is a very nice command. To have the same in Windows use: <syntaxhighlight lang="bash"> @Echo off
- ==================================================================
- -- Unix ls -al implementation on Windows. --
- -- Author: Harm Frielink 2010 --
- -- Copy this file to the windows system32 directory --
- -- %WINDIR%\System32 --
- -- 1.0.1.0 - 23 Sep 2010 - Introduction --
- ==================================================================
- Start
dir /ogn /a /n /4 %*
- Exit
</syntaxhighlight>
See also
External
- Dos Tips, Functions and CMD examples.
Internal
References
- ↑ Microsft Windows XP, Batch file reference.
- ↑ Stack Overflow, Why the system can not find the batch label, Common pitfalls in windows scripting.
- ↑ Batch-as-batch-can! (27.1.2006)
- ↑ SS64, Syntax-dequote
- ↑ Stack Overflow, How do you strip quotes out of an echoed string in a windows batch-file.
- ↑ Ritchy Lawrence Website, batch file library.