简体   繁体   English

删除自身的批处理文件和包含该文件的文件夹

[英]Batch file that deletes itself and folder that contains it

I'm trying to do the following but deleting the downloaded folder which contains the batch file fails: NOTE: All exe's, apps, batch file etc. are contained in file.zip. 我正在尝试执行以下操作,但是删除包含批处理文件的下载文件夹失败:注意:所有exe,应用程序,批处理文件等都包含在file.zip中。

  1. User downloads file.zip to any directory and unzips. 用户将file.zip下载到任何目录并解压缩。
  2. User runs an exe which is located in the unzipped folder. 用户运行位于解压缩文件夹中的exe。
  3. This in turn runs two portable apps and some other things. 依次运行两个便携式应用程序和其他一些东西。
  4. Once duties are performed, I remote in and run the same exe but this time I select an option that runs a batch file (located in unzipped folder) that starts a 30 second timer then is supposed to stop the apps and delete file.zip and the unzipped folder including the batch file itself. 执行完职责后,我进入远程并运行相同的exe,但是这次我选择一个选项,该选项运行一个批处理文件(位于解压缩的文件夹中),该选项启动30秒计时器,然后应该停止应用程序并删除file.zip和解压缩的文件夹,包括批处理文件本身。

Below is the batch file: 下面是批处理文件:

@echo off
mode con: cols=32 lines=7
color 4f
title 
echo         30 Second Delay
echo      Close window to abort
echo/
echo/
echo 0%%                         100%%
SET /P var= <NUL

set count=0
:loop
  PING -n 2 127.0.0.1 >NUL 2>&1
  call :printline _
  set /a count=count+1
  if %count%==30 goto finish
goto loop

:printline
 REM Print text passed to sub without a carriage return.
 REM Sets line variable in case %1 intereferes with redirect
 set line=%1
 set /p var=%line%<NUL
exit /b

:finish
cls
color 0f
title Finished
mode con: cols=80 lines=25
echo Do NOT close this window!
echo/
echo Killing processes...
echo/
echo/
echo/

taskkill /t /f /im app1mainprocess.exe >nul
timeout /t 5 >nul
taskkill /t /f /im app2mainprocess.exe >nul

timeout /t 5 >nul

echo Do NOT close this window!
echo/
rem echo Restarting Windows Explorer...

rem timeout /t 10 >nul

rem taskkill /f /im explorer.exe >nul

rem start explorer.exe

echo Do NOT close this window!
echo/
echo Deleteing files and folders...
echo/

rem timeout /t 10 >nul

Set "Folder2Del=%~dp0"
cd ..
IF EXIST "file.zip" DEL "file.zip" /s /q >nul

rem echo %scrptDir%

echo Do NOT close this window!
echo/
echo Still working...
timeout /t 10 >nul
rd %Folder2Del% /s /q
(goto) 2>Nul & RD /S /Q "%Folder2Del%" & exit

The problem I encounter is that the folder never gets deleted. 我遇到的问题是该文件夹永远不会被删除。 I realize my code is not correct but another reason is because one of the dll files in the unzipped folder is sometimes still in use by the dllhost.exe process. 我意识到我的代码不正确,但是另一个原因是因为dllhost.exe进程有时仍在使用解压缩文件夹中的一个dll文件。

I'm not sure if it is safe to add a line that kills the dllhost.exe process or not but my code still won't work because I have something wrong with how it deletes the batch file itself and the folder that contains it. 我不确定添加是否可以杀死dllhost.exe进程的行是否安全,但是我的代码仍然无法正常工作,因为我在删除批处理文件本身以及包含该批处理文件的文件夹时遇到了问题。

What lines do I need to edit and is it safe to kill dllhost.exe? 我需要编辑哪些行?杀死dllhost.exe是否安全?

According to a link from dbenham 根据dbenham的链接

This does the trick: 这可以解决问题:

@Echo off
Echo Ref: "http://www.dostips.com/forum/viewtopic.php?f=3&t=6491"
Set "Folder2Del=%~dp0"
cd "%~d0"
pause
(goto) 2>Nul & RD /S /Q "%Folder2Del%"

Take care the folder containing the batch is deleted 注意删除包含该批次的文件夹
including any other files/folders without any further question! 包括任何其他文件/文件夹,没有任何其他疑问!

Ok... I THINK I figured out how to do what I want by trying to delete the dll file, first, before trying to delete the entire directory. 好吧...我想,我想出了要通过尝试删除dll文件来完成我想做的事情的方法,首先是在尝试删除整个目录之前。 The code below looks for the problem dll and tries to delete it. 下面的代码查找有问题的dll,然后尝试将其删除。 If it still exists, it will try to delete the file every 30 seconds for up to 15 minutes. 如果仍然存在,它将尝试每30秒删除一次文件,最多15分钟。 As soon as the dll gets deleted, the entire folder will also be deleted. 一旦dll被删除,整个文件夹也将被删除。 If after 15 minutes the dll cannot be deleted, the remaining files in the folder will be deleted. 如果15分钟后无法删除该dll,则该文件夹中的其余文件将被删除。

I still have a small issue. 我还有一个小问题。 If I add code that kill/restarts Windows Explorer, the folder does not get deleted. 如果添加杀死/重新启动Windows资源管理器的代码,则不会删除该文件夹。 Why and is there a workaround? 为什么并且有解决方法?

Below is the latest code: 下面是最新的代码:

@echo off
mode con: cols=32 lines=7
color 4f
title 
echo         30 Second Delay
echo      Close window to abort
echo/
echo/
echo 0%%                         100%%
SET /P var= <NUL

set count=0
:loop
  PING -n 2 127.0.0.1 >NUL 2>&1
  call :printline _
  set /a count=count+1
  if %count%==30 goto finish
goto loop

:printline
 REM Print text passed to sub without a carriage return.
 REM Sets line variable in case %1 intereferes with redirect
 set line=%1
 set /p var=%line%<NUL
exit /b

:finish
cls
color 0f
title Uninstall
mode con: cols=80 lines=25
echo Do NOT close this window!
echo/
echo Killing processes...

tasklist /fi "imagename eq app1mainprocess.exe" |find ":" > nul
if errorlevel 1 taskkill /t /f /im "app1mainprocess.exe" > nul

tasklist /fi "imagename eq app2mainprocess.exe" |find ":" > nul
if errorlevel 1 taskkill /t /f /im "app2mainprocess.exe" > nul

timeout /t 5 >nul

rem echo Do NOT close this window!
rem echo/
rem echo Restarting Windows Explorer...

rem timeout /t 10 >nul

rem taskkill /f /im explorer.exe >nul

rem start explorer.exe
echo/
echo Deleteing file.zip if it exists...

timeout /t 5 >nul

Set "Folder2Del=%~dp0"
cd ..
IF EXIST "file.zip" DEL "file.zip" /s /q >nul

rem echo %Folder2Del%

rem Loops for 30 times in 30 second intervals (Total 15 minutes) to confirm deletion. Loop will exit after 30 loops and move on if dll cannot be deleted.
for /l %%i in (1,1,30) do (
del "%Folder2Del%name*.dll"
if not exist "%Folder2Del%name*.dll" goto Folder2Del
echo/
echo File locked! May take up to 15 minutes to delete.
echo Will stop trying 15 minutes after first attempt.
timeout /t 30 >nul
)

:Folder2Del
echo/
echo Attempting to delete the Connector folder and it's contents...
timeout /t 5 >nul
rd "%~dp0" /s /q & exit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM