# Can a Windows batch file figure out what directory it is stored in? - Programmer

This is a discussion on Can a Windows batch file figure out what directory it is stored in? - Programmer ; This is probably elementary (whether it can be done or not), but is it possible for a Windows batch file to know, or figure out, what directory it is stored in? My script has to reference other files stored in ...

# Thread: Can a Windows batch file figure out what directory it is stored in?

1. ## Can a Windows batch file figure out what directory it is stored in?

This is probably elementary (whether it can be done or not), but is it
possible for a Windows batch file to know, or figure out, what
directory it is stored in? My script has to reference other files
stored in directories relative to it, and I'd rather not have to set
an environment variable indicating where it is stored.

2. ## Re: Can a Windows batch file figure out what directory it is stored in?

"david.karr" wrote in message
> This is probably elementary (whether it can be done or not), but is it
> possible for a Windows batch file to know, or figure out, what
> directory it is stored in? My script has to reference other files
> stored in directories relative to it, and I'd rather not have to set
> an environment variable indicating where it is stored.

Can't you figure it out using the relative pathnames "." (= current dir) and
".." (= parent dir)?
If you can't, chances are the directories are *not* relative to the current,
working, directory. Maybe you never had to use them, but especially ".." can
be handy, because you can nest them just like regular path names. E.g.,
"..\your_file" is in the directory above the current, "..\..\another" is yet
one directory higher, and "..\..\subdir\yet_another_file" is two directories
up, and then in that subdirectory "subdir".

[Jongware]

3. ## Re: Can a Windows batch file figure out what directory it is stored in?

On Feb 2, 8:19 am, "[Jongware]" wrote:
> "david.karr" wrote in message
>
>
> > This is probably elementary (whether it can be done or not), but is it
> > possible for a Windows batch file to know, or figure out, what
> > directory it is stored in? My script has to reference other files
> > stored in directories relative to it, and I'd rather not have to set
> > an environment variable indicating where it is stored.

>
> Can't you figure it out using the relative pathnames "." (= current dir) and
> ".." (= parent dir)?
> If you can't, chances are the directories are *not* relative to the current,
> working, directory. Maybe you never had to use them, but especially ".." can
> be handy, because you can nest them just like regular path names. E.g.,
> "..\your_file" is in the directory above the current, "..\..\another" is yet
> one directory higher, and "..\..\subdir\yet_another_file" is two directories
> up, and then in that subdirectory "subdir".
>
> [Jongware]

Perhaps I wasn't clear. I need to figure out what directory the script
is stored in, not the directory it is being executed from. I'm quite
familiar with "." and "..", but I don't think that helps me here.

4. ## Re: Can a Windows batch file figure out what directory it is stored in?

On Feb 2, 6:49 am, "david.karr" wrote:
> This is probably elementary (whether it can be done or not), but is it
> possible for a Windows batch file to know, or figure out, what
> directory it is stored in? My script has to reference other files
> stored in directories relative to it, and I'd rather not have to set
> an environment variable indicating where it is stored.

I think I figured this out.

It appears that the magic incantation of "%~dp0" in a batch file
represents the directory where the batch file is stored.