我正在尝试执行以下操作,但是删除包含批处理文件的下载文件夹失败: 注意:所有exe,应用程序,批处理文件等都包含在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是否安全?
答案 0 :(得分:1)
根据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%"
请注意删除包含该批次的文件夹
包括任何其他文件/文件夹,没有任何其他疑问!
答案 1 :(得分:0)
好吧...我想,我想出了办法,首先尝试删除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