How do I run a batch script from within a batch sc

2020-02-07 16:33发布

How do I call another batch script from within a batch script?

I want it to execute in an if statement.

7条回答
萌系小妹纸
2楼-- · 2020-02-07 16:55

huh, I don't know why, but call didn't do the trick
call script.bat didn't return to the original console.
cmd /k script.bat did return to the original console.

查看更多
3楼-- · 2020-02-07 16:57

You can use

call script.bat

or just

script.bat
查看更多
放我归山
4楼-- · 2020-02-07 16:59

You can just invoke the batch script by name, as if you're running on the command line.

So, suppose you have a file bar.bat that says echo This is bar.bat! and you want to call it from a file foo.bat, you can write this in foo.bat:

if "%1"=="blah" bar

Run foo blah from the command line, and you'll see:

C:\>foo blah

C:\>if "blah" == "blah" bar

C:\>echo This is bar.bat!
This is bar.bat!

But beware: When you invoke a batch script from another batch script, the original batch script will stop running. If you want to run the secondary batch script and then return to the previous batch script, you'll have to use the call command. For example:

if "%1"=="blah" call bar
echo That's all for foo.bat!

If you run foo blah on that, you'd see:

C:\>foo blah

C:\>if "blah" == "blah" call bar

C:\>echo This is bar.bat!
This is bar.bat!

C:\>echo That's all for foo.bat!
That's all for foo.bat!
查看更多
霸刀☆藐视天下
5楼-- · 2020-02-07 17:01

Use CALL as in

CALL nameOfOtherFile.bat

This will block (pause) the execution of the current batch file, and it will wait until the CALLed one completes.

If you don't want it to block, use START instead.

Get the nitty-gritty details by using CALL /? or START /? from the cmd prompt.

查看更多
forever°为你锁心
6楼-- · 2020-02-07 17:07

You should use CALL

CALL batch.bat
查看更多
地球回转人心会变
7楼-- · 2020-02-07 17:10

Here is example:

You have a.bat:

@echo off
if exist b.bat goto RUNB
goto END
:RUNB
b.bat
:END

and b.bat called conditionally from a.bat:

@echo off 
echo "This is b.bat"
查看更多
登录 后发表回答