新聞速報

        

2015年1月8日 星期四

批次檔 字串變數 取代

 echo 和 @

回顯命令

@                     #關閉單行回顯

echo off              #從下一行開始關閉回顯

@echo off           #從本行開始關閉回顯。一般批次處理第一行都是這個

echo on               #從下一行開始打開回顯

echo                  #顯示當前是 echo off 狀態還是 echo on 狀態 

echo.                 #輸出一個” 回車換行”,空白行
                          #(同 echo, echo; echo+ echo [ echo] echo/echo)

                       

 

 errorlevel

echo %errorlevel%

每個命令運行結束,可以用這個命令列格式查看返回碼

預設值為 0,一般命令執行出錯會設 errorlevel 為 1

 ==== 注 ===========
返回值從大到小的順序排列不是必須的,而只是執行命令為 goto 時的習慣用法,當使用 set 作為執行命令時,通常會從小到大順序排列,比如需將返回碼置入環境變數,就需使用以下的順序形式:

if errorlevel 1 set el=1
if errorlevel 2 set el=2
if errorlevel 3 set el=3
if errorlevel 4 set el=4
if errorlevel 5 set el=5
...

當然,也可以使用以下迴圈來替代,原理是一致的:
for %%e in (1 2 3 4 5 6 7 8...) do if errorlevel %%e set el=%%e

 

 

%~dp0 →批次檔所在路徑
%cd% →目前工作路徑

%~dp0 批次檔所在路徑,例如 C:\Program Files\Mozilla Firefox\ 或 UNC 路徑,
             例如 \\Server\Share\Program Files\Mozilla Firefox\
%~d0 批次檔所在磁碟代號,例如 C: 或 UNC 路徑的雙反斜線 \\
%~p0 批次檔所在路徑,不含磁碟代號,例如 \Program Files\Mozilla Firefox\ 或
            開頭不帶雙反斜線的 UNC 路徑,例如 Server\Share\Program Files\Mozilla Firefox\
%cd% 目前工作路徑,非根路徑時後面不帶反斜線,例如 C:\Program Files\Mozilla Firefox

得知在參數%n(%0, %1, ... %9)之間加了波浪符號(~)與相關的修飾字詞後, 就會有擴充的支援。


  1. %0:第 0 個參數, 表示批次檔本身。
  2. %~d0:Dirver, 批次檔存在的磁碟機代號。
  3. %~p0:Path, 批次檔的路徑位置(不包含磁碟機代號)。
  4. %~n0:批次檔的檔案名稱。
  5. %~x0:批次檔的副檔名。
  6. 所有的修飾字詞可以混搭使用, 所以%~dp0即是批次檔所在的完整路徑。
  7. 修飾字詞還有~f, ~a, ...等, 請參考程式說明頁面。

        %~1         - 展開 %1 且移除包圍的引號 (")
        %~f1        - 展開 %1 為一個完全符合的路徑名稱
        %~d1        - 只展開 %1 為磁碟機代號
        %~p1        - 只展開 %1 為路徑
        %~n1        - 只展開 %1 為檔名
        %~x1        - 只展開 %1 為副檔名
        %~s1        - 展開的路徑只包含短檔名
        %~a1        - 展開 %1 為檔案的檔案屬性
        %~t1        - 展開 %1 為檔案的日期/時間
        %~z1        - 展開 %1 為檔案的長度
        %~$PATH:1   - 搜尋所有列在 PATH 環境變數內的目錄且展開 %1 為第一個找到的完全符合檔名。

        %~dp1    - 將 %1 擴充為磁碟機代號及路徑。

        %~nx1    - 將 %1 擴充為檔名和副檔名。

        %~dp$PATH:1      -在 PATH 環境變數所列的目錄中搜尋 %1,並將其擴充為第一個找到的磁碟機代號和路徑。

        %~ftza1    - 將 %1 擴充與 dir 類似的輸出行。
     

如果沒有定義環境變數名稱或是搜尋找不到檔案,則這個修飾元會展開成空字串。

rem 變量延遲的啟動語句是並且變量要用一對嘆號「 !! 」括起來(注意要用英文的嘆號),否則就沒有變量延遲的效果
Setlocal Enabledelayedexpansion

rem 資料夾 不刪除
set NoDelPath=%cd%\RTXPOS\BIN

FOR /F %%a IN ('DIR BIN /AD /S /B') DO (

set myPath=%%a
   
   echo !myPath!
   echo !NoDelPath!

   if /I "!myPath!" == "%NoDelPath%"  (
      echo 不刪除

   ) else (
      echo RMDIR/S/Q  !myPath! )   

)




1. 取代字串 "work" with "play"
@echo off
set string=This is my string to work with.
set string=%string:work=play%
echo %string%
Output = This is my string to play with.



2. 移除字串 "to work with."
@echo off
set string=This is my string to work with.
set string=%string:to work with.=%
echo %string%
Output = This is my string


3. 將所有空格 換成 底線
@echo off
set string=This is my string to work with.
set string=%string: =_%
echo %string%
Output = This_is_my_string_to_work_with.


4. 移除所有空格
@echo off
set string=This is my string to work with.
set string=%string: =%
echo %string%
Output = Thisismystringtoworkwith.


5. 取代字串,取代:來自其他變數   換成: 來自其他變數
@echo off
setlocal enabledelayedexpansion
set string=This is my string to work with.
set find=my string
set replace=your replacement
call set string=%%string:!find!=!replace!%%
echo %string%
Output = This is your replacement to work with.


6. Find rest of string after one looked for

This looks for the string "my string " and the * before the string to look for means to match any other characters. This whole string is removed leaving the rest of the line.
@echo off
setlocal enabledelayedexpansion
set string=This is my string to work with.
set "find=*my string "
call set string=%%string:!find!=%%
echo %string%
Output = to work with.


7. Find the line up to the string searched for

This looks for the string "*my string " and gets the rest of the line into the variable %delete%. It then substitutes the string from %delete% with nothing.
@echo off
setlocal enabledelayedexpansion
set string=This is my string to work with.
set "find=*my string "
call set delete=%%string:!find!=%%
call set string=%%string:!delete!=%%
echo %string%
Output = This is my string

This only works of course if the strings are not going to be found elsewhere in the line, e.g. with the string below removes both "to work with" strings

set string=This to work with is my string to work with

Output = This is my string

8.  取得批次檔執行路徑
SET mypath=%~dp0
echo %mypath:~0,-1%

 

 

批處理中的 &、&&、|、||、>、>> 符號


順序執行多條命令,而不管命令是否執行成功
例:copy nul 5.txt & echo 666 >>5.txt & more 5.txt
創建 5.txt 文檔,向 5.txt 文檔中寫入內容 “666”,輸出 5.txt 的內容。

&& 順序執行多條命令,當碰到執行出錯的命令後將不執行後面的命令

|| 順序執行多條命令,當碰到執行正確的命令後將不執行後面的命令(即:只有前面命令執行錯誤時才執行後面命令)

管道命令前一個命令的執行結果輸出到後一個命令如:help|more

> 清除文件中原有的內容後再寫入

>> 追加內容到文件末尾,而不會清除原有的內容主要將本來顯示在屏幕上的內容輸出到指定文件中指定文件如果不存在,則自動生成該文件

命令列參數

假設在命令列鍵入了下列指令

test.cmd c:\windows\notepad.exe c:\windows\write.exe

此時批次檔內部自動將命令列上的參數視為特別的變數如下表

%0 (命令)$1 (參數 1)%2 (參數 2)
test.cmdc:\windows\notepad.exec:\windows\write.exe

請以 Notepad++ 編輯 test.cmd 並鍵入下面的指令,存檔後在命令列上輸入

test.cmd c:\windows\notepad.exe c:\windows\write.exe

@ech off
echo command = %0
echo argument1 = %1
echo argument2 = %2


 

 

沒有留言:

張貼留言