Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 603
Jason Moore Avatar asked Jul 13 '14 12:07

Jason Moore


People also ask

How do I run an elevated batch file?

Right click->run as Admin on batch files would be more convenient in that case than first having users enable powershell scripts.

What is the command to move up one level in a folder?

To navigate to your home directory, use "cd" or "cd ~" To navigate up one directory level, use "cd .." To navigate to the previous directory (or back), use "cd -"

What does %% A mean in batch?

%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files. Wildcards may be used.

What is @echo off in batch script?

batch-file Echo @Echo off @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.


1 Answers

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.

like image 124
Mofi Avatar answered Oct 06 '22 00:10

Mofi