37

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?

chiccodoro
  • 117
  • 5
Mohammad AL-Rawabdeh
  • 1,592
  • 12
  • 30
  • 54
  • 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 04 '11 at 08: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 06 '14 at 09:43

4 Answers4

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

Cinnam
  • 105
  • 7
Bart De Vos
  • 17,761
  • 6
  • 62
  • 81
  • 2
    As another reference source, the same list of variable substitutions is also shown when you type `for /?` – Kevin Apr 04 '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
  • @Mihai, I have edited the question accordingly, avoiding the term "current directory" completely. – chiccodoro Jan 06 '14 at 09:45
  • 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 06:39
16

The current directory is held in %CD%

user9517
  • 114,104
  • 20
  • 206
  • 289
  • 4
    %CD% reture the path which you execute the batch from it not where the batch file exist – Mohammad AL-Rawabdeh Apr 04 '11 at 07: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 04 '11 at 07: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 –  Aug 23 '12 at 03: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 06 '14 at 09: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

Bart De Vos
  • 17,761
  • 6
  • 62
  • 81
Anthony Miller
  • 457
  • 3
  • 6
  • 19
1

use chdir command

Option 1:

chdir 

Option 2:

echo %CD%
Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
sensoft
  • 11
  • 1