![]() |
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 10:50 |
47,814 topics / 349,908 posts / today 0 new / 48,259 members |
| DOS疑难解答 & 问题讨论 (解答室) » (Solved) How to get the current batch processing path under DOS? |
| Printable Version 2,744 / 18 |
| Floor1 winlong102 | Posted 2008-06-23 00:24 |
| 初级用户 Posts 14 Credits 35 | |
|
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 ] |
|
| Floor2 thirteen | Posted 2008-06-23 02:36 |
| 初级用户 Posts 29 Credits 58 From 地狱十九重 | |
| Floor3 Climbing | Posted 2008-06-23 08:33 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
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. |
|
| Floor4 lixin12345 | Posted 2008-06-23 08:54 |
| 初级用户 Posts 13 Credits 27 | |
|
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 |
|
| Floor5 winlong102 | Posted 2008-06-23 11:18 |
| 初级用户 Posts 14 Credits 35 | |
|
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.
|
|
| Floor6 lianjiang2004 | Posted 2008-06-23 18:07 |
| 金牌会员 Posts 1,884 Credits 3,946 | |
|
With the help of third - party tools. For example, strings and so on
|
|
| Floor7 winlong102 | Posted 2008-06-23 21:57 |
| 初级用户 Posts 14 Credits 35 | |
|
Hehe, thanks for the reply from lianjiang.
|
|
| Floor8 Climbing | Posted 2008-06-23 22:27 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
Using strings only extracts the drive letter in the path, which is not equivalent to obtaining the running path of the batch file.
|
|
| Floor9 lianjiang2004 | Posted 2008-06-24 07:37 |
| 金牌会员 Posts 1,884 Credits 3,946 | |
Originally posted by Climbing at 2008-6-23 22:27: Can extract both the drive letter and the directory. Using both parameters can get the complete path. |
|
| Floor10 Climbing | Posted 2008-06-24 12:30 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
假设你的批处理文件是`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. |
|
| Floor11 lianjiang2004 | Posted 2008-06-24 12:56 |
| 金牌会员 Posts 1,884 Credits 3,946 | |
Originally posted by Climbing at 2008-6-24 12:30: 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. |
|
| Floor12 lianjiang2004 | Posted 2008-06-24 12:59 |
| 金牌会员 Posts 1,884 Credits 3,946 | |
Originally posted by Climbing at 2008-6-24 12:30: Quoting "Don't judge a problem by imagination, practice makes true knowledge." Hehe. |
|
| Floor13 Climbing | Posted 2008-06-24 13:39 |
| 铂金会员 Posts 2,753 Credits 6,962 From 河北保定 | |
|
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. |
|
| Floor14 winlong102 | Posted 2008-06-25 12:12 |
| 初级用户 Posts 14 Credits 35 | |
|
My post has triggered a debate between two great experts. I feel quite accomplished.
|
|
| Floor15 qzwqzw | Posted 2008-06-25 21:55 |
| 银牌会员 Posts 636 Credits 2,343 | |
|
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. |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |