32

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?

  • 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 '14 at 9:43
57
%~p0

Will return the path only.

%~dp0

Will return the drive+path.

More info on the subject can be found on Microsoft's site.

Information about this syntax can also be found in the help for the for command by executing for /? on a Windows OS.

| improve this answer | |
  • 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
  • 1
    Probably title of the article in the link could have been supplied, since the link is no more valid..... – Rupsingh Mathwale Apr 24 '18 at 6:39
16

The current directory is held in %CD%

| improve this answer | |
  • 4
    %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
  • 4
    @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 '14 at 9:46
4

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

| improve this answer | |
0

use chdir command

Option 1:

chdir 

Option 2:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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