文章目录
Batch Commands
%0, %1, %2, %3, %4, %5, %6, %7, %8, %9 ... arguments to the batch file | %0 is the batch file itself %variablename% ... the name of the variable; the variable name is set by the SET variablename=string command Note the extra '%' at the end. ECHO [ON|OFF|message] "echo blank" will produce a blank line on screen in DOS2.1 "echo blank" will show ON/OFF on screen in DOS3.0 "echo ASCII-255" to get a blank line in DOS3.1 "echo." to get a blank line in DOS3.1 @echo off ... even the "echo off" will not be echoed (DOS 3.3) %@%echo off ... good for various versions of DOS (set @=@ for DOS 3.3 set @= for DOS 3.2) CALL batchfile set call=call for DOS 3.3 set call=%comspec% /C for DOS 3.2 %call% ... usage that is good for various version of DOS FOR %%variable IN (set) DO command {set is a list of filename; * or ? can be used.} Note that two %% are used in a batch file, because one % is striped off when given to the command line; only one % is needed when given directly from the command line.} GOTO label {%1 etc. can also be used for label; see example below} If [NOT] condition command |string1==string2 {upper & lower case letters matters} |EXIST [d:][path]filename {path in DOS3.0} | {path is not supported in DOS2.1} |ERRORLEVEL number {true if the previous program exit with number or higher; See ask} example of looping 7 times (i.e., 7 "-" signs) set count= :loop set count=%count%- command 1 command 2 if not "%count%"=="-------" %then% goto loop %else% set count= example of looping 27 times (i.e., 2 "+" signs and 7 "-" signs) set count1=| set count2= ... put two commands in one line with "|" redirection :loop set count1=%count1%- command 1 command 2 if not "%count2%%count1%"=="+++-------" %then% goto end %else% if not "%count1%"=="-------" %then% goto loop %else% set count2=%count2%+| set count1= goto loop SHIFT PAUSE [remark] REM [remark] Example: :label :comments .comments .comments %1 %2 Command line parameters may be displayed on comment lines. REM comments echo {bell} goto label Example: copy con: %1 {enter text to files within a batch environment} Example: REM CTRL-H CTRL-H CTRL-H comments {not to show "REM" in echo mode} Example: SET abc=\fortran\fortran.lib link %1,,,%abc% Example: SET mouse=1 {set the environment in one batch file} IF %mouse%=="1" copy abc.txt com1: Example: IF .%1==.abc dir {dots allow .BAT files to continue running when there is no parameter passed.} Example: IF dummy==dummy%1 GOTO label {test for nul parameter in a batch file} {good for terminating SHIFT} Example: IF ERRORLEVEL 1 IF x==%1 command 谀 IF ERRORLEVEL 1 IF EXIST filename command 滥{to nest IF statement} Example: ECHO CTRL-H {to output a blank line to the console} Example: ECHO A ... program1 ECHO B ... program2 ECHO Press the letter of your selection PAUSE > select.bat select Example: FOR %%f IN (*.*) DO program %%f {good for a program that cannot handle global chars * and ?} FOR %%f IN (*.*) DO type %%f FOR %%f IN (abc.hlp,command.hlp,batch.hlp) DO type %%f FOR %%f IN (*.g) DO ren %%f %%for {ren all .g files to .gor} FOR %%f IN (*.hlp) DO find "abc" %%f {find "abc" in all the .hlp files} Example: rem print a number of files with the corresponding directories & with formfeed echo off cls :loop echo ... PRINTING FILE %1 dir %1|find "-" > prn: echo > prn: {line feed} echo -------------------------------------------------------- > prn: echo > prn: {line feed} copy %1 prn: echo > prn: {form feed} shift {handle the next argument} if not dummy==dummy%1 goto loop Example: GOTO f%1 :fA echo the 1st argument was A GOTO end :fB echo the 1st argument was B GOTO end :fC echo the 1st argument was C :end See BAT for a set of enhanced batch file commands.