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-08-19 09:10
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » (Solved) How to get the current batch processing path under DOS? View 2,735 Replies 18
Original Poster Posted 2008-06-23 00:24 ·  中国 河南 郑州 电信
初级用户
Credits 35
Posts 14
Joined 2008-04-28 23:49
18-year member
UID 117220
Gender Male
Status Offline
How to get the current path where the batch file is located under DOS? I tried %~dp0 but it didn't work.
Thanks

[ Last edited by winlong102 on 2008-6-23 at 10:08 PM ]
Floor 2 Posted 2008-06-23 02:36 ·  中国 吉林 吉林市 电信
初级用户
夜语者
Credits 58
Posts 29
Joined 2007-04-22 11:37
19-year member
UID 86081
Gender Male
From 地狱十九重
Status Offline
冰我喜欢你


无论如何,坚守最后的良知!
thirteen.pdx.cn
Floor 3 Posted 2008-06-23 08:33 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
This, it seems that pure DOS does not provide similar functions.

fpath is a program to find the path of executable programs under pure DOS, but it seems only suitable for searching in the search path.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 4 Posted 2008-06-23 08:54 ·  中国 江苏 苏州 联通
初级用户
Credits 27
Posts 13
Joined 2008-06-21 09:52
18-year member
UID 120670
Gender Male
Status Offline
I don't know if that's what you mean? Try this to see if it meets your requirements:
@echo off
echo Current path is: %cd%
echo Current drive letter is: %cd:~0,1%
pause
Floor 5 Posted 2008-06-23 11:18 ·  中国 河南 郑州 电信
初级用户
Credits 35
Posts 14
Joined 2008-04-28 23:49
18-year member
UID 117220
Gender Male
Status Offline
Thanks to everyone above for the replies. I was talking about DOS, not cmd. Hehe. It should be that pure DOS didn't provide similar functions.
Floor 6 Posted 2008-06-23 18:07 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
With the help of third - party tools. For example, strings and so on
Windows 一键还原
http://www.yjhy.com
Floor 7 Posted 2008-06-23 21:57 ·  中国 河南 郑州 电信
初级用户
Credits 35
Posts 14
Joined 2008-04-28 23:49
18-year member
UID 117220
Gender Male
Status Offline
Hehe, thanks for the reply from lianjiang.
Floor 8 Posted 2008-06-23 22:27 ·  中国 河北 保定 移动
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Using strings only extracts the drive letter in the path, which is not equivalent to obtaining the running path of the batch file.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 9 Posted 2008-06-24 07:37 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
Originally posted by Climbing at 2008-6-23 22:27:
Using strings only extracts the drive letter in the path, which is not equal to obtaining the running path of the batch file.


Can extract both the drive letter and the directory.
Using both parameters can get the complete path.
Windows 一键还原
http://www.yjhy.com
Floor 10 Posted 2008-06-24 12:30 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
假设你的批处理文件是`c:\x.bat`,当前盘和当前目录是`d:\test`,当你运行`c:x.bat`时,要获取`x.bat`本身所在的路径,可以使用以下批处理代码:

```batch
@echo off
rem 获取当前批处理文件的完整路径
for %%i in (%0) do set "batPath=%%~fi"
echo %batPath%
```

解释一下,`%0`表示当前正在执行的批处理文件的名称及路径(相对路径),`%%~fi`会将`%0`扩展为完整的绝对路径,然后将其赋值给`batPath`变量,最后输出`batPath`就可以得到`x.bat`本身所在的路径了。So the translated text is:

Assume my batch file is c:\x.bat (the current directory of drive C is c:\), and my current drive and current directory are: d:\test. Then I run with the following command line:
c:x.bat.

How should your X.bat be written to get its own path?
I'm sure this can't be done with Strings.

The following is the code to get the path of the batch file itself:

```batch
@echo off
rem Get the full path of the current batch file
for %%i in (%0) do set "batPath=%%~fi"
echo %batPath%
```

Explanation: `%0` represents the name and path (relative path) of the currently executing batch file, `%%~fi` will expand `%0` to the complete absolute path, then assign it to the `batPath` variable, and finally output `batPath` to get the path where `x.bat` itself is located.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 11 Posted 2008-06-24 12:56 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
Originally posted by Climbing at 2008-6-24 12:30:
Suppose my batch file is c:\x.bat (the current directory of drive C is c:\), and my current drive and directory are: d:\test, then I run with the following command line:
c:x.bat.

How should your X.bat be...


Can be answered with certainty, yes.
The result is "c:\".

See the picture for the running result.



As long as you try strings yourself, you'll know. Hehe.
If it doesn't work after trying, I'll write those two commands.
Windows 一键还原
http://www.yjhy.com
Floor 12 Posted 2008-06-24 12:59 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
Originally posted by Climbing at 2008-6-24 12:30:
Suppose my batch file is c:\x.bat (the current directory on drive C is c:\), and my current drive and directory are: d:\test, then I run with the following command line:
c:x.bat.

How should your X.bat be ...


Quoting "Don't judge a problem by imagination, practice makes true knowledge." Hehe.
Windows 一键还原
http://www.yjhy.com
Floor 13 Posted 2008-06-24 13:39 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Hmm, I got it:
strings filedrive %0
strings filedir %0

Actually, you can also use: fpath %0
But to extract it, you also need to cooperate with other programs.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 14 Posted 2008-06-25 12:12 ·  中国 河南 郑州 电信
初级用户
Credits 35
Posts 14
Joined 2008-04-28 23:49
18-year member
UID 117220
Gender Male
Status Offline
My post has triggered a debate between two great experts. I feel quite accomplished.
Floor 15 Posted 2008-06-25 21:55 ·  中国 山西 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
It can be considered to not use strings respectively.
Respectively use
dir /s/b %0
and
for %%p in (%path%) do dir /b %%p.\%0
to detect, which should be able to get the path where the batch processing is located in most cases.

As for if the standard output is transferred and stored to the environment variable, there are many methods that can be used, so I won't discuss it much.
Forum Jump: