2014-11-21 2 views
0

for 문에서 토큰 값을 일괄 처리 스크립트의 변수로 설정 한 다음 스크립트에 필요한 모든 작업을 수행하는 방법에 대해 궁금합니다.토큰 값을 변수로 설정

Myconfigfile.config 라인 아래에있다 : 그래서

C:\logs|logfolder1|*.log|30 
C:\logs|logfolder12|*.log|30 

나는이 라인이 :

for /F "delims=| tokens=*" %%A in (Myconfigfile.config) do echo %%A 

내가 무엇을

location="tokens=1" 
subfolder="tokens=2" 
pattern="tokens=3" 
range="tokens=4" 

그런

echo the location is %location% 
echo the subfolder is %subfolder% 
echo the pattern is %pattern% 
echo the range is %range% 

분명히 나는 ​​4 문장으로 그렇게 할 수는 있지만 더 효과적인 방법이 있다고 의심된다.

답변

1
setlocal enableDelayedExpansion 
    for /F "delims=| tokens=1-4" %%A in (Myconfigfile.config) do (
    set "location=%%A" 
    set "subfolder=%%B" 
    set "pattern=%%C" 
    set "range=%%D" 

     echo the location is !location! 
     echo the subfolder is !subfolder! 
     echo the pattern is !pattern! 
     echo the range is !range! 
    ) 
endlocal 
+0

매력처럼 일했습니다! 고마워요. – Tee

0

이 안된 :

@echo off 
setlocal EnableDelayedExpansion 

rem Define the *names* of each one of the desired tokens: 
rem (this is the only line that require changes) 
set namesOfTokens=location subfolder pattern range 


rem Assemble the correct "tokens=..." and set commands 
set "tokens= ABCDEFGHIJKLMNOPQRSTUVWXYZ" 
set "setCommands=" 
set i=0 
for %%a in (%namesOfTokens%) do (
    set /A i+=1 
    for %%i in (!i!) do set setCommands=!setCommands! set "%%a=%%%%!tokens:~%%i,1!" ^& 
) 

rem DO IT! 
for /F "delims=| tokens=1-%i%" %%A in (Myconfigfile.config) do %setCommands:~0,-1% 

echo the location is %location% 
echo the subfolder is %subfolder% 
echo the pattern is %pattern% 
echo the range is %range% 

그것은 명령에 대한 긴에서 마지막 문자는 "&"문자 수 매우 중요 입니다. 결과를보고하십시오 ...

+0

덕분에, npocmaka는 이미 그것을 찍었습니다. :) – Tee

+0

실례합니다. 내 대답은 npocmaka의 답변과 완전히 다르다. 내 방법은 단 하나의 라인을 수정하는 임의의 수의 토큰을 삽입/재정렬/삭제할 수 있습니다. 'for /?'를 입력하면 다른 정보를 얻을 수 있습니다 (내가 이미 잘못 알고있는 것으로 가정합니다 ...) – Aacini

관련 문제