Jump to content

How do i make a batch file retrieve its location?


Recommended Posts

Basically i have multi files to run creating a program.

all of the files are in the same folder, so i need my .batch file to retrieve its own location to open the files in a sequencing order.

using the directory input information (C:\Users\Richard\Desktop\Folder) is out of the question because whome ever downloads the file may move the folder to a different location.

This WOULD be much appreciated!

Link to comment
Share on other sites


Hum %CD% will only get the location of the folder from where the batch is launched => if your batch is in e:\temp and then launched from C:\ %CD% will be C:\. It also won't work with unc path.

The right variable there is %~dp0.

To properly use it, something like this should be used at the beginning of your batch:

set batchfolder=%~dp0
echo %batchfolder%

Edited by allen2
Link to comment
Share on other sites

The right variable there is %~dp0.

To be picky, the variable is %0 and %~dp0 is a possible expansion of it.

http://www.robvanderwoude.com/ntfor.php

You can now use the following optional syntax:

%~i - expands %i removing any surrounding quotes (")

%~fi - expands %i to a fully qualified path name

%~di - expands %i to a drive letter only

%~pi - expands %i to a path only

%~ni - expands %i to a file name only

%~xi - expands %i to a file extension only

%~si - expanded path contains short names only

%~ai - expands %i to file attributes of file

%~ti - expands %i to date/time of file

%~zi - expands %i to size of file

%~$PATH:i - searches the directories listed in the PATH environment variable and expands %i to the fully qualified name of the first one found.

If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string

The modifiers can be combined to get compound results:%~dpi - expands %i to a drive letter and path only

%~nxi - expands %i to a file name and extension only

%~fsi - expands %i to a full path name with short names only

%~dp$PATH:i - searches the directories listed in the PATH environment variable for %i and expands to the drive letter and path of the first one found.

%~ftzai - expands %i to a DIR like output line

try also %~f0

@gunsmokingman

this may interest you:

http://www.robvanderwoude.com/ntset.php

%CD% expands to the current directory string (no trailing backslash, unless the current directory is a root directory)

%__CD__% expands to the current directory string, always terminated with a trailing backslash

also a few new (to me at least dynamic variables):

%=C:% expands to the current directory string on the C: drive

%=D:% expands to the current directory string on the D: drive if drive D: has been accessed in the current CMD session

%=ExitCode% expands to the hexadecimal value of the last return code set by EXIT /B

%=ExitCodeAscii% expands to the ASCII value of the last return code set by EXIT /B if greater than 32 (decimal)

The %=D:% may answer the question "Has drive D:\ ever been accessed during this CMD session?".

I have no idea right now where and why this can be useful, but it is "good to know".

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...