中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-19 09:16
47,814 topics / 349,908 posts / today 0 new / 48,259 members
DOS疑难解答 & 问题讨论 (解答室) » How to use %cd% to display the root directory where the batch file is located?
Printable Version  1,793 / 7
Floor1 xlrtx Posted 2008-06-21 16:37
新手上路 Posts 4 Credits 10
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.
Floor2 Climbing Posted 2008-06-22 09:33
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
%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.
Floor3 xlrtx Posted 2008-06-22 22:41
新手上路 Posts 4 Credits 10
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
Floor4 lixin12345 Posted 2008-06-23 08:52
初级用户 Posts 13 Credits 27
@echo off
echo Current path is: %cd%
echo Current drive letter is: %cd:~0,1%
pause
Floor5 xlrtx Posted 2008-06-24 00:01
新手上路 Posts 4 Credits 10
Thanks for the solution from the user above XD~
Floor6 quya Posted 2008-06-24 00:09
高级用户 Posts 172 Credits 558 From 江苏
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.
Floor7 Climbing Posted 2008-06-24 12:32
铂金会员 Posts 2,753 Credits 6,962 From 河北保定
Seeing a problem shouldn't rely on imagination; only practice can yield true knowledge.
Floor8 fastslz Posted 2008-06-24 23:15
铂金会员 Posts 2,315 Credits 5,493 From 上海
```
@echo off
echo %~d0
pause
```
Details refer to: content on the 5th return trip in for /?
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023