How can I get the name of a windows .bat script from within the script itself?

7,829

Solution 1

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.

Solution 2

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:
7,829

Related videos on Youtube

rmontgomery429
Author by

rmontgomery429

I love web development. Most people would describe me as "passionate" about my work, and I have to agree. For as long as I can remember, I have always wanted to be a web developer, and I have been trying, failing, and learning from my mistakes ever since. I appreciate the mix of art and technology that good web development demands. Good software takes time, and good web apps take patience. I think the internet is the most fascinating medium ever created and I love talking to and teaching others about the web and all its amazing abilities. I spend most of my time creating applications using web technologies for business' large and small. Some of these applications are closed source, others are open source. Some I create for my own use, but most I create for others. I do not consider web development work; I love doing it. It's what I would do with my free time regardless, so I consider myself lucky that people pay me to do it (maybe you?). I like working with people and I value pair programming with other developers. I'm not an introvert and I don't intend to work somewhere that expects me to be one. I enjoy contributing ideas and thinking aloud. I try very hard to be a good listener but realize that I have a tendency to work and move at a fast pace. I enjoying learning new things and consider web development to be a continuous learning process. If I currently don't possess all the skills you're looking for, I can guarantee you I'll pick them up in no time. I love to be challenged, especially when it comes to programming, computers, technology, etc. Oh yeah - I like coffee, wine, and beer too.

Updated on September 17, 2022

Comments

  • rmontgomery429
    rmontgomery429 over 1 year

    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
    
  • rmontgomery429
    rmontgomery429 over 14 years
    I ended up with the following: "..\lib\nant\nant.exe" -buildfile:nant.build %~n0 pause Works great! Thanks!
  • Joey
    Joey over 14 years
    It's just %0 as it's no variable but an argument.