This is a draft cheat sheet. It is a work in progress and is not finished yet.
Settings
SETLOCAL ENABLEDELAYEDEXPANSION
Enable delayed environment variable expansion. |
For loop
FOR %variable IN (set) DO command [command-parameters]
FOR /D
FOR /R
FOR /L
Loop against a set of files
FOR /F ["options"] %%parameter IN (filenameset|"Text String") DO command
options:
delims=xxx - The delimiter character(s) (default = a space)
skip=n - A number of lines to skip at the beginning of the file. (default = 0)
eol=; - Character at the start of each line to indicate a comment (default = semicolon)
tokens=n - Specifies which numbered items to read from each line (default = 1)
usebackq
1) Use the alternate quoting style:
2) Use double quotes for long file names in "filenameset".
3) Use single quotes for 'Text string to process'
4) Use back quotes for command to process
Examples:
FOR %%a IN (%*) DO ECHO %%a |
Choice
**Creates a pause for 2 seconds
CHOICE /T 2 /D Y > NUL |
|
|
Batch Parameters Modifiers
%~1 - expands %1 removing any surrounding quotes (")
%~f1 - expands %1 to a fully qualified path name
%~d1 - expands %1 to a drive letter only
%~p1 - expands %1 to a path only
%~n1 - expands %1 to a file name only
%~x1 - expands %1 to a file extension only
%~s1 - expanded path contains short names only
%~a1 - expands %1 to file attributes
%~t1 - expands %1 to date/time of file
%~z1 - expands %1 to size of file
The modifiers can be combined to get compound results:
%~dp1 - expands %1 to a drive letter and path only
%~nx1 - expands %1 to a file name and extension only
Changing a file extension to create a log file name:
SET MyLogFile=%~dpn0.log |
|
|
Service Control
Status query a service on a machine
SC localhost QUERY WPCSvc
Stop a service on a machine
SC \\SERVERNAME STOP WPCSvc |
|