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.
Re: Can a Windows batch file figure out what directory it is stored in?
"david.karr" <davidmichaelkarr@gmail.com> wrote in message
news:1170427761.997043.96790@a34g2000cwb.googlegroups.com...[color=blue]
> 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.[/color]
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]
Re: Can a Windows batch file figure out what directory it is stored in?
On Feb 2, 8:19 am, "[Jongware]" <sorry@no_spam.plz> wrote:[color=blue]
> "david.karr" <davidmichaelk...@gmail.com> wrote in message
>
> news:1170427761.997043.96790@a34g2000cwb.googlegroups.com...
>[color=green]
> > 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.[/color]
>
> 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][/color]
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.
Re: Can a Windows batch file figure out what directory it is stored in?
On Feb 2, 6:49 am, "david.karr" <davidmichaelk...@gmail.com> wrote:[color=blue]
> 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.[/color]
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.