169

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.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
user2083037
  • 1,691
  • 2
  • 11
  • 3

6 Answers6

345

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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 10
    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`. – mozzbozz Nov 05 '14 at 14:09
  • 12
    @mozzbozz 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. – Ansgar Wiechers Nov 05 '14 at 14:55
  • 5
    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. – mozzbozz Nov 05 '14 at 17:17
  • 2
    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. – LogicDaemon Jul 07 '19 at 06:48
51

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:?
)
35

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.

Compo
  • 36,585
  • 5
  • 27
  • 39
Sitri
  • 471
  • 4
  • 3
17

You should be able to use the current directory

"%CD%"\bin\Iris.exe

Johan A.
  • 378
  • 3
  • 7
  • 1
    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 itself – jeb May 03 '17 at 13:11
  • 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). – DimeCadmium Apr 06 '23 at 19:11
6

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

AjV Jsy
  • 5,799
  • 4
  • 34
  • 30
  • 1
    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? – user2083037 Feb 18 '13 at 14:21
  • 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 .exe – AjV Jsy Feb 18 '13 at 15:34
  • 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`. – Ansgar Wiechers Feb 18 '13 at 18:40
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!