
.txt(파일 이름)를 인수로 사용하고 파일을 한 줄씩 읽고 각 줄을 명령에 전달하는 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
또는 참조하십시오파일을 한 줄씩 읽어서 변수 배치 파일에 저장합니다.
다음과 같이 할 수 있습니다.
@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
::-------------------------------------------