简体   繁体   中英

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.

  1. User downloads file.zip to any directory and unzips.
  2. User runs an exe which is located in the unzipped folder.
  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.

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.

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.

What lines do I need to edit and is it safe to kill dllhost.exe?

According to a link from 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. The code below looks for the problem dll and tries to delete it. If it still exists, it will try to delete the file every 30 seconds for up to 15 minutes. As soon as the dll gets deleted, the entire folder will also be deleted. If after 15 minutes the dll cannot be deleted, the remaining files in the folder will be deleted.

I still have a small issue. If I add code that kill/restarts Windows Explorer, the folder does not get deleted. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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