How to call a batch file that is one level up from the current directory?

Jason Moore picture Jason Moore · Jul 13, 2014 · Viewed 71.8k times · Source

I'm using a batch file in folder1/folder2/file.bat

There is a batch file in parent folder folder1, that I want to open through file.bat

I have tried using:

start ..\..\code.bat

But this results in an error message, because file couldn't be found.

Any ideas?

Answer

Mofi picture Mofi · Jul 13, 2014

I want to explain better what should be used with an example as the answers posted up to now work only with current working directory being the directory containing the batch file file.bat.

There is a directory structure as follows:

  • C:\
    • Temp
      • Folder 1
        • Folder 2
          • Example.bat
        • Parent.bat

The current working directory is C:\Temp on executing Example.bat either with

"Folder 1\Folder 2\Example.bat"

or with

"C:\Temp\Folder 1\Folder 2\Example.bat"

The batch file Parent.bat contains for example:

echo %0 is active.
pause

The batch file Example.bat contains already:

@echo off
echo Calling Parent.bat ...
rem How to run Parent.bat here?
echo %0 is active.
pause

The really working solutions in this scenario with the current working directory being a different directory than directory containing Example.bat are as follows.


Continue batch processing with Parent.bat

"%~dp0..\Parent.bat"

%0 references argument 0 on execution of the batch file which is always the name of the batch file as specified in parent process on starting the batch file.

But wanted is the drive and path of the batch file without double quotes. Therefore the expression %~dp0 is used to get C:\Temp\Folder 1\Folder 2\ from argument 0.

On this path the string ..\Parent.bat is appended, and additionally the entire new file name
C:\Temp\Folder 1\Folder 2\..\Parent.bat is enclosed in double quotes because of the spaces.

There is no return to Example.bat after processing of Parent.bat finished.


Call Parent.bat like a subroutine

call "%~dp0..\Parent.bat"

Command call results in execution of batch file Parent.bat in same command process (console window) with halting the execution of Example.bat until Parent.bat finished.

The batch execution continues on next line in Example.bat after processing of Parent.bat finished.

Exception:
Parent.bat contains command exit without switch /B because this results in an immediate exit of command line interpreter cmd.exe processing Example.bat and Parent.bat.

Execute call /? or help call in a command prompt window for short help on command call.


Start Parent.bat as parallel process

start "Parent Batch" "%~dp0..\Parent.bat"

Command start without any parameter with the exception of the optional title results in execution of batch file Parent.bat by a separate command process in a separate console window without halting the execution of Example.bat.

Therefore both batch files run at same time (more or less).

Note:
Command start interprets first string in double quotes as title. Therefore it is necessary to define explicitly a title in double quotes when the batch file or application to start, or any argument of the started batch file / application must be specified in double quotes because of 1 or more spaces.

Execute start /? or help start in a command prompt window for short help on command start.


Call Parent.bat as separate process

start "Parent Batch" /wait "%~dp0..\Parent.bat"

Command start with optional parameter /wait results in execution of the started batch file / application as separate process (additional console window for a batch file or console application), but halting the execution of the current batch file until the started process (Windows application or batch file / console application executed in a new console window) terminates itself.