Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I use %0 in batch file to get the containing 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?

share|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
    
The directory containing the currently executed script is not necessarily the same as the current working directory (CD)! I edited the question accordingly - it now avoids the term of "current directory". –  chiccodoro Jan 6 at 9:43

4 Answers 4

up vote 36 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

share|improve this answer
1  
Thanks for pointing it out. –  Bart De Vos Apr 4 '11 at 7:40
2  
As another reference source, the same list of variable substitutions is also shown when you type for /? –  Kevin Apr 4 '11 at 15:06
    
%~dp0 gives the directory of the executing batch file. If the batch file is in a different directory, you get that other directory. %CD% always gives you the current directory. –  Mihai Danila Feb 25 '13 at 19:30
    
@MihaiDanila, no, that is not what OP asked. –  Bart De Vos Feb 25 '13 at 23:00
    
Actually I got to this question while searching for a way to get the current directory. This was the first hit on Google. The title obviously says current directory, although you're right about the content. We're both right, but I think lain and I are more right about what the question actually conveys, in spite of what was intended by it. It's why I've upped lain's answer... The question title should be renamed to be in sync with the question itself. –  Mihai Danila Feb 25 '13 at 23:15

The current directory is held in %CD%

share|improve this answer
2  
%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
3  
@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
    
%CD% returns the current path of the batch file but fails when we execute batch file as an administrator. When we run batch file as an administrator then it returns path of System32. So better option is to use %~dp0 for complete path –  user133165 Aug 23 '12 at 3:40
    
%~dp0 gives the directory of the executing batch file. If the batch file is in a different directory, you get that other directory. %CD% always gives you the current directory. –  Mihai Danila Feb 25 '13 at 19:30
    
For the reader's reference - the question was originally using the term "current directory" while meaning the directory containing the script - this is now fixed. –  chiccodoro Jan 6 at 9:46

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

share|improve this answer

use chdir command

Option 1:

chdir 

Option 2:

echo %CD%
share|improve this answer
    
Same here: This was not the OP's intention - the question is now clearer. –  chiccodoro Jan 6 at 9:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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