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?
Right click->run as Admin on batch files would be more convenient in that case than first having users enable powershell scripts.
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 -"
%%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.
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.
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:
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.
"%~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 "%~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 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With