Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Windows-DOS Commands Help
- ==Screen|:- Commands for Screen control==
- TITLE COLOR CLS CHCP MODE GRAFTABLE
- ==File|:- Commands to manage Files==
- ===Text files|:- Process the contents of Text files===
- TYPE MORE PRINT FIND FINDSTR SORT FC COMP EDIT EDLIN DEBUG
- ===All files|:- Process files as Entire units===
- DIR TREE REN COPY XCOPY ROBOCOPY MOVE REPLACE MKLINK DEL ATTRIB CACLS ICACLS MAKECAB EXPAND PACKAGER FSUTIL
- ==Directory|:- Commands to create, change and remove Directories==
- MD CD PUSHD POPD RD SUBST APPEND MKLINK MOUNTVOL
- ==Disk|:- Commands for Disk maintenance==
- FORMAT LABEL VOL CHKDSK CHKNTFS CONVERT COMPACT CIPHER RECOVER DISKCOPY DISKCOMP
- ==Process|:- Commands to execute, list and cancel Programs==
- START RUNAS AT SCHTASKS TASKLIST TASKKILL TSKILL TSSHUTDN SHUTDOWN PATH ASSOC FTYPE SETVER
- ==Comms|:- Commands to manage Communications==
- MSG TELNET TFTP PING PATHPING NET OPENFILES DRIVERQUERY GPRESULT
- ==Batch|:- Values, modifiers and commands for Batch files==
- ===Char:Characters|:- Special Characters used in commands and Batch files===
- [code]Characters
- ,;= Separators for command parameters and FOR sets, besides spaces and tabs.
- *? Wild-card characters that matches several file and directory names.
- @ Placed before a command does not echo it even if ECHO is ON: @ECHO OFF
- > Send command output to a new disk file: COMMAND > OUTPUT.TXT
- The DOS waste bin is called NUL: COPY *.* DEST > NUL
- >> Append command output if the file exists: COMMAND >> APPEND.TXT
- 2> Send error messages to a disk file: COMMAND 2> ERRORS.TXT
- 2>&1 Send error messages to the same file of >: COMMAND > ALL.TXT 2>&1
- < Get command input from a disk file: COMMAND < INPUT.TXT
- & COM1 & COM2 is the same as execute COM1 and then execute COM2
- | COM1 | COM2 is equivalent to: COM1 > F.TMP & COM2 < F.TMP & DEL F.TMP
- but both commands run in parallel (in different cmd.exe contexts).
- && COM1 && COM2 is the same as: COM1 & IF NOT ERRORLEVEL 1 COM2
- || COM1 || COM2 is the same as: COM1 & IF ERRORLEVEL 1 COM2
- () Group together several commands: ( COM1 & COM2 & COM3 ) > THREEOUTS.TXT
- TESTCOM && ( COM1THEN & COM2THEN ) || ( COM1ELSE & COM2ELSE & COM3ELSE )
- % Expand the value of Batch parameters (%1 ...), FOR parameters (%%A ...)
- and Batch variables (%VAR%); %* always expands to all Batch parameters.
- A FOR command typed in the command-line must use a single percent char.
- ! Additional way (Delayed Expansion) to take the value of a variable: !VAR!
- ^ Insert, not process, the next special character: ECHO ^%VAR^% is %VAR%
- An exclamation mark can not be inserted in a Batch file this way
- if Delayed !VARIABLE! Expansion is Enabled.
- " Enclose a string that will not be processed as special characters:
- SET "HEADER=<ONE|TWO|THREE>"
- Exclamation marks are always processed if Delayed Expansion is Enabled.
- [/code]
- ===Vars:Variables|:- Normal and delayed expansions, and dynamic Variables===
- [code]Variables
- %var% Expand the value of Var variable: ECHO Var value is: %Var%
- %var:old=new% Substitute all occurrences of Old (case insensitive) by New
- in Var expansion. If Old start with asterisk, substitute from
- beginning of Var value until first occurrence of Old.
- %var:~pos,size% Substring of Var from 0-based Position by Size characters; if
- Pos or Size are negative, indicate a backwards position from
- end. If Size is omitted, the substring ends at last character.
- A variable may be enclosed in exclamation-marks instead of percent-signs to
- expand its value in Delayed Variable Expansion that happens after the normal
- (percent-signs) expansion, so both types may be combined. This feature allows
- some advanced manipulations, like multi-dimensional arrays. For example:
- SET /P OLD=Enter old part:
- SET /P NEW=Enter new part:
- SET CHANGED=!ORIGINAL:%OLD%=%NEW%!
- SET /P POS=Enter position:
- SET /P SIZE=Enter size:
- SET SUBSTRING=!VAR:~%POS%,%SIZE%!
- SET VECTOR[1]=Element One
- SET VECTOR[2]=Element Two, etc...
- SET I=set subscript value in some way
- ECHO The value of element %I% is: !VECTOR[%I%]!
- If there are several commands in one line or enclosed in parentheses, %normal%
- expansions are performed just once before the line or block is executed, but
- !delayed! expansions are performed each time that the commands are executed:
- SET VAR=Old value
- SET VAR=New value & ECHO Previous value: %VAR%, modified in this line: !VAR!
- In order to use Delayed Expansion, insert this command at beginning of Batch file:
- SETLOCAL ENABLEDELAYEDEXPANSION
- Dynamic variables (don't define any variable with same name):
- %CD% Expands to the current directory string.
- %DATE% Expands to current date using same format as DATE command.
- %TIME% Expands to current time using same format as TIME command.
- %RANDOM% Expands to a random decimal number between 0 and 32767.
- %ERRORLEVEL% Expands to the current ERRORLEVEL value.
- [/code]
- ===Mods:Modifiers|:- Expansion Modifiers for replaceable parameters===
- [code]Modifiers
- The following modifiers works in FOR command parameters (letters) and
- Batch file parameters (digits); the modifiers may not be used with %*
- Letter (%A..%Z) and digit (%0..%9) Parameters are represented below by: %P
- %P Expands P
- %~P Expands P removing any surrounding quotes (").
- %~fP Expands P to a Fully qualified path name.
- %~dP Expands P to a Drive letter only (including the colon).
- %~pP Expands P to a Path only (that ends in backslash).
- %~nP Expands P to a file Name only.
- %~xP Expands P to a file eXtension only (including the dot).
- %~sP Expands P using Short 8.3 names.
- %~aP Expands P to file Attributes.
- %~tP Expands P to date/Time of file.
- %~zP Expands P to siZe of file.
- %~$var:P Special case: Searches current P file name in the directories listed
- in Var Batch variable and expands the parameter to the fully qualified
- path name of the first one found, or an empty string if not found.
- The directories must be separated by semicolons, like in PATH variable.
- Several modifiers may be combined in the same parameter: %~ntzP
- For example, the following command take the path of the running Batch file:
- SET MYPATH=%~p0
- [/code]
- ===Error:Errorlevel|:- Description of the Errorlevel value===
- [code]Errorlevel
- When most commands and programs ends they return an exit code, called
- ERRORLEVEL, that is a 32-bits signed integer value. The MS-DOS standard
- specify that a program return an ERRORLEVEL of zero if successfully ends,
- but the value is greater than zero if the program ends because an error.
- The following DOS commands, among others, return useful ERRORLEVEL values:
- TYPE return 0 if the file was typed, return 1 if File not found.
- COPY return 0 if file(s) was copied, return 1 if File not found.
- FIND return 0 if the string was found, return 1 otherwise (also FINDSTR).
- FC return 0 if both files matched, return 1 otherwise.
- CD return 0 if directory was changed, return 1 if Dir not exists.
- VOL return 0 if volume was displayed, return 1 if invalid Drive.
- TIME return 0 if given time is correct, return 1 if invalid Time (also DATE).
- SET /P return 1 if line read is empty, otherwise NOT change ERRORLEVEL.
- SET /A return not zero if expression is wrong, otherwise NOT change ERRORLEVEL.
- VERIFY return 0 if parameter is ON or OFF, return 1 otherwise.
- VER always return 0.
- The following internal DOS commands, among others, does not modify the
- current ERRORLEVEL value: CLS, REM, ECHO, PAUSE, SHIFT, GOTO, SET var=value
- All .COM and .EXE executable files modify the ERRORLEVEL when they ends.
- To check the ERRORLEVEL use "IF ERRORLEVEL Number Command" that execute the
- Command if the ERRORLEVEL value is greater than or equal to the given Number;
- to test for several values, place the IF's in descending ERRORLEVEL order:
- FOR /L %%I IN (6,-1,1) DO IF ERRORLEVEL %%I GOTO ERROR%%I
- A Batch file may directly get the ERRORLEVEL value via the %ERRORLEVEL%
- dynamic variable, for example: IF %ERRORLEVEL% EQU 3 GOTO ERROR3
- A Batch file may return an useful ERRORLEVEL value via "EXIT /B exitCode" command.
- This is the one-line SETERRORLEVEL.BAT file that allows to set the ERRORLEVEL
- to any value given in its parameter: @EXIT /B %1
- For example: CALL SETERRORLEVEL 123
- [/code]
- ===Commands|:- Commands used in Batch files===
- REM ECHO PAUSE CHOICE SHIFT GOTO IF FOR SET SETLOCAL ENDLOCAL CALL CMD EXIT
- ==Various|:- Commands that not fit in previous subdivisions==
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement