CHOICE /C:ABC Press A, B, or C:
IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 ECHO You pressed "A"
IF ERRORLEVEL 3 IF NOT ERRORLEVEL 4 ECHO You pressed "C"
IF ERRORLEVEL 2 IF NOT ERRORLEVEL 3 ECHO You pressed "B"
Note: that the order of IF commands is not important.
To increase the number of lines displayed on the screen from 25
(default) to 50:
MODE CON Lines=50
MODE CON [RATE=r DELAY=d]
r The rate at which a character is repeated. r can be from 1 to 32
(equivalent to 2 to 30 characters per second) with a default of 21.
d The delay before characters are repeated. d can be 1 to 4
(equivalent to 0.25 to 1 second respectively) with a default of 2
(0.5 seconds).
MODE [display][,n]
display The type of display adaptor - one of the following:
BW40 A colour adaptor with colour turned off and 40 characters/line.
BW80 A colour adaptor with colour turned off and 80 characters/line.
CO40 A colour adaptor with colour turned on and 40 characters/line.
CO80 A colour adaptor with colour turned on and 80 characters/line.
MONO A monochrome adaptor (with 80 characters per line).
n The number of lines displayed on the screen. n can be 25, 43, or 50.
Default: 25
DOS Macros:
Count number of directories in folder:
dir/ad|find/v "."|find/i/c "<dir>"
Count number of directories in tree:
dir/ad/s|find/v "."|find/i/c "<dir>"
List directories in tree:
dir/ad/s|find/v "."|find/i "<dir>"
List files with 'archive' artribute:
dir/aa :/h - hidden /s - system /r - read-only
Reset 'archive' bit on all files (recursive):
attrib -a *.* /s
Special Usage:
Get input from console:
c:\>copy con filename.ext
when finished: [Ctrl-Z] + [Enter]
Redirect STDOUT:
The character '>' is STDOUT -> STDIN.
The character '<' is STDIN -> STDOUT.
The character '>' will destroy all data in the file it redirects to.
The character '>>' will append the data to the file it redirects to.
c:\>ECHO Hello > filename.ext
c:\>ECHO ATDT > COM1
c:\>TYPE filename.txt > PRN
STDIN:
When using the format command, it will prompt you to insert
a disk, and hit Enter. After format is complete, you will
be asked if you would like to format another disk. Normally,
you choose N for no. If you did not specify a volume label,
you will also be prompted for a volume label. To automate
this process, use the command line below. The X is a STDIN
redirect to the format command. The contents of the X file
is a line break, an N, and then two more line breaks. This
will make the format another disk prompt default to NO.
FORMAT A: /Q /S /V:VOLUMELABEL < CMDFILE.TXT
PIPES:
A pipe is when you redirect an program's STDOUT to another
program's STDIN. The TYPE command concatonates text to the
console, the STDOUT of TYPE is the console (monitor).
In the below example, we pipe a long file into the MORE
program so that we can see one screenful of text at a time.
c:\>TYPE longfile.txt | MORE
c:\>TYPE thisfile.txt | FIND/i "echo this line if it exists"
c:\>ECHO. | DATE | FIND/i "date is"
c:\>ECHO. | TIME | FIND/i "time is"
FOR loops:
FOR %i IN (set) DO COMMAND arguments
FOR %i IN (*.exe) DO ECHO %i
same as c:\>dir *.exe
FOR %i IN (*.txt) DO TYPE %i
IF statements:
IF [NOT] EXIST filename.ext COMMAND arguments [or GOTO]
IF EXIST filename.ext ECHO This file exists!
IF NOT EXIST filename.exe ECHO This file does not exist!
IF ERRORLEVEL 1 COMMAND /switches arguments
IF NOT ERRORLEVEL 1 COMMAND /switches arguments
IF %PATH%==C:\WINDOWS ECHO Your path is set to c:\windows
IF '%1==' GOTO END
%1 is the first argument to a batch file.
The above command reads 'if no command line argument, go to end'.
IF '%1==/h' GOTO HELP
ERRORLEVEL comparison:
The ERRORLEVEL variable holds a programs return state.
In C++, int main() usually returns 0 for null, or 1 as flag.
CHOICE.COM arguments
IF ERRORLEVEL 0 GOTO CHOICEY
IF ERRORLEVEL 1 GOTO CHOICEN
FC file1.txt file2.txt | FIND /i "FC: no differences encountered" > NUL
IF ERRORLEVEL==2 GOTO _ERR
IF ERRORLEVEL==1 GOTO _DIF
GOTO _OK
CHOICE.COM:
ECHO Press [e] to edit.
ECHO Press [x] to exit.
CHOICE.COM /N /C:ex /T:x,8 > NUL
IF ERRORLEVEL 2 GOTO EXIT
IF ERRORLEVEL 1 GOTO EDIT
> NUL ECHO REM|CHOICE.COM /N /C:n /T:n,8
This is an example of a DOS timer. sleep in unix.
ECHOing REM into CHOICE w/pipe will disable the keyboard.
CD command:
CD stands for Change Directory aka: (CHDIR)
c:\dir1\dir2\>CD \
change directory to root or c:\
c:\dir1\dir2\>CD ..
change directory back 1 or c:\dir1
c:\dir1\dir2\>CD ..\..
change directory back 2 or c:\ (same as cd \ in this case)
c:\dir1\dir2\>\app.exe
runs an application named app.exe located in root directory.
c:\dir1\dir2\>..\app.exe
runs an application named app.exe located in dir1 directory.
Networking:
NET CONFIG|FIND/i "user name"
gives the username currently logged in under.
NET TIME \\palgte|FIND/i "time at"
gives the official time of the \\server.
ECHO Y|NET TIME /SET \\palgte
sets the workstation to the official network time.
Changing a File's Date and Time Stamp:
COPY filename /B + ,, /Y
You don't need the close double quote when entering long folder names in DOS.
CD "\program files
works as well as
CD "\program files"
If you want to see what error codes are being generated by DOS programs,
start your DOS session with the addition of a /Z parameter to the COMMAND.COM program.