
我正在尋找為 Windows 命令提示字元編寫一個腳本,該腳本採用 .txt (檔案名稱)作為參數,逐行讀取文件,並將每一行傳遞給命令。如果我的FILE.txt
看起來像:
LINE1
LINE2
LINE3
.
.
.
它運行:
command_name argument --option LINE1
, 然後
command_name argument --option LINE2
,成功執行第一個指令後。和繼續使用不同的參數來執行相同的命令逐行挑選FILE.txt
。
這我想要實作的 Linux/UNIX 版本如下:
cat FILE.txt | xargs -L 1 echo --option | xargs -L 50 command_name argument
答案1
和電源外殼:
foreach($line in GC PathFolder\File.txt) {Write-Host command_name argument --option $Line}
和命令列: 使用FOR /f
For /f "delims=" %a in ('Type "File.txt"') do (echo command_name argument --option %a)
與一個批次檔:
@echo off
@For /f "delims=" %%a in ('Type "File.txt"') do ( echo command_name argument --option %%a)
pause
你可以這樣做:
@echo off
Color 0B & Title Read and parse each line of a text file as an argument to command prompt
set "File2Read=file.txt"
If Not Exist "%File2Read%" (Goto :Error)
rem This will read a file into an array of variables and populate it
setlocal EnableExtensions EnableDelayedExpansion
@For /f "delims=" %%a in ('Type "%File2Read%"') do (
set /a count+=1
set "Line[!count!]=%%a"
)
REM Display array elements
@For /L %%i in (1,1,%Count%) do (
echo "Var%%i" is assigned to ==^> "!Line[%%i]!"
echo command_name argument --option !Line[%%i]!
)
pause>nul
Exit
::-------------------------------------------
:Error
cls & Color 4C
echo(
echo The file "%File2Read%" dos not exist !
Pause>nul
exit /b
::-------------------------------------------