2017-01-29 2 views
0

나는 다음 .txt 파일이 있습니다파일을 줄을 읽고 변수 배치 파일에 저장

Marco 
Paolo 
Antonio 

내가 줄 단위로 읽고 싶은, 각 라인 I 할당 할을 .txt 행 값을 변수에 추가하십시오. 가정하면 내 변수가 name이며, 흐름은 다음과 같습니다

  • name로 일부 작업을 수행 파일에서
  • 지정 name = "마르코"
  • 을 첫 번째 줄을 읽어 보자는 파일에서 두 번째 줄을 읽어
  • set first= %name%
  • 할당 name = "파올로" 어떻게해야합니까?
+4

표시/대 '의 출력을 판독?'(은'대/F 찾는다 'setlocal /?'(지연된 확장에 대해 읽음) –

+4

명령 출력에서 ​​변수를 설정할 때 [Windows 일괄 처리 도움말]이 중복 될 수 있습니다 (http://stackoverflow.com)./questions/1746475/windows-batch-help-in-setting-a-variable에서 명령 출력). 조이의 대답을 읽어보십시오. – JosefZ

답변

0

이 배열로 파일을 읽어와 변수로 각 행을 지정하고 그들에게 명령 행에서

@echo off 
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]!" 
) 
pause>nul 
Exit 
::*************************************************** 
:Error 
cls & Color 4C 
echo(
echo The file "%File2Read%" dos not exist ! 
Pause>nul 
exit /b 
::*************************************************** 
+0

배열 없이도 가능합니까? –

+0

@ FazleRabbi 왜이 솔루션이 효과가 없습니까? 당신의 목표는 무엇입니까? 더 많은 목적을 설명하십시오! 또는 질문을 편집하고 전체 코드를 게시하십시오! – Hackoo

관련 문제