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.
If you have any sort of GNU toolset installed, you should be able to go
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) – Phoshi – 2010-07-06T22:16:28.6272i need to avoid GNU tools so that the batch file will work anywhere for anyone. My question is for "pure DOS" anyways. – djangofan – 2010-07-06T23:17:51.053
Alright. A quick google showed a SO result for implementing regex search and replace in VBScript (http://stackoverflow.com/questions/127318/is-there-any-sed-like-utility-for-cmd-exe) which could use the same syntax and create the same result - I believe VBScript has been built in since windows 98, so should be quite anywhere for everyone! (You could also very easily rejigger it to work on *nix OS', too)
– Phoshi – 2010-07-07T09:18:54.6833FYI, neither
for /f
nor TomWij's%~n*
are supported in MS-DOS. (Windows'cmd.exe
is not DOS, it's a native Windows program.) – user1686 – 2010-07-07T12:05:12.863Are the Command Prompt and MS-DOS the same thing?, The Windows command prompt is NOT a DOS prompt! – phuclv – 2016-11-30T16:44:58.877