relative path in BAT script

Batch FileCmdRelative PathDrive

Batch File Problem Overview


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.

Batch File Solutions


Solution 1 - Batch File

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.

Solution 2 - Batch File

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

Solution 3 - Batch File

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.

Solution 4 - Batch File

You should be able to use the current directory

> "%CD%"\bin\Iris.exe

Solution 5 - Batch File

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2083037View Question on Stackoverflow
Solution 1 - Batch FileAnsgar WiechersView Answer on Stackoverflow
Solution 2 - Batch FileGaurav Kolarkar_InfoCeptsView Answer on Stackoverflow
Solution 3 - Batch FileSitriView Answer on Stackoverflow
Solution 4 - Batch FileJohan A.View Answer on Stackoverflow
Solution 5 - Batch FileAjV JsyView Answer on Stackoverflow