|
cmd9x
新手上路

积分 8
发帖 3
注册 2007-1-18
状态 离线
|
『楼 主』:
求一个能取当前执行的bat文件所在目录方法。
使用 LLM 解释/回答一下
因为bat文件需要执行一个跟它在一个目录下的某个文件,但是bat文件的位置是不确定的,原来写成这样:
SET CurPath=%0
IF NOT "%CurPath%"=="" set CurPath=%CurPath:~0,-9%
echo %CurPath%
但是有时执行时是对的,有时得到的结果却是错的,要么前后都多一个字符,要不就都少一个字符。
谁能帮帮心,多谢了
Because the bat file needs to execute a certain file in the same directory, but the location of the bat file is uncertain. Originally written like this:
SET CurPath=%0
IF NOT "%CurPath%"=="" set CurPath=%CurPath:~0,-9%
echo %CurPath%
But sometimes it is correct when executed, and sometimes the result is wrong. Either there is one more character before and after, or one less character.
Who can help, thank you very much
|
|
2007-1-18 22:53 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
   『第 2 楼』:
使用 LLM 解释/回答一下
%CD% 这个环境变量内容就是当前所在目录。
例:显示当前目录
@echo off
echo 当前目录是:%cd%
pause
%CD% This environment variable's content is the current directory.
Example: Display the current directory
@echo off
echo The current directory is: %cd%
pause
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2007-1-18 23:00 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
|
2007-1-18 23:20 |
|
|
cmd9x
新手上路

积分 8
发帖 3
注册 2007-1-18
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
这个我知道了,我是想说,如果我是这样执行bat
C:\abc>D:\abc.bat
那么怎么取到"D:\abc"
I know this. I mean, if I execute the bat like this:
C:\abc>D:\abc.bat
Then how to get "D:\abc"
|
|
2007-1-18 23:58 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
@echo off
:: set "abc=%cd%"
echo 当前正在运行的批处理文件所在路径:%~dp0
pause
@echo off
:: set "abc=%cd%"
echo The current path where the batch file is running: %~dp0
pause
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2007-1-19 00:23 |
|
|
hxuan999
中级用户
   DOS之日
积分 337
发帖 161
注册 2006-11-4
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
'%~dp0'所得到的路径中最后一个字符总是\,所有用的时候要注意了.比如调用它下面的abc.bat就要写成'%~dp0abc.bat',
另外你并不知道路径中有无空格,所以最好用双引号引起来,或是写成'%~sdp0'用路径的短名形式.
The path obtained from '%~dp0' always has a \ as the last character, so you need to be careful when using it. For example, to call the abc.bat under it, you should write it as '%~dp0abc.bat'. Also, you don't know if there are spaces in the path, so it's best to enclose it in double quotes, or write it as '%~sdp0' in the short name form of the path.
|

for /f %%h in (`echo hxuan`) do for /f %%x in (`echo hxuan`) do if %%h==%%x nul |
|
2007-1-19 00:50 |
|
|
cmd9x
新手上路

积分 8
发帖 3
注册 2007-1-18
状态 离线
|
|
2007-1-19 02:06 |
|
|
xuyuansheng
初级用户
 
积分 20
发帖 8
注册 2007-1-17
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
但,如果你的这个批处理文件(x.bat)放到一个含有空格的路径中,将什么也得不到。
可以试试:
[curpath.bat]
echo %cd% > curpath.txt
查看curpath.txt就可以了。
But, if your batch file (x.bat) is placed in a path with spaces, you will get nothing.
You can try:
echo %cd% > curpath.txt
Just check curpath.txt.
|
|
2007-1-20 00:28 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
>>curpath.txt echo="%cd%"
```
>>curpath.txt echo="%cd%"
```
|
|
2007-1-20 07:21 |
|
|
jmz573515
银牌会员
    
积分 1212
发帖 464
注册 2006-12-13
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
@echo off
setlocal EnableDelayedExpansion
echo 当前正在运行的批处理文件所在路径:!cd!
pause
```
@echo off
setlocal EnableDelayedExpansion
echo Current path where the batch file is running: !cd!
pause
```
|
|
2007-3-14 08:22 |
|
|
merryheart0424
初级用户
 
积分 28
发帖 10
注册 2007-4-25
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Originally posted by hxuan999 at 2007-1-19 12:50 AM:
'%~dp0'所得到的路径中最后一个字符总是\,所有用的时候要注意了.比如调用它下面的abc.bat就要写成'%~dp0abc.bat',
另外你并不知道路径中有无空格,所以最好用双引号引起来,或是写成'%~sdp0'用路径的短名形式.
请教一下,'%~dp0' '%~sdp0' 具体是什么意思? 怎么我输入试了一下没有显示路径呢?
Originally posted by hxuan999 at 2007-1-19 12:50 AM:
The path obtained by '%~dp0' always ends with \, so you need to be careful when using it. For example, to call abc.bat under it, you should write it as '%~dp0abc.bat'.
In addition, you don't know if there are spaces in the path, so it's best to enclose it in double quotes, or write it as '%~sdp0' to use the short name form of the path.
Please teach me, what do '%~dp0' and '%~sdp0' specifically mean? Why didn't I see the path when I tried it?
|
|
2007-5-25 10:47 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
To merryheart0424:
演示一下:
@echo off
echo 当前的盘符及路径:%~dp0
echo 当前的盘符及路径的短文件名格式:%~sdp0
pause
保存为test.bat文件运行.
Last edited by lxmxn on 2007-5-25 at 03:10 PM ]
To merryheart0424:
Demonstrate:
@echo off
echo Current drive letter and path: %~dp0
echo Current drive letter and path in short filename format: %~sdp0
pause
Save it as a test.bat file and run it.
Last edited by lxmxn on 2007-5-25 at 03:10 PM ]
|
|
2007-5-25 15:09 |
|
|
graper
新手上路

积分 8
发帖 4
注册 2007-3-24
状态 离线
|
『第 13 楼』:
如何只取当前文件的上级目录名称
使用 LLM 解释/回答一下
Originally posted by lxmxn at 2007-5-25 03:09 PM:
To merryheart0424:
演示一下:
@echo off
echo 当前的盘符及路径:%~dp0
echo 当前的盘符及路径的短文件名格式:%~sdp0
pause
保存为test.bat文件运行 ...
上面这个直接以BAT方式运行可以工作,但如果用工具编译成EXE文件就不行了,路径会变成系统临时目录的路径:(
别外如果我只想取当前文件的上级目录名称如何写呢?
比如当前文件为C:\AAAA\BBB\TEST.BAT
我只要取得BBB这个目录名称
先谢谢了
Originally posted by lxmxn at 2007-5-25 03:09 PM:
To merryheart0424:
Demonstrate:
@echo off
echo Current drive letter and path: %~dp0
echo Current drive letter and path in short filename format: %~sdp0
pause
Save as test.bat and run...
The above works directly when run as a BAT file, but if compiled into an EXE file using a tool, the path will become the path of the system temporary directory :(
Also, if I only want to get the name of the parent directory of the current file, how to write it?
For example, the current file is C:\AAAA\BBB\TEST.BAT
I just want to get the directory name BBB
Thanks in advance
|
|
2007-6-28 18:58 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
  『第 14 楼』:
使用 LLM 解释/回答一下
上面这个直接以BAT方式运行可以工作,但如果用工具编译成EXE文件就不行了,路径会变成系统临时目录的路径:(
编译bat文件为exe文件的工具是死的东西,稍微把批处理变换一下就会出问题,似乎批处理的很多扩展功能,编译工具都完成的不是很好。
如果你只是为了迎合编译工具的要求,那就另当别论了。
取当前文件的上级目录的名称可以参考下面这个代码:
@echo off&SetLocal EnableDelayedExpansion
set a=D:\abc\def ghi\j k\lmn.bat
for %%a in ("%a%") do (
set ok=%%~dpa
for /f "delims=" %%b in ("!ok:~0,-1!") do (
echo %%~nb
)
)
pause
The above one can work when run directly in BAT mode, but it doesn't work when compiled into an EXE file with a tool. The path will become the path of the system temporary directory:(
The tool that compiles BAT files into EXE files is inflexible. A slight change to the batch script will cause problems. It seems that many extended functions of the batch script are not well completed by the compilation tool.
If you are just catering to the requirements of the compilation tool, that's another story.
For referring to getting the name of the parent directory of the current file, you can refer to the following code:
@echo off&SetLocal EnableDelayedExpansion
set a=D:\abc\def ghi\j k\lmn.bat
for %%a in ("%a%") do (
set ok=%%~dpa
for /f "delims=" %%b in ("!ok:~0,-1!") do (
echo %%~nb
)
)
pause
|
|
2007-6-28 20:59 |
|
|
byxyk
初级用户
 
积分 112
发帖 54
注册 2007-6-18
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
%0这个参数并不是一成不变的。
1.资源管理器中双击执行
2.CMD中全路径运行
3.CMD中当前目录下运行
The parameter %0 is not invariable.
1. Double-click to execute in the resource manager
2. Run with full path in CMD
3. Run in the current directory in CMD
|
|
2007-6-28 21:15 |
|