0

So I'm creating a script that will make my life a little easier as a tech. The idea is that a command to uninstall runs and will remove the contents of the folder. So if that folder is empty it will continue on.

This loop should check if directory is empty and if not it should wait 60 seconds and then loop again.

for /F %%f in ('dir /A "C:\Test\*.*"') do (
    echo Waiting on uninstall to complete...
    timeout 60
)

I created a test directory filled it with a word document, but i've noticed it will loop 5-10 times then exit the loop down the rest of the program.

Anything helps

Compo
  • 36,585
  • 5
  • 27
  • 39
  • 2
    You are not rechecking the directory contents. The FOR command is parsing the entire output of the `DIR` command. For every file in the directory it finds, it will do the timeout for 60 seconds. – Squashman Mar 14 '19 at 16:55
  • Possible duplicate of [Check if any type of files exist in a directory using BATCH script](https://stackoverflow.com/questions/10813943/check-if-any-type-of-files-exist-in-a-directory-using-batch-script) – Squashman Mar 14 '19 at 18:38

1 Answers1

3

Your example does a 60 second pause for each file in the folder as mentioned by @Squashman in the comments. If you just want to check if the file directory is/is not empty and repeat until it is empty, then this might help. Just check the count of files and if file count is not 0, restart the loop, this will continue until the file count reaches 0.

@echo off
:start
for /f %%i in ('dir ^| findstr "File(s)"') do (
    if not "%%~i"=="0" (
        echo %%~i files remaining.. Waiting on uninstall to complete...
        timeout 60
        goto :start
    ) else (
        echo uninstall completed... %%~i files remaining
  ) 
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43