0

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?

Jeff
  • 495
  • 1
  • 5
  • 18
  • You would need to be sure that you were only killing the `dllhost.exe` process which was invoked by one of your executable processes, if you've done that, I don't see any reason why you couldn't delete it after you've ended the invoking process. _Alternatively you might want to try using `TaskKill` with it's `/T` option, when you end the invoking process?_ – Compo Nov 05 '18 at 17:42
  • Killing the main process of each app seems to kill all app spawned processes but some system processes still lock a particular dll that belongs to one of the apps. That's why the folder cannot be deleted, sometimes. The dll remains locked for 5-10 minutes. I'm not sure how to work around this. Some of those system processes cannot be killed and probably shouldn't, anyway. As I mentioned in a comment below, killing/restarting explorer seems to make deletion of that folder fail as well. Does anyone know why? – Jeff Nov 05 '18 at 21:58
  • I updated the code above to reflect the latest edits. Thanks for pointing out about `null` and `nul`. – Jeff Nov 05 '18 at 21:58

2 Answers2

1

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!

  • @Mofi if you see obvious typos, feel free to edit them. –  Nov 05 '18 at 20:02
  • Thanks but I had already tried this. It does not work either. Just like with my code above, everything in the folder and the batch file gets deleted but the folder remains. – Jeff Nov 05 '18 at 20:42
  • The above batch proves the concept works. Seems some program originating from the folder is still active and prevents the deletion. –  Nov 05 '18 at 20:49
  • The `cd /D` might have been a bad decision because if it was on a different drive it doesn't change the current folder on that drive, so see changed batch. –  Nov 05 '18 at 21:02
  • I need to delete the folder and the zip file. That's why I used `cd ..`. I'm wondering if killing and starting explorer.exe is part of the issue. If I `REM` out the code that kills/starts explorer, the folder gets deleted sometimes. When it does not get deleted, it's because one of the dll's associated with one of the apps is still held on to by dllhost.exe or other processes even after I kill the main app process with the `/T` option. The dll is held on to for 5-10 minutes. After that time, the folder can be deleted because the dll is no longer locked... – Jeff Nov 05 '18 at 21:39
  • ...However, when I do not `REM` the kill/start of explorer, the folder is not deleted even when nothing in the folder is locked by another process. – Jeff Nov 05 '18 at 21:39
  • So I have more than one issue. How do I makes sure all files are unlocked so the folder can be deleted? When I kill the app's main process, all other spawned processes are killed but some of the system processes keep one particular dll locked for several minutes. – Jeff Nov 05 '18 at 21:43
  • Then do the cleanup as possible and schedule a batch to the final rd? –  Nov 05 '18 at 21:46
  • I guess I can try scheduling a batch for final `rd`. Any ideas as to why killing/restarting explorer would interfere with deleting the folder? I restart explorer so the icons of the two apps are removed from the systray. – Jeff Nov 05 '18 at 21:51
  • Well even some uninstallers defer crucial actions to the bext restart... –  Nov 05 '18 at 21:57
  • Understood but these are portable apps and if I'm not mistaken, from earlier testing, right-clicking on the icons in the systray and selecting Exit resulted in me being able to delete the folder immediately, every time. I'll re-test this but the fact is that it doesn't matter because I have to stop/delete every thing via scripting. – Jeff Nov 05 '18 at 22:06
  • FYI - I just tested this code, as is, and it only deletes the batch file, not the folder and the batch file :-) – Jeff Nov 05 '18 at 23:42
0

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
Jeff
  • 495
  • 1
  • 5
  • 18