Search code examples
windowsbatch-filecmdrelative-path

Batch - Resolve relative path


My batch scripts are located in the following directory:

\\test\public\windows\scripts\32\test.bat

I'm trying to get the path of this folder: \test\public\windows\logs I've tried using %~dp0 in order to get the path of the script, and i've tried:

SET REL_PATH=..\..\

The thing is that it's not showing me this folder.


Solution

  • SET REL_PATH=..\..\
    

    path points to the path according to you, to verify where it is taking from, please make a

    echo %CD%
    

    it will tell you the path the system is looking at.

    then set the relative path from %CD% to the path of your script.

    Please comment if you get stuck after doing this.

    For example:

    echo %CD%
    

    gives C:\windows\system32

    and you want to execute batch file at c:\test\public\windows\scripts\32\test.bat

    you will need to do

    SET REL_PATH=%CD%\..\..\test\public\windows\scripts\32\
    

    To move to this path do a:

    cd /d %REL_PATH%
    

    To solve UNC path do PUSHD and POPD:

    @echo off
    pushd \\test\public\windows\scripts\32\
    
    REM do your work
    call test.bat
    
    popd
    

    Reference: How to run batch file from network share without "UNC path are not supported" message?