Passing parameter from a bat to a bat (environment variable)

April 8, 2013 at 04:54:28
Specs: Windows 7
I need to pass some parameters from a bat, file1.bat to another bat, file2.bat. Problem is that @echo %2 inside file2.bat shows "" from prompt command, so the variable named secondVar defined inside file1.bat is not being send as it should be, while the first argument is always sending successfully.
Notice that the echo !secondVar! appears at the console from file1.bat. But It does not retrieve when calling file2.bat from file1.bat

I'm stuck here therefore i cannot go ahead. Please someone could tell me what is happening and give me something right. Thank you very much in advance!

file2.bat looks like:

@echo off
echo %2
Set arg1=%1
echo %arg1%
cscript process.vbs %1>result
more +1 %1|findstr /I /r /b /C:"^T.*=%2">viewout

file1.bat looks like:

@echo off & setLocal
SETLOCAL EnableDelayedExpansion
set secondVar=%secondVar%

for /f "tokens=*" %%a in (myFile) do (set secondVar="%%a"
echo !secondVar!
call file2.bat "aFile" !secondVar!
)


See More: Passing parameter from a bat to a bat (environment variable)

Report •


#1
April 8, 2013 at 05:20:45
simply add that formy needs, I have to get into quotes %%a ('set secondVar="%%a"' ) because the variable 'a' contains a text (not a single string). I hope someone help me!!!!!Thanks

Report •

#2
April 8, 2013 at 05:30:48
Token passing works fine. (Hint: Your problem is not in file1.bat) (Second hint: Leave echo on and analyze the output)
Mon 2013.04.08  8.26.57
M:\>copy con file1.bat
@echo off & SETLOCAL EnableDelayedExpansion
set secondVar=%secondVar%

for /f "tokens=*" %%a in (myFile) do (set secondVar="%%a"
echo !secondVar!
call file2.bat "aFile" !secondVar!
)^Z
        1 file(s) copied.


Mon 2013.04.08  8.27.22
M:\>copy con file2.bat
set i=0
:loop
echo %i%: %0
set /a i += 1
shift
if not "%~0"=="" goto loop^Z
        1 file(s) copied.


Mon 2013.04.08  8.27.54
M:\>copy con myfile
asdf jkl;
Hello World^Z
        1 file(s) copied.


Mon 2013.04.08  8.28.04
M:\>file1
"asdf jkl;"
0: file2.bat
1: "aFile"
2: "asdf jkl;"
"Hello World"
0: file2.bat
1: "aFile"
2: "Hello World"


Mon 2013.04.08  8.28.08
M:\>

How To Ask Questions The Smart Way


Report •

#3
April 8, 2013 at 05:57:51
please could you a little comment your post?? I see many command lines numbered and sorry i am lost a bit
For example in

call file2.bat "aFile" !secondVar!
)^Z

what the point of ")^Z"? I don't understand at all you file2.bat, please enlighten more me! .

Thanks


Report •

Related Solutions

#4
April 8, 2013 at 06:22:19
Ctrl-Z, represented as ^Z denotes end of file. It tells copy to stop saving what I type, and it does not get saved to the file.

How To Ask Questions The Smart Way


Report •

#5
April 8, 2013 at 06:43:20
Forgive me to come back!
It does not a copy i want. The goal of the call file2.bat "aFile" !secondVar! is to search for the secondVar variable into aFile for each secondVar. Why do you make reference to a copy or saving while goal is someplace else??

Thanks


Report •

#6
April 8, 2013 at 07:00:39
It's a play in three acts.
Act 1) I write the files, mostly by copying yours or writing my own to stress my point or workaround what wasn't provided (the VBScript).
Act 2) With everything in place, I run the script(s).
Act 3) I note the output, and post everything here.

You're getting caught up in the first act.

How To Ask Questions The Smart Way


Report •

#7
April 8, 2013 at 12:23:14
Could you please, just tell me what exactly i should write into file1.bat and file2.bat for my needs? I'd would like to go ahead. Since I'm new in batch I shall come back to that a little later to understand!
I have read at the moment some tutorial to learn more but I need to go ahead in scripting.
Thank you for your understanding.

Report •

#8
April 8, 2013 at 18:35:53
Usually (for most people) the first thing you do when you receive a package is... (?)
Remove the packaging! I believe you need to strip the quotes off the "package" in file2.bat (the recipient) before attempting to use it inside of another set of dbl-quotes. As Razor pointed out, running with echo ON would reveal this problem:
::==== file2.bat (crash-test-dummy prototype for evaluation of input-stream and parsing)
echo testing command: findstr /r /c:"*.%2"

Report •

#9
April 11, 2013 at 10:29:43
OK, just tell the one document to echo. the variable into a text document (echo.%variable1% >> Temp.temp) and in your second document write this to extract the contents of Temp.temp as a variable (for /f "tokens=*" %%a in (Temp.temp) do set variable2=%%a) and that makes the variable from the first document (variable1) into the variable in the second document (%variable2%)

1.bat
set /p variable1=}
echo.%variable1% >> Temp.temp

2.bat
for /f "tokens=*" %%a in (Temp.temp) do set variable2=%%a
echo.%variable2%

if you use the for command more than once, change %%a to maybe %%b or %%hello


Report •

#10
April 11, 2013 at 11:11:51
thanks very much at all,
it's OK.

Report •


Ask Question