If you want to know the current location of the batch file (and if your Windows isn't a very ancient release), type for /?
in a 'DOS box' window. Scroll down. Read.
You'll find out, that you can now read (from within the batch file) these variables:
%0 - as the name how this batchfile was called
%~d0 - as the drive letter where this batchfile is located ('\\' in case of share)
%~p0 - as path (without the drive letter) where this batchfile is located
%~n0 - as filename (without suffix) of this batchfile
%~x0 - as filename's suffix (without filename) of this batchfile
%~a0 - as this batchfile's file attributes
%~t0 - as this batchfile's date+time
%~z0 - as this batchfile's filesize
%~dpnx0 - as this batchfile's fully qualified path+filename
[... and then some more ...]
This works for many cases. Assume, the batchfile is called mytest.bat
. You may call it in different ways:
..\..\to\mytest.bat
............................... (relative path)
d:\path\to\mytest.bat
........................... (full path)
\\fileserver\sharename\mytest.bat
... (path on remote share)
...and you'll always get the right value in your variables.
cd | sed "s/.*\\//"
(That pipes the output of cd (cwd) into a regular expression search and replace, replacing everything before the final \ with nothing at all)for /f
nor TomWij's%~n*
are supported in MS-DOS. (Windows'cmd.exe
is not DOS, it's a native Windows program.)