how to navigate in range of parent folder without full path?

9,864
  • The folder the batch resides in can be obtained with %~dp0 (with a trailing backslash)
  • The file system takes care of relative folders addressed with .. meaning one level up.

In a tree A: /F like this:

A:.
└───Folder 1
    ├───BATCHES
    │       batch1.bat
    │
    ├───FILES
    │       myfile.x
    │
    ├───PROGRAMS
    │       pro.exe
    │
    └───SCRIPT
            script.srpt

This batch:

:: A:\FOLDER 1\BATCHES\batch1.bat
@Echo off
Echo batch1 folder is: %~dp0
Echo ======================
for %%A in ("%~dp0..\PROGRAMS\pro.exe")   Do set "_Prg=%%~fA"
for %%A in ("%~dp0..\SCRIPT\script.srpt") Do set "_Inp=%%~fA"
for %%A in ("%~dp0..\FILES\myfile.x")     Do set "_Out=%%~fA"
Set _

Echo "%_Prg%" --input "%_Inp%" --output "%_Out%"

will have this output:

A:\> "Folder 1\BATCHES\batch1.bat"
batch1 folder is: A:\Folder 1\BATCHES\
======================
_Inp=A:\Folder 1\SCRIPT\script.srpt
_Out=A:\Folder 1\FILES\myfile.x
_Prg=A:\Folder 1\PROGRAMS\pro.exe
"A:\Folder 1\PROGRAMS\pro.exe" --input "A:\Folder 1\SCRIPT\script.srpt" --output "A:\Folder 1\FILES\myfile.x"
Share:
9,864

Related videos on Youtube

gamer0
Author by

gamer0

bitcoin donate: bc1qn4ht6mecfnvnrr7y6jmupudzsa0r8juuykd3lr paypal donate: paypal.me/user0player0 patreon donate: patreon.com/user0player0 coffee donate: buymeacoffee.com/player0 as a reward for answering joint 6000+ questions across servers, I find myself being constantly targetted by mods which deliberately abuse their moderator privileges to ban this account due to ridiculous false accusations. therefore, I decided to slowly withdraw from the whole community due to reasonably lost appetite. thank you to all my supporters.

Updated on September 18, 2022

Comments

  • gamer0
    gamer0 over 1 year

    is there a way how to navigate to a program that is in different folder of the parent directory without using full path?

    atm I have a batch here:

    FOLDER 1\BATCHES\batch1.bat
    

    which uses a program that is located in:

    FOLDER 1\PROGRAMS\pro.exe 
    

    and my batch looks like this:

    "%SYSTEMDRIVE%\Temp\FOLDER 1\PROGRAMS\pro.exe" --input "%SYSTEMDRIVE%\Temp\FOLDER 1\SCRIPT\script.srpt" --output "%SYSTEMDRIVE%\Temp\FOLDER 1\FILES\myfile.x"
    

    as you can see its locked to a place with %SYSTEMDRIVE%\Temp\ so in order to make my script "portable", is there something I could replace that %SYSTEMDRIVE%\Temp\?

    or putting it in another words: I would like to drop all ocurances of %SYSTEMDRIVE%\Temp\ from my batch and replace it with something in a way that if I move my FOLDER 1 wherever in my PC, it will run

    • Joel Coehoorn
      Joel Coehoorn almost 6 years
      ..\BATCHES\batch1.bat
  • gamer0
    gamer0 almost 6 years
    if I may ask, is a %~dp0 part with whole for %%A in (".... ... Set_ part needed and why? I tried with just this: "..\\PROGRAMS\pro.exe" --input "..\\SCRIPT\script.srpt" --output "..\\FILES\myfile.x" and batch executes everything as should
  • gamer0
    gamer0 almost 6 years
    if I may ask, is a %~dp0 part with whole SETLOCAL EnableExtensions ... dir /B /S "%PathToBatch%" ... dir /B /S "%PathToBatch%..\programs" part needed and why? I tried with just this: "..\\PROGRAMS\pro.exe" --input "..\\SCRIPT\script.srpt" --output "..\\FILES\myfile.x" and batch executes everything as should
  • LotPings
    LotPings almost 6 years
    The for is just used to resolve the expression inside the double quotes, it would also work directly, but is less clear: A:\FOLDER 1\BATCHES\..\PROGRAMS\pro.exe in my example.
  • gamer0
    gamer0 almost 6 years
    what I meant was: yes I understand whole your answer/code... its just that both answers mention usage of %~dp0 in a one way or another, so my question was more about: its a way of choice to use %~dp0 or do I loose some benefit if I go without it, just and only with "..\\PROGRAMS\pro.exe" --input "..\\SCRIPT\script.srpt" --output "..\\FILES\myfile.x" and nothing else
  • LotPings
    LotPings almost 6 years
    Depending on context "..\PROGRAMS\pro.exe" --input "..\SCRIPT\script.srpt" --output "..\FILES\myfile.x" could refer to the current directory %CD% which is not neccessarily the same as the batch path.
  • JosefZ
    JosefZ almost 6 years
    My fault, sorry. Imagine set "PathToBatch=%~dp0" (which was there in the script during debugging). Answer updated.
  • gamer0
    gamer0 almost 6 years
    I apologize if I am anoying but could you pls explain: "...could refer to the current directory %CD% which is not neccessarily the same as the batch path." I reading it all over and cant understand the point
  • gamer0
    gamer0 almost 6 years
    I meant: whats the difference between "%~dp0..\\FILES\myfile.x" and "..\\FILES\myfile.x"? And why I need dir /B /S ?
  • LotPings
    LotPings almost 6 years
    When you open a cmd window you see a prompt like C:\Users\UserName> all actions without expressed other path take place in this folder. If you invoke a batch with a path like C:\Temp\FOLDER 1\BATCHES\batch1.bat the current directory stays the same C:\Users\UserName so a relative path will be relative to the current folder not to the folder the batch resides in.
  • JosefZ
    JosefZ almost 6 years
    The difference is now explained in the updated answer. I used dir /B /S "%~dp0..\programs" merely to show result of using .. in a path specification. SuperUser isn't a free scripting service so you can't expect a foolproof solution whensoever.