*********************************************************************************
Test1
@echo off
echo Test1: pass parameters to the t1.bat batch file
call t1.bat "hello" "haha"
t1.bat
echo %1 (Print: "hello")
echo %2 (Print: "haha")
echo %0 (Print: t1.bat)
echo %19 (Print: "hello"9)
echo %29 (Print: "hello"9)
echo %09 (Print: "hello"9)
pause
************************
Test2
@echo off
echo Test2: display help information
help type
************************
Test3
@echo off
echo Test3: Type some commands into a specific "setupreg.reg" format file
echo hello world
echo Windows Registry Editor Version 5.00 > C:\Users\M3C\Desktop\BAT_test\setupreg.reg
echo "SourcePath"="D:\\Win2003\\">> C:\Users\M3C\Desktop\BAT_test\setupreg.reg
echo Test:append content to Test.txt > C:\Users\M3C\Desktop\BAT_test\t3.txt
echo append content to test.txt again >> C:\Users\M3C\Desktop\BAT_test\t3.txt
t3.txt
Test:append content to Test.txt
append content to test.txt again
************************
Test4
@echo off
echo Test4: Rem(::) command
echo Here is the descriotion.
Rem echo Here is the description.
@Rem Here is the descriptiom.
:: echo Here is the description.
************************
Test5
@echo off
echo Test5: Goto (Goto label ":") and pause command
:begin
copy C:\Users\M3C\Desktop\BAT_test\Test5\t5.txt C:\Users\M3C\Desktop\BAT_test
echo Here Test5 file t5.txt will be copied from folder Test to the current (BAT_test) folder.
pause
goto begin
************************
Test6
@echo off
echo Test6: Call command (call [[Drive:][Path] FileName [BatchParameters]] [:label [arguments]] ) (Call another batch file)
echo Test6-1: Automatically generate a test6.bat file and add "%%1" "%%2" "%%3" to it, and then pass parameters to the t6.bat file.
echo echo %%1 > C:\Users\M3C\Desktop\BAT_test\t6.bat
echo echo %%2 >> C:\Users\M3C\Desktop\BAT_test\t6.bat
echo echo %%3 >> C:\Users\M3C\Desktop\BAT_test\t6.bat
echo pause >> C:\Users\M3C\Desktop\BAT_test\t6.bat
call test6.bat "T6" "TT6" "TTT6"
echo Test6-2: Call t6.bat in the specified directory and enter three arguments to it.
call C:\Users\M3C\Desktop\BAT_test\Test6\t6-2.bat "T62" "T623" "T6234"
call "%cd%\Test6\t6-2.bat" T62 T623 T6234
call="%cd%\Test6\t6-2.bat" T66 T666 T6666
echo Test6-3: Call t6.bat in the directory and enter three arguments to it.
call t6.bat "arg1" "arg2" "arg3"
t6-2.bat
echo %1
echo %2
echo %3
pause
************************
Test7
@echo off
echo Test7:Start command (Common Parameters of instrusion : MIN/SEPARATE/HIGH/REALTIME/WAIT)
echo Test7-1:Call t7.bat and enter 2 arguments to it, and minimize or maximize the window.
echo Notes: After "WAIT" is added, the instruction is execulted before the next one is executed.
start /MIN t7.bat arg1 arg2
start /WAIT /MAX t7_1.bat arg1 arg2
echo Test7-2:Call destination patch file, file path name empty format to double quotes.
start "" "C:\Users\M3C\Desktop\BAT_test\Test 7\Test7.txt"
start C:\Users\M3C\Desktop\BAT_test\"Test 7"\Test7_1.txt
start "" "%cd%\Test 7\Test7_2.txt"
start %cd%\"Test 7"\Test7_3.txt
t7.bat
echo %%If "exit" is added,the windows will close automatically after running.
echo %1
echo %2
pause
exit
t7_1.bat
echo %0
echo %1
echo %2
pause
exit
Test7.txt
Congratulation on learning the instruction!
start "" "C:\Users\M3C\Desktop\BAT_test\Test 7\Test7.txt"
Test7_1.txt
Congratulation on learning the instruction!
start C:\Users\M3C\Desktop\BAT_test\"Test 7"\Test7_1.txt
Test7_2.txt
Congratulation on learning the instruction!
start "" "%cd%\Test 7\Test7_2.txt"
Test7_3.txt
Congratulation on learning the instruction!
start %cd%\"Test 7"\Test7_2.txt
************************
Test8
@echo off
echo Test8-1: Enter two elements and use the if statement, If [not] "parameter" == "character string"
set /p var1=Please enter an element1:
set /p var2=Please enter an element2:
if "%var1%" == "%var2%" echo The two elements of the input are identical
pause
set /p var3=Please enter an element3:
set /p var4=Please enter an element4:
if "%var3%" == "%var4%" (echo The two elements of the input are identical) else echo The two elements of the input are different
pause
set /p var3=Please enter an element5:
set /p var4=Please enter an element6:
if "%var3%" == "%var4%" (echo The two elements of the input are identical) else (echo The two elements of the input are different)
pause
rem if not "1" == "1" (echo true) else echo fail
***
echo Test8-2: if [not] exist [path\]FileName Command to be executed
echo Test for ifexist > "%cd%\ifexist.sys"
if exist "%cd%\ifexist.sys" type ifexist.sys
if exist "%cd%\ifexist.sys" echo True
if not exist "%cd%\ifexist.sys" (echo Fail) else echo Pass
rem if exist "%cd%\ifexist.sys" edit ifexist.sys
pause
***
echo Test8-3: if [not] errorlevel <number> Command to be executed
xCOPY %cd%\Test8-3\test8-3.txt %~dp0\
if errorlevel 1 (echo copy file fail) else if errorlevel 0 echo copy file pass.
pause
del "%cd%\test8-3.txt"
rem if errorleve 1 (echo Succeeded in deleting files) else if errorlevel 0 echo Failed to delete a file.
pause
***
echo Test8-4: if condition (An order executed on establishment) else (An order not to be executed immediately)
if 1==0 (echo Fale) else if 1==0 (echo Fale) else (echo 1=1)
if 1==0 (echo Fale) else if 1==0 (echo true) ^
else (echo 1=1)
pause
***
echo Test8-5: Comparison operator
echo EQU -Equal to ("==" is commonly used)
echo NEQ -Not Equal to (not "!=", There is use "if not 1==1" here)
echo LSS -Less than
echo LEQ -Less than or equal
echo GTR -greater than
echo GEQ -greater than or equal
pause
***
test8-3.txt
test for if errorlevel
************************
Test9-1
@echo off
echo Test9-1: Choice /c:parameter (The element returns values 1,2,3...), for example: Choice defrag/mem/end or Yes/No.
Choice /c:dme
Choice /c:YN
Pause
***
Test9-2:
@echo off
echo Test9-2: "Choice /c:parameter " and "If errorlevel" are used together.
Choice /c:dme
if errorlevel 3 goto e
if errorlevel 2 goto m
if errorlevel 1 goto d
pause
exit
:d
echo Your choice is 'defrag'
pause
exit
:m
echo your choice is 'mem'
pause
exit
:e
echo your choice is 'end'
pause
exit
************************
Test10
@echo off
echo Test10: For {%%variable} in (set) do command [command-parameters]
echo For /parameter %%variable in (set) do (command)
echo notes: "For" has D/L/R/F parameter
echo notes: Bat file in the variable before adding "%%" to the normal operation
***
echo Test10-1: For %%variable in (set) Do command [command-parameters]
echo notes: The file name containing the extension name ".XXX" is displayed in the current directory
for %%c in (*.bat*) do (echo %%c)
rem for %%t in (*.txt*) do (echo %%t)
rem for %%s in (*.reg*) do (start %%s)
************************
Test10-2.1
@echo off
echo Test10: For {%%variable} in (set) do command [command-parameters]
echo For /parameter %%variable in (set) do (command)
echo notes: "For" has D/L/R/F parameter
echo notes: Bat file in the variable before adding "%%" to the normal operation
******
echo Test10-2.2: For /d %variable in (set) Do command [command-parameters]
echo notes: Displays the names of directories containing "X" or "Y" in the current directory
for /d %%c in (*T*) do (echo %%c)
rem for /d %%c in (*c*) do (echo %%c)
rem for /d %%c in (*T*) do (cd "%%c)
rem for /d %%c in (*T*) do (start %cd%\%%c)
************************
Test10-3.1
@echo off
echo Test10: For {%%variable} in (set) do command [command-parameters]
echo For /parameter %%variable in (set) do (command)
echo notes: "For" has D/L/R/F parameter
echo notes: Bat file in the variable before adding "%%" to the normal operation
******
Test10-3.2
@echo off
echo Test10-3.2: For /R [[drive:]path] %variable in (set) Do command [command-parameters]
echo notes: Displays the file name containing the extension name ".XXX" in the current or specified directory
for /r C:\Users\M7O\Desktop\BAT_test\Test10-3 %%t in (*.txt*) do (echo %%t)
rem for /r C:\Users\M7O\Desktop\BAT_test\Test10-3 %%t in (*.txt) do (echo %%t)
rem for /r %%t in (.) do (echo %%t)
rem for /r %%t in (*.txt) do (echo %%t)
rem for /r C:\Users\M7O\Desktop\BAT_test %%t in (.) do (echo %%t)
rem for /r C:\Users\M7O\Desktop\BAT_test\Test10-3 %%t in (.) do (echo %%t)
************************
Test10-4.1
@echo off
echo Test10: For {%%variable} in (set) do command [command-parameters]
echo For /parameter %%variable in (set) do (command)
echo notes: "For" has D/L/R/F parameter
echo notes: Bat file in the variable before adding "%%" to the normal operation
******
Test10-4.2
@echo off
echo Test10-4.2: For /L %variable in (start,step,end) Do command [command-parameters]
echo notes: The following command produces the sequence 1,2,3,4,5
************************
Test10-5
@echo off
echo Test10: For {%%variable} in (set) do command [command-parameters]
echo For /parameter %%variable in (set) do (command)
echo notes: "For" has D/L/R/F parameter
echo notes: Bat file in the variable before adding "%%" to the normal operation
******
echo Test10-5.1: For /F ["options"] %variable in (file-set) Do command [command-parameters]
echo Test10-5.2: For /F ["options"] %variable in ("string") Do command [command-parameters]
echo Test10-5.3: For /F ["options"] %variable in (command) Do command [command-parameters]
echo notes: "options" has "eol/skip/delims/tokens/m-n/usebackq" paramerter
echo Notes: The following two command show the current year month day and actual
************************************************************************************************
:: ******************************************************************************
:: %cd% and %~dp0
:: 这两个变量都是用来表示当前目录,可使用时却大不相同,记录下
:: %cd%代表的是执行文件的当前目录,强调bat是在哪里启动的 ->盘符展开后是可改变的
:: %~dp0代表的是bat文件所在的文件目录,强调bat的文件位置 ->盘符展开后是不可改变的
::
:: 验证步骤如下:
:: 新建一个文件夹 cddp0,里面创建一份Test.bat文件
:: 然后在BAT_test文件夹创建一份 cddp0.bat 文件
:: 直接双击运行Test.bat文件
:: 直接双击运行 cddp0.bat 文件
:: 比较区别
:: ******************************************************************************
@echo off
start "" "%cd%\cddp0\Test.bat"
:: pause
:: start "" "%~dp0\cddp0\Test.bat"
************************************************************************************************
Notes:
1.https://author.baidu.com/home?from=bjh_article&app_id=1669257974205542
2.You can practice in conjunction with "DOS_BATCH_练习参考.7z" resource attachments.