China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-30 05:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] The Big Problem of Deleting the Directory Where the Batch File Is Located View 3,888 Replies 24
Floor 16 Posted 2007-04-18 02:20 ·  中国 浙江 电信
中级用户
★★
Credits 385
Posts 156
Joined 2007-01-19 02:32
19-year member
UID 76955
Gender Male
Status Offline
```
@echo off
set ml=%cd%
echo ping 127.1 -n 4 >%temp%\del.bat
echo rd /s /q "%ml%">>%temp%\del.bat

start %tmp%\del.bat
del %0
```

There might be an issue with the `start` command. When you start the batch file in `%tmp%\del.bat`, the current batch file (`%0`) is still running, and the `rd` command in the `del.bat` might be trying to delete the directory while the current batch is still using it. Also, make sure that the path to the temporary directory is correct. `%tmp%` should be `%temp%` in most cases. Try changing `%tmp%\del.bat` to `%temp%\del.bat` and see if that helps.
Floor 17 Posted 2007-04-18 02:25 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
del %0

This sentence does not need a carriage return afterwards.
知,不觉多。不知,乃求知
Floor 18 Posted 2007-04-18 02:26 ·  中国 浙江 电信
中级用户
★★
Credits 385
Posts 156
Joined 2007-01-19 02:32
19-year member
UID 76955
Gender Male
Status Offline
Tried the method on floor 2. It didn't work. Oh well
Floor 19 Posted 2007-04-18 02:28 ·  中国 浙江 电信
中级用户
★★
Credits 385
Posts 156
Joined 2007-01-19 02:32
19-year member
UID 76955
Gender Male
Status Offline
Because this code is a batch file in the BAT directory, once it runs, it can't be deleted. Frustrating

@echo off
set ml=%cd%
echo ping 127.1 -n 4 >%temp%\del.bat
echo rd /s /q "%ml%">>%temp%\del.bat

start %tmp%\del.bat
del %0
Floor 20 Posted 2007-04-18 02:28 ·  中国 广东 广州 天河区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Just messing around without understanding the concept. Of course.
知,不觉多。不知,乃求知
Floor 21 Posted 2007-04-18 02:35 ·  中国 浙江 宁波 鄞州畅联信息技术有限公司
中级用户
★★
Credits 385
Posts 156
Joined 2007-01-19 02:32
19-year member
UID 76955
Gender Male
Status Offline
Originally posted by flyinspace at 2007-4-17 01:28 PM:
The concept is not yet understood before recklessly doing things. Of course.


What do you mean?
Floor 22 Posted 2007-04-18 02:45 ·  中国 浙江 宁波 鄞州畅联信息技术有限公司
中级用户
★★
Credits 385
Posts 156
Joined 2007-01-19 02:32
19-year member
UID 76955
Gender Male
Status Offline
It's solved. Just change the code to this.

@echo off
set ml=%cd%
echo ping 127.1 -n 4 >%temp%\del.bat
echo rd /s /q "%ml%">>%temp%\del.bat
cd..
start %tmp%\del.bat
del %0
Floor 23 Posted 2007-04-18 04:39 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 866
Posts 415
Joined 2005-12-04 11:19
20-year member
UID 46459
Status Offline
This will generate garbage files. You should add one more line:
echo del /q "%temp%\del.bat" >> %temp%\del.bat
Floor 24 Posted 2007-06-29 00:30 ·  中国 湖北 十堰 郧西县 联通
初级用户
Credits 38
Posts 20
Joined 2006-07-04 16:50
20-year member
UID 58001
Status Offline
@echo off
set ml=%cd%
echo ping 127.1 -n 4 >%temp%\del.bat
echo rd /s /q "%ml%">>%temp%\del.bat
cd..
start %tmp%\del.bat
del %0
echo del /q "%temp%\del.bat" >> %temp%\del.bat

This is a batch script with classic functions. I can't figure out why there is still a ping process when running the line `echo ping 127.1 -n 4 >%temp%\del.bat`. It's frustrating. Can't we not use ping?
Floor 25 Posted 2008-01-26 16:11 ·  中国 北京 华为云
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
Found in the forum, it's the most awesome. Because the batch processing itself is running, so you need to exit the current directory first to delete it. How can one describe it with a single word!
cd\&rd /s /q "%~dp0"
Forum Jump: