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

后端 未结 4 2121
北海茫月
北海茫月 2020-11-22 13:06

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.

相关标签:
4条回答
  • 2020-11-22 13:53

    ..\ is used to go one level up. your case requires two levels up Try:

    start ..\..\code.bat
    
    0 讨论(0)
  • 2020-11-22 14:04

    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.

    0 讨论(0)
  • 2020-11-22 14:07

    You could just:

    cd..
    start Code.bat
    

    And that would start code.bat from its own directory

    0 讨论(0)
  • 2020-11-22 14:09

    try to use this:

    start ../code.bat
    
    0 讨论(0)
提交回复
热议问题