2

I am trying to delete a folder on Windows server if a certain condition is met. If it is not met, then a wait for 10 seconds and loop around, check for the condition again. I also need to make sure that I am not in the loop forever. (Check if I am in the loop for more than 60 seconds, then get out of the loop). The batch file looks something like this:

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

So, I am trying to uninstall Postgresql from a windows server, making sure that the uninstall is complete by checking the tasklist and then delete the basedir (C:\postgresql). If the uninstall process is still running, then wait for 10 seconds and check the tasklist again. I just want to make sure that I am not stuck in the loop forever.

Thanks in advance

P_Ar
  • 249
  • 6
  • 19
  • I'm struggling to understand how or why, checking an `errorlevel` returned from `tasklist` piped through `findstr` would come anywhere close to taking `50` seconds. Although ensuring that the string to find is what you want would certainly help that too, I can envisage the string `_uninstall` existing, but not `_uninstalll`, or `_uninstallll` etc. – Compo Oct 09 '20 at 01:46
  • If what you're expecting is a Canonical Answer, it would need to match/pair with the question asked, which is specific not generalised. Also I would say that the comments [here](https://stackoverflow.com/questions/64161815/postgresql-uninstall-on-windows#comment113468039_64161815), [here](https://stackoverflow.com/questions/64161815/postgresql-uninstall-on-windows#comment113468128_64161815), and [here](https://stackoverflow.com/questions/64161815/postgresql-uninstall-on-windows#comment113468149_64161815), would cover it in a manner similar to your expectation. – Compo Oct 09 '20 at 10:59
  • @Compo -- checking for _uninstall because when you call uninstall-postgres.exe, it executes an uninstall.exe process by different name everytime.. Like _uninstall1833.exe or _uninstall1699.exe or something like that, so findstr "_uninstall*" – P_Ar Oct 13 '20 at 15:27
  • So you don't want `"_uninstall*"` then, because the `*` is a wildcard character meaning another `0` or more of the previous character. Hence what I told you in my previous comment, _(I doubt very much that you want to match `_uninstalll` or `_uninstallll`)_. Why don't you use `FindStr /IR "_uninstall[0123456789]*\.exe$` instead? or if the executable begins with an underscore, like that, `FindStr /IR "^_uninstall[0123456789]*\.exe$` – Compo Oct 13 '20 at 16:02
  • @Compo -- Sounds like a good idea... Thanks you. – P_Ar Oct 13 '20 at 16:43

2 Answers2

0

The simpler method would be to utitlise a count to break the loop

@Echo off & Set Count=0
 C:\postgresql\uninstall-postgresql.exe --mode unattended

:deleteFolder
If "%Count%"=="6" Goto :Failed
tasklist /V |%__AppDir_%findstr.exe /lic:"_uninstall" >nul 2> Nul && (Timeout /T 10 /Nobreak > Nul & <Nul Set /P "=." & Set /A "count+=1" & Goto :deleteFolder) || Goto :Post
:Post
if exist C:\postgresql (
 RD /Q /S C:\postgresql && Echo/Task Completed
) Else Echo/C:\postgresql Absent
Goto :Eof
:Failed
 Echo/Task failed to complete in the allocated time
Goto :Eof

I would however suggest a more robust approach on your part to identifying the task

T3RR0R
  • 1,830
  • 2
  • 7
  • 17
0

Using timestamps and 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
mnistic
  • 9,357
  • 2
  • 12
  • 28