目的:自解压后自动删除1.exe
过程:我建了一个自解压的文件(如1.exe),里面仅有一个1.bat,自解压目录在%systemroot%
我想在解压后自动删除1.exe,所以加入了del /q/f 1.exe
结果,找不到1.exe的路径了
尝试一:把自解压目录设为.\也就是当前目录,这样可以自动删除1.exe,但会将有一定的显示延时,这种情况暂且不论;
尝试二:建一个1.bat(内容为:@echo %cd% > c:\cpath.txt),设为“解压前运行”,
再建一个2.bat
@for /f "tokens=*" %%i in (c:\cpath.txt) do set cpath=%%i
@del /q /f "%cpath%\1.exe"
结果"%cpath%读出来的是%systemroot%所在路径,失败了,呵呵
尝试三:建一个1.bat和2.bat,将2.bat建一个自解压文件2.exe,解压目录设
为%systemroot%;将1.bat 和2.exe一起再建一个自解压文件1.exe,
解压目录设为当前目录。原来的想法是:利用1.bat将解压出来的当前
路径并将2.exe复制到%systemroot%下,再运行2.exe删除产生的文
件。只是删除1.exe的时候老是会出现“拒绝访问。”,始终迈不过这个
坎。还是想请大家帮忙给点意见,谢谢。
解决方案:方法如"尝试三",两个文件的内容如下:
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
:)
代码还是有点乱,希望大家不要介意。也谢谢9楼兄弟的意见。
此代码在xp+sp2下测试通过。
Last edited by tclgb on 2007-7-10 at 10:36 PM ]
**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 ]