Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Command Prompt To Call a Batch File in a Sub-Directory

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.

like image 232
KevinO Avatar asked Jan 11 '11 23:01

KevinO


People also ask

How do I run a batch file in a folder?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to run a Windows 10 batch file and press Enter: C:\PATH\TO\FOLDER\BATCH-NAME. bat.

How do I run a sub folder in command prompt?

If you want to move to a subdirectory of another parent directory or if you want to move up to the root of the drive, you would want to use CD \ (please note that there is a space between CD and the backslash character). If you type CD \ and press Enter, you will then be at the root of the current drive.

How do you call a batch file?

Run a batch file from the Command Prompt To run a batch file, move to the directory containing the file and type the name of the batch file. For example, if the batch file is named "hope. bat," you'd type "hope" to execute the batch file.

How do I open a batch file in CMD?

Using a BAT file in Windows is as simple as double-clicking or double-tapping it. You don't need to download any special program or tool. To use the first example from above, entering that text into a text file with a text editor and then saving the file with the .


1 Answers

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.

like image 130
Courtney Christensen Avatar answered Oct 13 '22 02:10

Courtney Christensen