簡體   English   中英

刪除自身的批處理文件和包含該文件的文件夾

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

我正在嘗試執行以下操作,但是刪除包含批處理文件的下載文件夾失敗:注意:所有exe,應用程序,批處理文件等都包含在file.zip中。

  1. 用戶將file.zip下載到任何目錄並解壓縮。
  2. 用戶運行位於解壓縮文件夾中的exe。
  3. 依次運行兩個便攜式應用程序和其他一些東西。
  4. 執行完職責后,我進入遠程並運行相同的exe,但是這次我選擇一個選項,該選項運行一個批處理文件(位於解壓縮的文件夾中),該選項啟動30秒計時器,然后應該停止應用程序並刪除file.zip和解壓縮的文件夾,包括批處理文件本身。

下面是批處理文件:

@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

我遇到的問題是該文件夾永遠不會被刪除。 我意識到我的代碼不正確,但是另一個原因是因為dllhost.exe進程有時仍在使用解壓縮文件夾中的一個dll文件。

我不確定添加是否可以殺死dllhost.exe進程的行是否安全,但是我的代碼仍然無法正常工作,因為我在刪除批處理文件本身以及包含該批處理文件的文件夾時遇到了問題。

我需要編輯哪些行?殺死dllhost.exe是否安全?

根據dbenham的鏈接

這可以解決問題:

@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%"

注意刪除包含該批次的文件夾
包括任何其他文件/文件夾,沒有任何其他疑問!

好吧...我想,我想出了要通過嘗試刪除dll文件來完成我想做的事情的方法,首先是在嘗試刪除整個目錄之前。 下面的代碼查找有問題的dll,然后嘗試將其刪除。 如果仍然存在,它將嘗試每30秒刪除一次文件,最多15分鍾。 一旦dll被刪除,整個文件夾也將被刪除。 如果15分鍾后無法刪除該dll,則該文件夾中的其余文件將被刪除。

我還有一個小問題。 如果添加殺死/重新啟動Windows資源管理器的代碼,則不會刪除該文件夾。 為什么並且有解決方法?

下面是最新的代碼:

@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