Batch - Windows Batch 常用命令

回到顶部(go to top)

比较符号(不能用 < , >)

The reason operators like > are not used is because they have special meanings in shell scripts. The > is used to redirect output; < used to redirect input, etc.

Operator | Description
EQU      | equal to
NEQ      | not equal to
LSS      | less than
LEQ      | less than or equal to
GTR      | greater than
GEQ      | greater than or equal to

 

回到顶部(go to top)

%* 命令

Key point:

  • It means "all the parameters in the command line".
  • %* expands to all parameters from the command line, even after a SHIFT operation. Normally a SHIFT will move parameter %2 to %1%3 to %2, etc., and %1 is no longer available. But %* ignores any SHIFT, so the complete parameter list is always available. This can be both a blessing and a curse.

 

Usage:

  •  it's useful when you want to forward the command line from your batch file to another program

 

回到顶部(go to top)

Shift 命令

shift command shifts the position of arguments one to the left. Running shift once in a batch file will make "%1" value to be the second argument, "%2" becomes the third, and so on. It's useful for processing command line arguments in a loop in the batch file.

Attention:

  • Shift doesn't change the actual order, just the index/pointer into the arguments.
  • It doesn't affect %*

 

Command like:

c:/>batchName.bat a b c d

batName.bat content:

@echo off
echo %1
shift 
echo %1
shift
echo %1
echo %*

And you get this:

a
b
c
a b c d

 

 

 

回到顶部(go to top)

%0、%1等是表示什么

%0 指批处理本身。

%1 指批处理文件名后加的以空格分隔的字符串。

%2~%9类推

比如说

D盘根目录下有aa.txt,bb.txt,cc.txt三个文本和一个名batName.bat的批处理,批处理内容是

@echo off

start %1

start %3

 

打开CMD定位到D盘

    D:\>batName     aa.txt    bb.txt    cc.txt

对应:       %0            %1       %2        %3

 

就会打开aa.txt(它是第一个参数即%1)和cc.txt(它是第三个参数即%3),而不会打开bb.txt,因为批处理的命令里没有start %2(bb.txt排第二所以是第二个参数)

 

回到顶部(go to top)

%2 与 %~2 的区别

%2 substitutes in the second argument. %~2 substitutes the second argumenty but removes any quote marks:

C:\Temp>type t.cmd
@echo off
echo %%2 is: %2
echo %%~2 is: %~2

C:\Temp>t.cmd first second third
%2 is: second
%~2 is: second

C:\Temp>t.cmd first "second third"
%2 is: "second third"
%~2 is: second third

 

 

回到顶部(go to top)

echo off和echo on的作用

先写一个批处理文件,命令很简单,只是打印语句,如下:

复制代码
rem 我把"执行echo off""执行echo on"也打印出来了,对了,前面的 rem 相当于java注释中的//
echo 第一句
echo 第二句
echo 第三句
echo off
echo 执行echo off后
echo 第一句
echo 第二句
echo 第三句
echo on
echo 执行echo on后
echo 第一句
echo 第二句
echo 第三句
复制代码

然后是运行结果,图中有说明:

 

执行”echo off”,后面所执行的命令不会显示出来,只会显示结果.

但”echo off”本身的命令会显示出来怎么办,所以一般见到的echo off 前面都有”@”,”@”作用是此行所有的命令都不显示,只执行,和”echo off”比较相似,不过只对当前行起作用 


————————————————

版权声明:本文为CSDN博主「S_clifftop」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/S_clifftop/article/details/78632313


版权声明:本文为CSDN博主「rainbow702」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/rainbow702/article/details/50516739

posted on   frank_cui  阅读(1532)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

levels of contents
点击右上角即可分享
微信分享提示