windows - 如果Windows批处理文件超过一分钟,请立即退出循环

标签 windows loops batch-file cmd uninstallation

如果满足特定条件,我试图删除Windows服务器上的文件夹。如果不满足,则等待10秒钟然后循环,再次检查条件。我还需要确保我不会永远陷入困境。 (检查我是否在循环中超过60秒,然后退出循环)。批处理文件如下所示:

C:\postgresql\uninstall-postgresql.exe --mode unattended
set TIMESTAMP1=%TIME%
:deleteFolder
tasklist /V |findstr /i "_uninstall*" >nul
if %errorlevel% == 0 (timeout /T /10 >nul
set TIMESTAMP2=%TIME%
**REM I want to make sure that we get out of this loop if the diff b/w TIMESTAMP2 
AND TIMESTAMP1 IS MORE THEN 60 SECONDS**
goto deleteFolder 
) ELSE (
if exists C:\postgresql RD /Q /S C:\postgresql)

Command 1
Command 2
Command 3
因此,我试图从Windows服务器上卸载Postgresql,通过检查任务列表,然后删除basedir(C:\postgresql),确保卸载完成。如果卸载过程仍在运行,请等待10秒钟,然后再次检查任务列表。我只想确保我不会永远陷入困境。
提前致谢

最佳答案

使用时间戳和calculating time difference:

@echo off
setlocal EnableDelayedExpansion

C:\postgresql\uninstall-postgresql.exe --mode unattended

set "startTime=%time: =0%"
set "endTime="

:deleteFolder
tasklist /V |findstr /i "_uninstall*" >nul
if %errorlevel% == 0 (
    goto waitAndDeleteFolder
) else (
    goto cleanup
)

:waitAndDeleteFolder
timeout /T 10
set "endTime=%time: =0%"
set "end=!endTime:%time:~8,1%=%%100)*100+1!"  &  set "start=!startTime:%time:~8,1%=%%100)*100+1!"
set /A "elap=((((10!end:%time:~2,1%=%%100)*60+1!%%100)-((((10!start:%time:~2,1%=%%100)*60+1!%%100), elap-=(elap>>31)*24*60*60*100"
if !elap! gtr 6000 goto done
goto deleteFolder

:cleanup
if exist "C:\postgresql" RD /Q /S "C:\postgresql"
goto done

:done

关于windows - 如果Windows批处理文件超过一分钟,请立即退出循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64213254/

相关文章:

linux - bat to sh 翻译请

shell - 相当于 CURRENTDIR ="$PWD"的批处理文件?

batch-file - 递归搜索子文件夹并删除子文件夹中超过6个月的所有文件

c - 如何在 NDIS 6 筛选器驱动程序中启用 802.11 监视模式 (DOT11_OPERATION_MODE_NETWORK_MONITOR)?

linux - 查找用于搜索可执行程序的路径?

C# FontFamily 不显示新字体

c++ - 如何在Windows上获取可用的RAM?

scala - 在 Scala 中将命令式 for 循环重写为声明式样式

Python 脚本,运行迭代谷歌搜索并打印热门结果和链接

java - 如何循环回到字符串的开头? super 技能,加密-解密