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 06:07
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] The Big Problem of Deleting the Directory Where the Batch File Is Located View 3,890 Replies 24
Original Poster Posted 2007-04-17 22:45 ·  中国 浙江 电信
中级用户
★★
Credits 385
Posts 156
Joined 2007-01-19 02:32
19-year member
UID 76955
Gender Male
Status Offline
For example, the batch file del.bat is in the directory C:\Documents and Settings\Administrator\Desktop\Bat\.

I want to delete the Bat folder and its own del.bat file.

Because del.bat itself is running, so the folder cannot be deleted. Hey

I wonder if there are experts who can help achieve this!


____________________The above has been resolved—————————————————



 

[ Last edited by heicai on 2007-4-17 at 01:46 PM ]
Floor 2 Posted 2007-04-17 23:33 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
The method can be arbitrary.

It doesn't have to be limited to creating files locally.

You can create a new del file in a temporary file.

The content is as follows:
cscript /nologo delay.vbs
rd /s /q "C:\Documents and Settings\Administrator\桌面\Bat"
del %0
Add the following statement at the end of the del file
echo WSCRIPT.sleep(5000)>delay.vbs
start %tmp%\del.bat
知,不觉多。不知,乃求知
Floor 3 Posted 2007-04-18 00:14 ·  中国 安徽 蚌埠 电信
中级用户
★★
Credits 292
Posts 137
Joined 2007-04-03 01:59
19-year member
UID 83758
Gender Male
Status Offline
Take D:/1 as an example. Yours is too long, so I won't make it long. ; )

@echo off
echo rmdir D:\1 /s /q >c:\1.bat
echo del c:\1.bat >>c:\1.bat
echo Windows Registry Editor Version 5.00>r.dll
echo >>r.dll
echo "Del"="C:\\1.bat">>r.dll
regedit -s r.dll
echo ☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
echo ★ ☆
echo ☆ The folder will be deleted after restart or logoff! ★
echo ★ ---------Press any key to exit!!---------- ☆
echo ☆ ★
echo ★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆
pause
del r.dll
del d:\1\1.bat

Just for fun. I didn't figure out how to immediately delete the running folder, but I came up with a way to completely delete all generated files and the target folder after logoff or restart.

[ Last edited by bw070 on 2007-4-17 at 11:19 AM ]
Floor 4 Posted 2007-04-18 00:34 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Delete after terminating the explorer process
Floor 5 Posted 2007-04-18 01:07 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Oh, I'm speechless. There are simple methods available, but all are used in a really complicated way.
知,不觉多。不知,乃求知
Floor 6 Posted 2007-04-18 01:12 ·  中国 浙江 电信
中级用户
★★
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 10:33 AM:
The method can be arbitrary.

It doesn't have to be limited to creating files locally.

You can create a new del file in a temporary file.

The content is as follows:
cscript /nologo delay.vbs
rd /s /q "C: ...

Thank you for the method. I tried it and it works well!

But I have another question. If the bat folder is a random and undetermined name, is there a way to obtain the name of this folder?
Floor 7 Posted 2007-04-18 01:16 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
There is there,
But at least you need to know where this folder is, right?

Otherwise, if you suddenly find a folder with many bat files and then delete them all at once. Wouldn't that be frustrating?

Or know the file name of the bat to be deleted. The drive letter where it is located is also okay.

But... if there are more than one, manual judgment is still needed.
知,不觉多。不知,乃求知
Floor 8 Posted 2007-04-18 01:19 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
The directory where the batch file is located is "%~dp0"
Floor 9 Posted 2007-04-18 01:20 ·  中国 浙江 电信
中级用户
★★
Credits 385
Posts 156
Joined 2007-01-19 02:32
19-year member
UID 76955
Gender Male
Status Offline
The function I want to implement is like this:

First, copy the bat folder into each of the folders such as a1, b2, c3, d4, haha, test, temp, etc. on the desktop respectively. Then do some work and delete all those folders.

C:\Documents and Settings\Administrator\Desktop\temp\
It would be great if I could get the folder at this temp position set as an environment variable. I don't know how to intercept this part of the folder name.
Floor 10 Posted 2007-04-18 01:25 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
You're talking about that first it's best to sort out your thoughts, so that everyone can help you. Is the work of you copying to various directories done by a script or manually? If it's a script, you can establish the deletion list at this time; after you finish copying, are the operations performed on each directory started manually or by the previous copy script? If it's started by a script, then the deletion list you mentioned earlier can exist in the form of variables. Then comes your deletion requirements. Is it a one-time deletion at the end or deletion one by one in accordance with the work order? If it's in accordance with the work order, then what I said earlier is basically useless, heh heh
Floor 11 Posted 2007-04-18 01:27 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
set is where you check environment variables. It seems like there are things you need in it... I don't quite understand what you mean.
知,不觉多。不知,乃求知
Floor 12 Posted 2007-04-18 01:30 ·  中国 浙江 电信
中级用户
★★
Credits 385
Posts 156
Joined 2007-01-19 02:32
19-year member
UID 76955
Gender Male
Status Offline
Originally posted by everest79 at 2007-4-17 12:25 PM:
You'd better organize your thoughts first when stating your requirements, so that everyone can help you better. Is the work of copying to various directories done by a script or manually? If it's a script, you can establish the deletion column at this time...

The batch processing in each folder is to delete that folder after clicking.
Floor 13 Posted 2007-04-18 01:35 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
Then the problem returns to the 8th floor. You can complete it by combining the method of the 2nd floor. You can use "%~dp0" as a parameter to start del.bat
Floor 14 Posted 2007-04-18 01:41 ·  中国 浙江 电信
中级用户
★★
Credits 385
Posts 156
Joined 2007-01-19 02:32
19-year member
UID 76955
Gender Male
Status Offline
After excavating posts in the forum, I finally found the answer! Also, I am very grateful to these friends who answered my question. Because my level is low, today's points are full, sorry

@echo off
for %%p in ("%cd%") do set temp=%%~nxp
echo %temp%
pause



Just don't know what %%~nxp means?

[ Last edited by heicai on 2007-4-17 at 12:42 PM ]
Floor 15 Posted 2007-04-18 01:45 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Actually, I think the method on floor 10 is quite good.

rem Create copy and delete lists, give an example.
echo %userprofile%\Desktop\temp>>del.txt
::... You finish writing this list yourself.
for /f "tokens=* " %%i in (del.txt) do (
rem Prevent accidental deletion of files
if exist "%%i" (
xcopy "your file" "%%i"
start "%%i\file name"
)
)
for /f "tokens=* " %%i in (del.txt) do (
dir /b /s "%%i" | find /i "the file you copied"
if not errorlevel 1 echo "%%i\the file you copied"
)
This script has not been debugged. When actually using it, if the last echo is correct, it can be changed to rd /s /q
知,不觉多。不知,乃求知
Forum Jump: