Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

up vote 11 down vote accepted
%~dp0

Will give you the full path to the script.

%~f0

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.