Windows batch file reference to own directory

batch filecommand linewindows

Suppose you have C:\foo\foo.bat which needs to refer to C:\foo\foo.txt. It may be run from a different directory, but needs to get foo.txt from its own directory, not the current directory. Obviously this could be done by putting the full path C:\foo\foo.txt in foo.bat.

The twist is, it's not known at the time of writing the batch file, where it will end up residing on the user's machine, so what the batch file actually needs to do is get foo.txt from the directory where I live, wherever that happens to be. (In a C program I'd use argv[0] but that doesn't seem to work with batch files.)

Is there a way to do this?

Best Answer

%~dp0

Will give you the full path to the script.

%~f0

Will give you the full path to the script, including the script name.