@ECHO OFF
REM 判断文件是否存在
IF EXIST example.txt ECHO found it
REM 组合命令和js的短路径操作类似
DIR example.txt && ECHO found it too
REM 追加方式到处内容到文件
ECHO not bad day >> exmple.txt
REM 最简单的组合命令
ECHO hello & ECHO world!
REM 这是注释,为了让地球人看懂
ECHO comments example
REM |(前一个命令的输出 作为后一个命令的输入) more 一屏一屏的显示后面的内容 Enter 1行, 空格 1屏
:: help | more
:: more longArticle.txt
:: type longArticle.txt
REM || 短路径操作 参考js
DIR setup_*.exe || echo can see me
DIR foo.ttt & DIR foo.txt || ECHO can not see me 2
DIR foo.ttt && DIR foo.txt || ECHO can see me 2
REM 输入重定向 < >& <&
:: more < longArticle.txt
REM 输出重定向 > >>
:: echo has some good idea > haha.txt
:: echo come on >> haha.txt
REM call命令 从1个批处理调用另1个批处理 且接受入参
call hi.bat sindy
:: hi.bat
:: echo hello, %1
REM choice命令 提示用户输入1个字母进行选择 它的返回码为1234……
CHOICE /C abc /M apple,banana,coffee
if errorlevel 3 goto coffee
if errorlevel 2 goto banana
if errorlevel 1 goto apple
:apple
echo you like apple
goto end
:banana
echo you like banana
goto end
:apple
echo you like coffee
goto end
:end
echo goodbye
REM find命令 find string 注意这个string需要双引号
netstat -an > a.txt
type a.txt | find "5355" && echo yes,you found the string
REM if命令 判断入参
if "%1"=="" goto usage
if "%1"=="/?" goto usage
if "%1"=="help" goto usage
:: if not "%1"="" goto somejob
:usage
ECHO this is something about how to use bat
REM 判断文件是否存在
IF EXIST *.jpg DEL *.jpg
:: IF NOT EXIST *.jpg MKDIR pic
:: DOS程序在运行完后都有返回码),如果和定义的错误码符合(这里定义的错误码为1),则执行相应的操作(这里相应的操作为pause & edit %1.asm部分)
::masm %1.asm
::if not errorlevel 1 link %1.obj
::pause & edit %1.asm
:: ------------------------
::masm %1.asm
::if exist %1.obj link %1.obj
::else pause & edit %1.asm
REM IF [NOT] ERRORLEVEL number do command
REM IF [NOT] string1==string2 do command
REM IF [NOT] EXIST filename do command