|
winlong102
初级用户
 
积分 35
发帖 14
注册 2008-4-28
状态 离线
|
『楼 主』:
(已解决)如何在dos下获得当前批处理所在的路径?
使用 LLM 解释/回答一下
如何在dos下获得当前批处理所在的路径?我试过 %~dp0无效。
谢谢
Last edited by winlong102 on 2008-6-23 at 10:08 PM ]
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 ]
|
|
2008-6-23 00:24 |
|
|
thirteen
初级用户
  夜语者
积分 58
发帖 29
注册 2007-4-22 来自 地狱十九重
状态 离线
|
|
2008-6-23 02:36 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
这个,似乎纯DOS没有提供类似的功能。
fpath是一个在纯DOS下找可执行程序路径的程序,但似乎只适合在搜索路径中查找。
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.
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2008-6-23 08:33 |
|
|
lixin12345
初级用户
 
积分 27
发帖 13
注册 2008-6-21
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
不知道你是不是这个意思?试试这个看看是否符合你的要求:
@echo off
echo 当前路径是:%cd%
echo 当前盘符是:%cd:~0,1%
pause
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
|
|
2008-6-23 08:54 |
|
|
winlong102
初级用户
 
积分 35
发帖 14
注册 2008-4-28
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
谢谢上面所有人的答复 我说得是dos 不是cmd,呵呵
应该 纯DOS没有提供类似的功能
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.
|
|
2008-6-23 11:18 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
|
2008-6-23 18:07 |
|
|
winlong102
初级用户
 
积分 35
发帖 14
注册 2008-4-28
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
呵呵 感谢 lianjiang答复。
Hehe, thanks for the reply from lianjiang.
|
|
2008-6-23 21:57 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
用strings只是提取路径中的盘符,并不等于能够得到批处理的运行路径。
Using strings only extracts the drive letter in the path, which is not equivalent to obtaining the running path of the batch file.
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2008-6-23 22:27 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
Originally posted by Climbing at 2008-6-23 22:27:
用strings只是提取路径中的盘符,并不等于能够得到批处理的运行路径。
既可提取盘符,也可提取目录。
两个参数一起用就可得到完整路径。
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 |
|
2008-6-24 07:37 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
假设我的批处理是c:\x.bat(C盘的当前目录是c:\),而我的当前盘及当前目录是:d:\test,那么我用下面的命令行运行:
c:x.bat。
请问,你的X.bat该如何写才能得到自己本身所在的路径?
我肯定这不是Strings能够做到的。
假设你的批处理文件是`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.
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2008-6-24 12:30 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by Climbing at 2008-6-24 12:30:
假设我的批处理是c:\x.bat(C盘的当前目录是c:\),而我的当前盘及当前目录是:d:\test,那么我用下面的命令行运行:
c:x.bat。
请问,你的X.bat该如何 ...
可以肯定地回答,可以。
结果就是"c:\"
运行结果看下图。
只要你自己试试strings就知道了。呵呵。
假如试过不行的话,我写出那两行命令。
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 |
|
2008-6-24 12:56 |
|
|
lianjiang2004
金牌会员
     
积分 3946
发帖 1884
注册 2006-1-20
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
Originally posted by Climbing at 2008-6-24 12:30:
假设我的批处理是c:\x.bat(C盘的当前目录是c:\),而我的当前盘及当前目录是:d:\test,那么我用下面的命令行运行:
c:x.bat。
请问,你的X.bat该如何 ...
引用 “看问题不能凭想象,实践才能出真知。 ”, 呵呵。
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 |
|
2008-6-24 12:59 |
|
|
Climbing
铂金会员
       网络独行侠
积分 6962
发帖 2753
注册 2003-4-16 来自 河北保定
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
嗯,我知道了:
strings filedrive %0
strings filedir %0
其实,还可以用:fpath %0
但要提取出来,也得配合其它的程序了。
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.
|

偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
|
|
2008-6-24 13:39 |
|
|
winlong102
初级用户
 
积分 35
发帖 14
注册 2008-4-28
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
我的帖子引发了两大高手的争论
我觉得很有成就感
My post has triggered a debate between two great experts. I feel quite accomplished.
|
|
2008-6-25 12:12 |
|
|
qzwqzw
银牌会员
     天的白色影子
积分 2343
发帖 636
注册 2004-3-6
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
可以考虑分别不用strings
分别用
dir /s/b %0
和
for %%p in (%path%) do dir /b %%p.\%0
探测
应该能得到绝大数情况下的批处理所在路径
至于如果将标准输出转存至环境变量
那可利用的方法有很多
就不多讨论了
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.
|
|
2008-6-25 21:55 |
|