bat批处理命令语法总结

::1. 关闭回显表示不显示命令本身只显示输出部分
@echo off


::2. 设置变量注意等号间不能有空格
set num=10

::3. 使用变量
echo %num%

::4. call 调用另一个批处理文件 与 start的区别是 执行完另一个批处理文件后可以继续执行下面的代码
call test1.bat

::5. 按任意键继续
pause

::6. rem 注释和 :: 都表示注释

::7. 命令的执行结果输出到文件 >覆盖 >>追加
dir D:\bat\ >>D:\bat\b.txt

::8. 接收执行bat命令的参数 %1,%2%3
echo %1

::9. 顺序显示文件里的内容
type D:\bat\test1.bat

::10. if / if not条件语句 if exist / if not exist 判断文件是否存在
if %1==a echo %1
if not %1==a echo param not a


if exist D:\bat\b.txt echo b.txt exist

::11. goto语句 

::12. 用户选项 一般配合goto语句使用
choice /C dme /M "defrag,mem,end"
echo %errorlevel%

::13. for循环
for %%I in (A,B,C) do echo %%I
for %%c in (D:\bat\*) do type %%c

::14. 获取时间
::年月日信息 2022/06/10
echo %date:~0,10%
::星期 周五
echo %date:~-3%
::时分14:55
echo %time:~0,5%
::时分秒 14:55:55
echo %time:~0,-3%
::年 2022
echo %Date:~0,4%
::月 06
echo %Date:~5,2%
::日 10
echo %Date:~8,2%

::时 15
echo %Time:~0,2%
::分 03
echo %Time:~3,2%
::秒 05
echo %Time:~6,2%

 

posted @ 2022-06-10 10:58  龍飛鳯舞  阅读(343)  评论(0编辑  收藏  举报