Batch - Windows Batch 常用命令


比較符號(不能用 < , >)

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

 

%* 命令

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

 

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

 

 

 

%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排第二所以是第二個參數)

 

%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

 

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM