我正在寻找编写一个 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
::-------------------------------------------