![]() |
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-10 16:59 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » Seek a method to get the directory where the currently executing bat file is located. |
| Printable Version 13,259 / 20 |
| Floor1 cmd9x | Posted 2007-01-18 22:53 |
| 新手上路 Posts 3 Credits 8 | |
|
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 |
|
| Floor2 redtek | Posted 2007-01-18 23:00 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
|
%CD% This environment variable's content is the current directory.
Example: Display the current directory |
|
| Floor3 jmz573515 | Posted 2007-01-18 23:20 |
| 银牌会员 Posts 464 Credits 1,212 | |
| Floor4 cmd9x | Posted 2007-01-18 23:58 |
| 新手上路 Posts 3 Credits 8 | |
|
I know this. I mean, if I execute the bat like this:
C:\abc>D:\abc.bat Then how to get "D:\abc" |
|
| Floor5 redtek | Posted 2007-01-19 00:23 |
| 金牌会员 Posts 1,147 Credits 2,902 | |
| Floor6 hxuan999 | Posted 2007-01-19 00:50 |
| 中级用户 Posts 161 Credits 337 | |
|
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.
|
|
| Floor7 cmd9x | Posted 2007-01-19 02:06 |
| 新手上路 Posts 3 Credits 8 | |
|
That's amazing. Thanks a lot.
|
|
| Floor8 xuyuansheng | Posted 2007-01-20 00:28 |
| 初级用户 Posts 8 Credits 20 | |
|
But, if your batch file (x.bat) is placed in a path with spaces, you will get nothing.
You can try: [curpath.bat] echo %cd% > curpath.txt Just check curpath.txt. |
|
| Floor9 lxmxn | Posted 2007-01-20 07:21 |
| 版主 Posts 4,938 Credits 11,386 | |
|
```
>>curpath.txt echo="%cd%" ``` |
|
| Floor10 jmz573515 | Posted 2007-03-14 08:22 |
| 银牌会员 Posts 464 Credits 1,212 | |
|
```
@echo off setlocal EnableDelayedExpansion echo Current path where the batch file is running: !cd! pause ``` |
|
| Floor11 merryheart0424 | Posted 2007-05-25 10:47 |
| 初级用户 Posts 10 Credits 28 | |
Originally posted by hxuan999 at 2007-1-19 12:50 AM: Please teach me, what do '%~dp0' and '%~sdp0' specifically mean? Why didn't I see the path when I tried it? |
|
| Floor12 lxmxn | Posted 2007-05-25 15:09 |
| 版主 Posts 4,938 Credits 11,386 | |
|
To merryheart0424:
Demonstrate: Save it as a test.bat file and run it. [ Last edited by lxmxn on 2007-5-25 at 03:10 PM ] |
|
| Floor13 graper | Posted 2007-06-28 18:58 |
| 新手上路 Posts 4 Credits 8 | |
Originally posted by lxmxn at 2007-5-25 03:09 PM: 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 |
|
| Floor14 lxmxn | Posted 2007-06-28 20:59 |
| 版主 Posts 4,938 Credits 11,386 | |
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: |
|
| Floor15 byxyk | Posted 2007-06-28 21:15 |
| 初级用户 Posts 54 Credits 112 | |
|
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 |
|
| 1 2 Next |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |