windows批处理教程
内容目录

批处理相关用法

设置注释

设置注释可以通过Rem和::完成,例如:

:: 
::  the below code is used to build project using cmake.
:: 

Rem This is for listing down all the files in the directory Program files 

重定向

和linux类似,可以通过>进行重定向

echo "sdfsdf" > sb.txt // 进行覆盖
echo "sdfsdf" >> sb.txt // 进行追加

管道使用

通过|来将一个命令的输出作为另一个命令的输入

dir | find ".txt"

多命令使用

通过&&和||来进行多命令。

sb && ipconfig // 当第一个命令失败,会短路,不会执行第二个命令
sb || ipconfig // 两个命令都会执行,尽管可能会失败。

给bat文件传递参数

sb.bat 1 2

在bat文件中,可以通过%1, %2 ... 来获取指定参数:

echo %1
echo %2

变量

设置变量

通过set来设置变量,例如:

set sb = message
echo %sb%

如果是数字:

set /A sb = 1

使用变量

通过%变量名%来使用。

变量运算

批处理常用命令

可以通过 command /? 来获取命令帮助

windows常用命令

设置命令行颜色

通过color设置命令行颜色,color /?获取帮助

设置命令行标题

title

日期时间命令

date显示日期
time显示时间

启动新命令行窗口

start

上一篇
下一篇