1

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

1
  • 1
    ..\BATCHES\batch1.bat May 21, 2018 at 19:59

2 Answers 2

0
  • 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"
6
  • 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
    May 21, 2018 at 19:41
  • 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.
    – LotPings
    May 21, 2018 at 19:48
  • 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
    – gamer0
    May 21, 2018 at 19:57
  • 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.
    – LotPings
    May 21, 2018 at 20:01
  • 1
    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.
    – LotPings
    May 21, 2018 at 20:42
0

Let's consider following scenario:

d:\bat> tree /F "d:\temp\FOLDER 1"
Folder PATH listing for volume VolName
Volume serial number is ABCD-EFGH
D:\TEMP\FOLDER 1
├───BATCHES
│       batch1.bat
│
└───PROGRAMS
        pro.exe

Then, use %~dp0 link relative to the Batch Script and .. relative path to parent directory as in the following script:

@echo OFF
SETLOCAL EnableExtensions
echo(
echo                                     listing "%~dp0"
dir /B /S "%~dp0"
echo                                     listing "%~dp0..\programs"
dir /B /S "%~dp0..\programs"

Output:

d:\bat> "d:\temp\FOLDER 1\BATCHES\batch1.bat"

                                    listing "d:\temp\FOLDER 1\BATCHES\"
d:\temp\FOLDER 1\BATCHES\batch1.bat
                                    listing "d:\temp\FOLDER 1\BATCHES\..\programs"
d:\temp\FOLDER 1\programs\pro.exe

d:\bat>

Note that %~dp0..\PROGRAMS differs from ..\PROGRAMS generally:

  • %~dp0..\PROGRAMS is path relative to directory where the batch resides
    • (d:\temp\FOLDER 1\BATCHES\ in above example)
  • ..\PROGRAMS  is a path relative to current directory where the batch was started from
    • (d:\bat\ in above example)
4
  • 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
    – gamer0
    May 21, 2018 at 19:47
  • My fault, sorry. Imagine set "PathToBatch=%~dp0" (which was there in the script during debugging). Answer updated.
    – JosefZ
    May 21, 2018 at 20:04
  • I meant: whats the difference between "%~dp0..\\FILES\myfile.x" and "..\\FILES\myfile.x"? And why I need dir /B /S ?
    – gamer0
    May 21, 2018 at 20:17
  • 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.
    – JosefZ
    May 21, 2018 at 20:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .