10

I'd like to use a batch file to call a different batch file that is in a sub directory. For instance, if my file system look as follows:

MainFolder
    main.bat
    FirstDirectory
    SecondDirectory
        foo.bat

Then main.bat might look something like this:

echo on
REM This lines tells the user what this bat file is doing
call ant
call \SecondDirectory\foo.bat

I'm looking for a one line solution which I think does not exist. Unfortunately I don't always want to do this with a batch file and want to do it directly from the command line.

2
  • 3
    If you remove the leading backslash, it should work: call SecondDirectory\foo.bat Commented Jan 11, 2011 at 23:03
  • I'm not sure i understand the problem, the main batch file you listed is calling another batch file in a subdirectory (though the \ in the beginning forces it to be off the root directory). What's the actual issue? If you just want to combine the last two lines into one, then you can use the && and || operators which will run the command after && if the first command succeeds, while the || will run the command after it only if the preceding command fails. Commented Jan 11, 2011 at 23:04

1 Answer 1

17

You can indeed call a batch file from another batch file using the call command. As @Blorgbeard states, the issues is the leading backslash \. Removing it indicates SecondDirectory is relative to the current working directory.

call SecondDirectory\foo.bat

A word of caution: the call command essentially inserts called code in at run time. Be careful to avoid variable name collisions.

Sign up to request clarification or add additional context in comments.

2 Comments

Using call ".\folder\file.bat" will also work (. is the current directory and quoting paths is usually a good idea)
this is the answer to the question that I asked. Unfortunately, it was not the solution to the problem I was facing but it did lead me to figuring out the problem. So, thanks, twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.