how to get the bat file self's path in a bat file?

  • Thread starter Thread starter hantechs
  • Start date Start date
Is there a way to get the bat file self 's path in a bat file?Thx
Hi,

To get the path not including the bat file name, you need to parse %0:

@echo off
set scriptpath=%~d0%~p0
echo %scriptpath%


For more on the %~ parameters, at the command prompt enter "FOR /?"
 
Gospel said:
One can also pipe the current directory e.g.

cd > C:\TellMeWhere.txt

And the current directory will be texted into TellMeWhere.txt

Comment?

The current directory may or may not be the directory in which the
batch file resides. As long as you are using Win2000 or newer,
the system variable %cd% will always contain the name of the
current directory, so there is no need to write it to a file.

The directory in which the batch file resides is '%~dp0'.
 
Thank you. I've bought myself "Microsoft WINDOWS COMMAND-LINE" (Stanek) and
"Microsoft Windows Shell Script Programming for the absolute beginner"
(Ford, Harris) and am still finding my way about .bat and .cmd files.
 
Gospel said:
Thank you. I've bought myself "Microsoft WINDOWS COMMAND-LINE" (Stanek) and
"Microsoft Windows Shell Script Programming for the absolute beginner"
(Ford, Harris) and am still finding my way about .bat and .cmd files.

You're welcome. Another good book is Tim Hill's _Windows NT Shell
Scripting_. I do *not* recommend Stanek's _Windows NT Scripting
Administrator's Guide_.

The two best newsgroups for batch file programming are
alt.msdos.batch.nt and microsoft.public.win2000.cmdprompt.admin. You
can find out a ton of useful information by using Google to search these
newsgroups. Also check out www.jsiinc.com, which has lots and lots of
very useful stuff.

Happy batching!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top