
私は、.txt (ファイル名) を引数として受け取り、ファイルを 1 行ずつ読み取り、各行をコマンドに渡す Windows コマンド プロンプト用のスクリプトを作成したいと考えています。私のスクリプトは次のようになります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
または参照ファイルを1行ずつ読み取り、変数バッチファイルに保存します。
次のようなことができます:
@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
::-------------------------------------------