44

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?

4 Answers4

71
%~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
  • 18,171
17

The current directory is held in %CD%

user9517
  • 117,122
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
  • 18,171
1

use chdir command

Option 1:

chdir 

Option 2:

echo %CD%
Michael Hampton
  • 252,907
sensoft
  • 11