China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-25 06:37
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Seek a method to get the directory where the currently executing bat file is located. View 13,072 Replies 20
Original Poster Posted 2007-01-18 22:53 ·  中国 广东 深圳 电信
新手上路
Credits 8
Posts 3
Joined 2007-01-18 21:59
19-year member
UID 76920
Gender Male
Status Offline
Because the bat file needs to execute a certain file in the same directory, but the location of the bat file is uncertain. Originally written like this:
SET CurPath=%0
IF NOT "%CurPath%"=="" set CurPath=%CurPath:~0,-9%
echo %CurPath%
But sometimes it is correct when executed, and sometimes the result is wrong. Either there is one more character before and after, or one less character.
Who can help, thank you very much
Floor 2 Posted 2007-01-18 23:00 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
%CD% This environment variable's content is the current directory.

Example: Display the current directory

@echo off

echo The current directory is: %cd%

pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
jmz573515 +4 2007-01-18 23:20
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 3 Posted 2007-01-18 23:20 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
Floor 4 Posted 2007-01-18 23:58 ·  中国 广东 深圳 电信
新手上路
Credits 8
Posts 3
Joined 2007-01-18 21:59
19-year member
UID 76920
Gender Male
Status Offline
I know this. I mean, if I execute the bat like this:
C:\abc>D:\abc.bat
Then how to get "D:\abc"
Floor 5 Posted 2007-01-19 00:23 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline

@echo off

:: set "abc=%cd%"
echo The current path where the batch file is running: %~dp0

pause

    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 6 Posted 2007-01-19 00:50
中级用户
★★
DOS之日
Credits 337
Posts 161
Joined 2006-11-04 05:27
19-year member
UID 69523
Gender Male
Status Offline
The path obtained from '%~dp0' always has a \ as the last character, so you need to be careful when using it. For example, to call the abc.bat under it, you should write it as '%~dp0abc.bat'. Also, you don't know if there are spaces in the path, so it's best to enclose it in double quotes, or write it as '%~sdp0' in the short name form of the path.
for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul
Floor 7 Posted 2007-01-19 02:06 ·  中国 广东 深圳 电信
新手上路
Credits 8
Posts 3
Joined 2007-01-18 21:59
19-year member
UID 76920
Gender Male
Status Offline
That's amazing. Thanks a lot.
Floor 8 Posted 2007-01-20 00:28 ·  中国 江苏 南京 电信
初级用户
Credits 20
Posts 8
Joined 2007-01-17 22:45
19-year member
UID 76829
Gender Male
Status Offline
But, if your batch file (x.bat) is placed in a path with spaces, you will get nothing.

You can try:
[curpath.bat]
echo %cd% > curpath.txt

Just check curpath.txt.
Floor 9 Posted 2007-01-20 07:21 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
```
>>curpath.txt echo="%cd%"
```
Floor 10 Posted 2007-03-14 08:22 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
```
@echo off
setlocal EnableDelayedExpansion
echo Current path where the batch file is running: !cd!
pause
```
Floor 11 Posted 2007-05-25 10:47 ·  中国 北京 联通
初级用户
Credits 28
Posts 10
Joined 2007-04-25 04:51
19-year member
UID 86377
Gender Female
Status Offline
Originally posted by hxuan999 at 2007-1-19 12:50 AM:
The path obtained by '%~dp0' always ends with \, so you need to be careful when using it. For example, to call abc.bat under it, you should write it as '%~dp0abc.bat'.
In addition, you don't know if there are spaces in the path, so it's best to enclose it in double quotes, or write it as '%~sdp0' to use the short name form of the path.


Please teach me, what do '%~dp0' and '%~sdp0' specifically mean? Why didn't I see the path when I tried it?
Floor 12 Posted 2007-05-25 15:09 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
To merryheart0424:

Demonstrate:

@echo off
echo Current drive letter and path: %~dp0
echo Current drive letter and path in short filename format: %~sdp0
pause

Save it as a test.bat file and run it.

[ Last edited by lxmxn on 2007-5-25 at 03:10 PM ]
Floor 13 Posted 2007-06-28 18:58 ·  中国 贵州 遵义 电信
新手上路
Credits 8
Posts 4
Joined 2007-03-24 06:00
19-year member
UID 82745
Gender Male
Status Offline
Originally posted by lxmxn at 2007-5-25 03:09 PM:
To merryheart0424:

Demonstrate:

@echo off
echo Current drive letter and path: %~dp0
echo Current drive letter and path in short filename format: %~sdp0
pause

Save as test.bat and run...


The above works directly when run as a BAT file, but if compiled into an EXE file using a tool, the path will become the path of the system temporary directory :(

Also, if I only want to get the name of the parent directory of the current file, how to write it?
For example, the current file is C:\AAAA\BBB\TEST.BAT
I just want to get the directory name BBB

Thanks in advance
Floor 14 Posted 2007-06-28 20:59 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
The above one can work when run directly in BAT mode, but it doesn't work when compiled into an EXE file with a tool. The path will become the path of the system temporary directory:(

The tool that compiles BAT files into EXE files is inflexible. A slight change to the batch script will cause problems. It seems that many extended functions of the batch script are not well completed by the compilation tool.

If you are just catering to the requirements of the compilation tool, that's another story.

For referring to getting the name of the parent directory of the current file, you can refer to the following code:
@echo off&SetLocal EnableDelayedExpansion
set a=D:\abc\def ghi\j k\lmn.bat
for %%a in ("%a%") do (
set ok=%%~dpa
for /f "delims=" %%b in ("!ok:~0,-1!") do (
echo %%~nb
)
)
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
26933062 +5 2007-09-28 19:18
Floor 15 Posted 2007-06-28 21:15 ·  中国 云南 玉溪 电信
初级用户
★★
Credits 112
Posts 54
Joined 2007-06-18 22:47
19-year member
UID 91747
Gender Male
Status Offline
The parameter %0 is not invariable.
1. Double-click to execute in the resource manager
2. Run with full path in CMD
3. Run in the current directory in CMD
Forum Jump: