Solved Call Same Batch with Different Variable

September 20, 2011 at 09:59:56
Specs: Windows 7 Home edition
I'm trying to call a batch file from another batch file. Thus:

B1 = first batch and will be called via Windows Task Scheduler. This will pass a variable to B2
B2 = contains other commands

B1.bat

Call B2.bat %var1%
Call B2.bat %var2%

The first line Call B2.bat %var1% is being called and executed but it doesn't execute the second line. Can you tell what I'm missing?

B2.bat basically closes after executing commands. Thanks!


See More: Call Same Batch with Different Variable

Report •

#1
September 20, 2011 at 12:09:23
✔ Best Answer
I'm not sure on this, but it might be timing. Maybe you need a timeout between calling b2.bat the first and second time? If that doesn't work... Since you know that it calls the batch with the variable correctly the first time, you could create a B3.bat that contains the exact same code as B2.bat and pass var2 to it.

Report •

#2
September 20, 2011 at 19:58:31
The problem is that when a CMD window closes, all of the variables are dumped. Use something like this:

ECHO %VAR1% > TMP.TXT
CALL B2.BAT

The above 2 lines of code need to go in B1, then put this line in B2:

SET /P VAR1=<TMP.TXT

This will re-import the variable into the second batch file.

Mouse could not be found. Click OK to continue.
What to do now?


Report •

#3
September 21, 2011 at 07:56:53
thanks for your response guys.

Just an FYI, adding timeout in between did not work. It still closed BAT1.bat without executing the second line.

RMT2's suggestion also only worked on first call.


Report •

Related Solutions

#4
September 22, 2011 at 08:04:21
It's hard to see the problem without seeing the second script.
$50 says B2.BAT contains an EXIT.

How To Ask Questions The Smart Way


Report •

#5
September 22, 2011 at 12:20:01
Did you try my second idea? ...Since you know that it calls the batch with the variable correctly the first time, you could create a B3.bat that contains the exact same code as B2.bat and pass var2 to it.

Report •

#6
September 22, 2011 at 21:02:29
Razor 2.3. I think you're right. It's what I missed so you guys will understand more my issue.

@jowah. I was about to do it but...

I got it working right using

"start /wait" with timeout.

Thanks all for your responses. Indeed, this forum is a gem for newbies like me.


Report •

Ask Question