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-07-02 13:19
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » How to use %cd% to display the root directory where the batch file is located? View 1,744 Replies 7
Original Poster Posted 2008-06-21 16:37 ·  中国 北京 丰台区 联通
新手上路
Credits 10
Posts 4
Joined 2007-05-28 20:41
19-year member
UID 89594
Gender Male
Status Offline
I remember someone said before that it seems that by taking the first 3 characters of the variable %cd%, there is a single command for that.
强大的DOS......

아자 아자 화이팅! Fighting !
Floor 2 Posted 2008-06-22 09:33 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
%CD% does not necessarily represent the directory where the batch file is located, but rather the current directory when you run this batch file. Suppose like this:

D:\DOS> c:\batch\my.bat

Then %CD% represents the d:\dos directory, while the directory where my.bat is located is c:\batch.

If in cmd, you can use %~dp0 to represent the drive letter where the batch file is located.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 3 Posted 2008-06-22 22:41 ·  中国 北京 丰台区 联通
新手上路
Credits 10
Posts 4
Joined 2007-05-28 20:41
19-year member
UID 89594
Gender Male
Status Offline
Thanks for the previous answer, but I misunderstood it.

Can this effect be achieved?

When this bat file is run, it will echo the drive letter where this bat file is located.

Someone said before that it can be done with one command
强大的DOS......

아자 아자 화이팅! Fighting !
Floor 4 Posted 2008-06-23 08:52 ·  中国 江苏 苏州 昆山市 移动
初级用户
Credits 27
Posts 13
Joined 2008-06-21 09:52
18-year member
UID 120670
Gender Male
Status Offline
@echo off
echo Current path is: %cd%
echo Current drive letter is: %cd:~0,1%
pause
Floor 5 Posted 2008-06-24 00:01 ·  中国 北京 丰台区 联通
新手上路
Credits 10
Posts 4
Joined 2007-05-28 20:41
19-year member
UID 89594
Gender Male
Status Offline
Thanks for the solution from the user above XD~
强大的DOS......

아자 아자 화이팅! Fighting !
Floor 6 Posted 2008-06-24 00:09 ·  中国 江苏 苏州 电信
高级用户
★★
五星老土
Credits 558
Posts 172
Joined 2003-02-09 00:00
23-year member
UID 881
Gender Male
From 江苏
Status Offline
Originally posted by xlrtx at 2008-6-24 12:01 AM:
Thanks to the upstairs for the answer XD~


Actually, the person on the 2nd floor is correct, he didn't misinterpret.

And the person on the 4th floor is not completely correct, although in most cases the result is the same.

So, I need to remind you that the people you should thank the most are the person on the 2nd floor and me.
我怎么找不到一个比我注册日期早的人? 难道我是传说中的超级管理员? 其实我只是个潜水冠军而已.
Floor 7 Posted 2008-06-24 12:32 ·  中国 河北 保定 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Seeing a problem shouldn't rely on imagination; only practice can yield true knowledge.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 8 Posted 2008-06-24 23:15 ·  中国 上海 松江区 电信
铂金会员
★★★★
DOS一根葱
Credits 5,493
Posts 2,315
Joined 2006-05-01 10:41
20-year member
UID 54766
Gender Male
From 上海
Status Offline
```
@echo off
echo %~d0
pause
```
Details refer to: content on the 5th return trip in for /?
第一高手 第二高手

Forum Jump: