Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a .bat file. I want to programmatically get the name of the .bat file. How can I do this?

This is my end goal

"..\lib\nant\nant.exe" -buildfile:nant.build {{pass in name of this file here}}
pause
share|improve this question

2 Answers 2

up vote 7 down vote accepted

Try for /? on the command line. The help shows all kinds of useful filename substitutions, such as:

%~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

Replace I with 0 to get just the batch file name, and replace I with 1, 2, 3, etc. to get argument names.

share|improve this answer
    
I ended up with the following: "..\lib\nant\nant.exe" -buildfile:nant.build %~n0 pause Works great! Thanks! –  Ryan Montgomery Sep 15 '09 at 21:45

The %0% variable will give you the fully qualified path to the batch file, including its name. There may be a better way to get just the name.

share|improve this answer
    
It's just %0 as it's no variable but an argument. –  Joey Sep 16 '09 at 10:09

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.