2014-12-22 5 views
4

입력 변수를 가져 와서 처리 할 다른 배치 파일로 보내는 배치 파일을 작성하려면 어떻게해야합니까?하나의 배치 파일에서 다른 배치 파일로 변수를 전달하는 방법은 무엇입니까?

배치 한

나는 여기에 내 문제가 배치 2에 변수를 전송하는 방법을 모르겠어요.

배치 2

if %variable%==1 goto Example 
goto :EOF 

:Example 
echo YaY 
+0

당신은 batch2에 인수로 batch1 변수를 전달할 수 있습니다, 또는 당신이 동시에 스크립트를 실행해야하는 경우 임시 파일에 변수를 저장할 수 있습니다. – SomethingDark

+0

둘 다 예를 들려 주시겠습니까? –

+1

batch2는 batch1의 하위 프로세스 또는 관련이없는 프로세스입니까? – Magoo

답변

4

아무 것도 할 필요가 없습니다. 배치 파일에 설정된 변수는 호출하는 배치 파일에 표시됩니다.

echo In test2.bat with x = %x%. 

출력

test2.bat

@echo off 
set x=7 
call test2.bat 
set x=3 
call test2.bat 
pause 

test1.bat 예

... test1.bat 실행됩니다.

In test2.bat with x = 7. 
In test2.bat with x = 3. 
Press any key to continue . . . 

11

당신은 batch2.bat에 인수로시키면서 batch1.bat 변수에 전달할 수 있습니다.

@echo off 

:: There should really be error checking here to ensure a 
:: valid string is passed, but this is just an example. 
set arg1=%~1 
set arg2=%~2 

echo Hello, %arg1%! My name is %arg2%. 

동시에 스크립트를 실행해야하는 경우, 임시 파일을 사용할 수 있습니다 arg_batch2.bat arg_batch1.bat

@echo off 
cls 

set file_var1=world 
set file_var2=%computername% 
call arg_batch2.bat %file_var1% %file_var2% 

:: Note that after batch2.bat runs, the flow returns here, but since there's 
:: nothing left to run, the code ends, giving the appearance of ending with 
:: batch2.bat being the end of the code. 

.

file_batch1.bat

@echo off 
set var=world 

:: Store the variable name and value in the form var=value 
:: > will overwrite any existing data in args.txt, use >> to add to the end 
echo var1=world>args.txt 
echo var2=%COMPUTERNAME%>>args.txt 

call file_batch2.bat 

@echo off 
cls 

:: Get the variable value from args.txt 
:: Again, there is ideally some error checking here, but this is an example 
:: Set no delimiters so that the entire line is processed at once 
for /f "delims=" %%A in (args.txt) do (
    set %%A 
) 

echo Hello, %var1%! My name is %var2%. 
+0

배치 2를 숨기고 계속 작동시키는 방법은 무엇입니까? –

+0

신경 쓰지 마, 알아 냈어 –

+1

고마워. – NetworkKingPin

0

file_batch2.bat 당신은 어떤이 코드가 수행하는 것입니다

variable goes here >> "second file goes here.bat" 

below-에서 해결책을 찾을 수 있습니다 그것은 variab을 쓴다. 파일이 존재하지 않으면 두 번째 파일에 추가합니다. 존재하지 않으면 새 파일을 만듭니다.

관련 문제