155

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
  • 35,848
  • 27
  • 79
  • 88
user2083037
  • 1,551
  • 2
  • 10
  • 3

5 Answers5

319

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
  • 184,186
  • 23
  • 230
  • 299
  • 8
    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
  • 4
    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
49

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

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
  • 34,184
  • 4
  • 22
  • 36
Sitri
  • 451
  • 4
  • 3
16

You should be able to use the current directory

"%CD%"\bin\Iris.exe

Johan A.
  • 338
  • 2
  • 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
5

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,531
  • 4
  • 32
  • 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