40

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?

2
  • 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. Apr 4, 2011 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, 2014 at 9:43

4 Answers 4

66
%~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.

6
  • 2
    As another reference source, the same list of variable substitutions is also shown when you type for /?
    – Kevin
    Apr 4, 2011 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. Feb 25, 2013 at 19:30
  • @MihaiDanila, no, that is not what OP asked. Feb 25, 2013 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. Feb 25, 2013 at 23:15
  • 1
    Probably title of the article in the link could have been supplied, since the link is no more valid..... Apr 24, 2018 at 6:39
17

The current directory is held in %CD%

5
  • 5
    %CD% reture the path which you execute the batch from it not where the batch file exist Apr 4, 2011 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, 2011 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, 2012 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. Feb 25, 2013 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, 2014 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

1

use chdir command

Option 1:

chdir 

Option 2:

echo %CD%
1
  • Same here: This was not the OP's intention - the question is now clearer.
    – chiccodoro
    Jan 6, 2014 at 9:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .