Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 12 down vote accepted

Will give you the full path to the script.


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

share|improve this answer
Well done. I was about to write something similar, but you beat me to it. – dgw Feb 15 '11 at 22:48
@Voyager Thanks! ;) – BloodPhilia Feb 15 '11 at 22:49
And I do mean that your answer was posted less than a minute before I was to start typing. :) – dgw Feb 15 '11 at 22:50
@Voyager I've had lots of exercise! ;) – BloodPhilia Feb 15 '11 at 22:53
If you're going to use the shift command though, you need to make sure that you cache that information somewhere (like a variable) before doing a shift or you'll lose your information. – afrazier Feb 15 '11 at 23:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .