relative path in BAT script
Asked Answered
C

6

173

Here is my own program folder on my USB drive:

Program\
     run.bat
     bin\
         config.ini
         Iris.exe
         library.dll
         etc.

I would like to use run.bat to start Iris.exe

I cannot use this: F:/Program/bin/Iris.exe like a shortcut, because sometimes it does not attach as drive F: (e.g. E: or G:)

What do I need to write in the bat file to work regardless of the drive letter?

I tried this in the BAT file:

"\bin\Iris.exe"

But it does not work.

Clingfish answered 18/2, 2013 at 12:43 Comment(0)
O
351

Use this in your batch file:

%~dp0\bin\Iris.exe

%~dp0 resolves to the full path of the folder in which the batch script resides.

Orelu answered 18/2, 2013 at 18:28 Comment(4)
Actually this resolves to something like C:\myDir\\bin\Iris.exe (note the double-backslash). This still works but leaving away the backslash before bin seems to be "cleaner"? --> %~dp0bin\Iris.exe.Ponder
@Ponder If you can guarantee that %~dp0 will always have a trailing backslash both statements will work. Otherwise the one with the additional backslash is the safer variant.Orelu
Ok, that's a point. I've only tested this on two different Windows 7 machines, might be different elsewhere (XP, Vista oder Windwos 8 --> I don't know but: Microsoft logic and I couldn't find any docs about it ;)). However, I found that I had to put quotation marks around it ("%~dp0\bin\Iris.exe") as the path had a whitespace in it :) Just to be really sure it works on every computer.Ponder
you can ensure there's backslash with SET "scriptdir=%~dp0" and on the next line IF NOT "%scriptdir:~-1%"=="\" SET "scriptdir=%scriptdir%\". I've seen incidents where double backslash in middle of the path breaks software.Provided
B
54

You can get all the required file properties by using the code below:

FOR %%? IN (file_to_be_queried) DO (
    ECHO File Name Only       : %%~n?
    ECHO File Extension       : %%~x?
    ECHO Name in 8.3 notation : %%~sn?
    ECHO File Attributes      : %%~a?
    ECHO Located on Drive     : %%~d?
    ECHO File Size            : %%~z?
    ECHO Last-Modified Date   : %%~t?
    ECHO Parent Folder        : %%~dp?
    ECHO Fully Qualified Path : %%~f?
    ECHO FQP in 8.3 notation  : %%~sf?
    ECHO Location in the PATH : %%~dp$PATH:?
)
Barn answered 18/2, 2013 at 23:28 Comment(1)
How to get relative path from absolute path?Dominick
H
37

I have found that %CD% gives the path the script was called from and not the path of the script, however, %~dp0 will give the path of the script itself.

Honeywell answered 3/5, 2017 at 11:13 Comment(0)
N
17

You should be able to use the current directory

"%CD%"\bin\Iris.exe

Natant answered 17/3, 2017 at 12:26 Comment(2)
This fails when the current directory isn't Program, this will happen when you double click the run.bat from the explorer. %CD% is the current directory %~dp0 is the directory of the batch file itselfBesprinkle
That will not happen when you double click the batch file from Explorer, because Explorer runs things with the current directory set to the containing directory. The only exception is shortcuts, where you can manually change the directory in which the shortcut is started; but even there the default value is the containing folder of the shortcut's destination i.e. equivalent to %~dp0. The failure scenario for using a relative path like this is when the batch file is run manually, or from another batch file (or program).Desmoid
S
6

either bin\Iris.exe (no leading slash - because that means start right from the root)
or \Program\bin\Iris.exe (full path)

Spatial answered 18/2, 2013 at 12:52 Comment(3)
bin\Iris.exe it is not working :( I don't like to use root, because someday maybe I will move this dir to an other location. And what if I ask from the OS the current absolute path? and I will use that to start exe in bin?Clingfish
I assumed the current drive would be the USB stick's drive, and current folder would be \Program - is that not the case? You can show that with a simple cd command in the line before you try to run the .exeSpatial
Simply using a relative path won't necessarily work. The path will be relative to the current working directory, which may be different from the parent directory of run.bat.Orelu
P
0
%~dp0 will return the drive and the path
so try two things
%~dp0 --> will give ..F:\Program\run.bat
now inside the .bat file if we want to access exe then use below
pushd "%~dp0" 
..\bin\Iris.exe 

Explanation
-->PUSHD command to get the directory of the batch script file in the Windows operating system.
-->while %CD% can be used on command line.
for %CD%, the current directory means the directory when executing the command line or the batch file. So if you put the batch file in c:\dir\test.bat --> @echo %CD%, if you are now in C:\dir and execute test.bat, it will output c:\dir;

-->%~dp0 can only be used in bat file 
For %~dp0, the current directory is the directory where the bat file resides. In above example - the output is the same: c:\dir\. Note there is a back slash at the end.
Hope this helps!
Psychopath answered 18/7, 2023 at 17:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.