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-08-05 09:13
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved] How to make a self-extracting file delete itself View 1,979 Replies 10
Original Poster Posted 2007-07-07 21:43 ·  中国 湖北 武汉 电信
初级用户
小子
Credits 76
Posts 26
Joined 2007-06-20 19:30
19-year member
UID 91913
Gender Male
Status Offline
**Purpose**: Automatically delete 1.exe after self-extraction

**Process**: I created a self-extracting file (such as 1.exe), which only contains a 1.bat. The self-extraction directory is in %systemroot%. I wanted to automatically delete 1.exe after extraction, so I added del /q/f 1.exe. As a result, I couldn't find the path of 1.exe.

Attempt 1: Set the self-extraction directory to. \, which is the current directory. In this way, 1.exe can be automatically deleted, but there will be a certain display delay. This situation is not discussed for the time being;

Attempt 2: Create a 1.bat (content: @echo %cd% > c:\cpath.txt), set it as "Run before extraction", then create a 2.bat
@for /f "tokens=*" %%i in (c:\cpath.txt) do set cpath=%%i
@del /q /f "%cpath%\1.exe"
As a result, the read "%cpath%" is the path where %systemroot% is located, and it failed, heh heh

Attempt 3: Create a 1.bat and 2.bat. Create a self-extracting file 2.exe for 2.bat, and set the extraction directory to %systemroot%; create a self-extracting file 1.exe together with 1.bat and 2.exe, and set the extraction directory to the current directory. The original idea was: use 1.bat to extract the current path and copy 2.exe to the %systemroot% directory, then run 2.exe to delete the generated files. Just when deleting 1.exe, the "Access denied." always appears, and I can't get over this hurdle. I still want everyone to help give some advice, thank you.

**Solution**: The method is like "Attempt 3". The contents of the two files are as follows:
1.bat
@echo off
echo %cd% > c:\windows\cpath.txt
start "hehe" "%cd%\2.exe" >nul
del /q /f %0 >nul

2.bat
@echo off
@for /f "tokens=*" %%i in (c:\windows\cpath.txt) do set cpath=%%i
set "cpath=%cpath:~0,-1%"
taskkill /f /im 1.exe >nul
taskkill /f /im 2.exe >nul
del /f /q c:\windows\cpath.txt >nul
del /f /q "%cpath%"\1.exe >nul
del /f /q "%cpath%"\2.exe >nul
del /f /q %0 >nul
:)

The code is still a bit messy. I hope everyone doesn't mind. Also, thank you to the brother on floor 9 for the advice.
This code was tested and passed under xp+sp2.

[ Last edited by tclgb on 2007-7-10 at 10:36 PM ]
Floor 2 Posted 2007-07-07 21:50 ·  中国 安徽 马鞍山 电信
金牌会员
★★★★
Credits 3,946
Posts 1,884
Joined 2006-01-20 13:00
20-year member
UID 49283
Gender Male
Status Offline
It seems that there is an option in WinRAR to delete the self - contained compressed file.
Windows 一键还原
http://www.yjhy.com
Floor 3 Posted 2007-07-07 22:00 ·  中国 湖北 武汉 电信
初级用户
小子
Credits 76
Posts 26
Joined 2007-06-20 19:30
19-year member
UID 91913
Gender Male
Status Offline
Thank you lianjiang2004, can you be more specific?
Floor 4 Posted 2007-07-08 01:30 ·  中国 广东 汕头 潮阳区 电信
高级用户
★★★
DOS时空
Credits 713
Posts 348
Joined 2007-02-10 13:59
19-year member
UID 79169
Gender Male
Status Offline
Is to delete its own file

[ Last edited by haiou327 on 2007-7-8 at 01:32 AM ]
Attachments
1exe.JPG
Floor 5 Posted 2007-07-08 01:31 ·  中国 广东 汕头 潮阳区 电信
高级用户
★★★
DOS时空
Credits 713
Posts 348
Joined 2007-02-10 13:59
19-year member
UID 79169
Gender Male
Status Offline
Add DEL 1.EXE after BAT, and also add DEL %0,
Use the Phantom mode of QBFC.

[ Last edited by haiou327 on 2007-7-8 at 01:36 AM ]
Floor 6 Posted 2007-07-08 02:58 ·  中国 湖北 武汉 电信
初级用户
小子
Credits 76
Posts 26
Joined 2007-06-20 19:30
19-year member
UID 91913
Gender Male
Status Offline
re

Originally posted by haiou327 at 2007-7-8 01:31:
Add DEL 1.EXE after the BAT, and also add DEL %0,
Use QBFC's Phantom mode.

[ Last edited by haiou327 on 2007-7-8 at 01:36 AM ]



Adding DEL %0 is to delete the bat itself, or can't delete the self-extracting file, heh heh

Thanks
Floor 7 Posted 2007-07-09 00:23 ·  中国 湖北 武汉 电信
初级用户
小子
Credits 76
Posts 26
Joined 2007-06-20 19:30
19-year member
UID 91913
Gender Male
Status Offline
Still hope brothers can give some suggestions, thank you thank you thank you
Floor 8 Posted 2007-07-09 01:27 ·  中国 广东 汕头 潮阳区 电信
高级用户
★★★
DOS时空
Credits 713
Posts 348
Joined 2007-02-10 13:59
19-year member
UID 79169
Gender Male
Status Offline
Add DEL/q [self-extracting file path and file name] at the end of the batch processing.
Floor 9 Posted 2007-07-10 12:09 ·  中国 广东 广州 海珠区 电信
银牌会员
★★★
Credits 1,206
Posts 517
Joined 2007-03-25 01:18
19-year member
UID 82819
Gender Male
Status Offline
Tips:
To delete an executable file,
this file must not be running in the Windows system (the one that is executing cannot be deleted)

This problem can be solved by using delay instructions + writing code.
知,不觉多。不知,乃求知
Floor 10 Posted 2007-07-10 22:38 ·  中国 湖北 武汉 电信
初级用户
小子
Credits 76
Posts 26
Joined 2007-06-20 19:30
19-year member
UID 91913
Gender Male
Status Offline
After testing, the delay does not have the expected effect. I don't know if it's due to the local machine. I have to use taskkill.
Floor 11 Posted 2007-07-13 12:08 ·  中国 广东 佛山 电信
初级用户
★★
Credits 193
Posts 98
Joined 2007-01-17 11:56
19-year member
UID 76803
Gender Male
Status Offline
A friend named 10 had a fresh idea. He used taskkill to end the WinRAR process and reversed to solve this problem. Nice! Add points.
这家伙很聪明 什么都没留下
Forum Jump: