I use %0 in batch file to get the current directory of the batch file but the result is :-

c:\folder1\folder2\batch.bat

I want just directory, without batch file name, like this :-

c:\folder1\folder2\

How can I do it? Maybe I should filter the path. If yes, how can I do it?

link|improve this question

When using %0 in a batch file as part of a command line you should use %0\..\RestOfStuff. The double dot takes it back past the filename. – John Gardeniers Apr 4 '11 at 8:30
feedback

3 Answers

up vote 5 down vote accepted
%~p0

Will return the path only.

%~dp0

Will return the drive+path.

More info on the subject can be found on Microsofts site

link|improve this answer
1  
Thanks for pointing it out. – Bart De Vos Apr 4 '11 at 7:40
1  
As another reference source, the same list of variable substitutions is also shown when you type for /? – Kevin Apr 4 '11 at 15:06
feedback

The current directory is held in %CD%

link|improve this answer
%CD% reture the path which you execute the batch from it not where the batch file exist – Mohammad AL-Rawabdeh Apr 4 '11 at 7:33
1  
@Mohammad: Indeed, it is the current directory. The other one is the directory where the batch file is. Those two are not the same and your question asks explicitly for this one. So please edit it if that is not what you mean. – Joey Apr 4 '11 at 7:36
feedback

Some expressions that effect the filename:

~f0 will give the fully qualified file name.
~dpnx0 will give the same as ~f0, but this shows you that you can break it down into parts: d=drive p=path n=name x=extension

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.