猴子的記憶庫

因為常常忘記之前的安装和配置過程,所以建立本記憶庫

0%

Batch同時讀取多個輸入參數

Batch同時讀取多個輸入參數

因為想用bat同時處理多個檔案,想寫一個可以直接拉多個檔案放到bat檔就可以讀取檔案路徑的程式。

cmd_arg.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@echo off && setlocal EnableDelayedExpansion
:: %*就是輸出從第一個參數開始的所有參數::
echo %*

:: 把參數用分成陣列 ::
for %%Z in (%*)do (
set "_arg_=%%Z" && set/a "_cnt+=1+0" && call set "_arg_[!_cnt!]=!_arg_!")
)

:: 單獨輸出參數 ::
for /l %%l in (1 1 !_cnt!)do (
echo/ The argument n:%%l is: !_arg_[%%l]!
)

PAUSE

如指令 arg.bat a b c d e

arg.bat 是批次檔本身,可以用指令 %0讀取

a 是第一個參數,用指令 %1讀取

e 是第五個參數,用指令 %5讀取等等…

%* 就是輸出從第一個參數開始的所有參數

如果直接拉多個檔案放到bat檔上,檔案的路徑就會用參數的方式加上指令。

arg.bat D:\Folder\text1.txt D:\Folder\text2.txt D:\Folder\text3.txt....

記得如果檔案有空白符號參數會自動加上” “

arg.bat "D:\Folder\text with space.txt" D:\Folder\text2.txt D:\Folder\text3.txt....