Batch start does not wait for installation to finish

Solved
adgm1 Posted messages 306 Status Member -  
brucine Posted messages 24901 Registration date   Status Member Last intervention   -
Hello
I have batch commands to run, the first is launching an executable to install software, and the other commands are to block the firewall and to copy files.
My issue is that I need to put a timeout; otherwise, the batch executes the other commands immediately without waiting for the software installation to complete. Depending on the PC, the timeout may be more or less precise; sometimes the PC is slower than expected for the installation, and thus the following commands run too soon. How can I force the wait after the start command?

start V:\softwares\Prog.exe
timeout /t 260
netsh advfirewall firewall add rule name="Prog" program="C:\Program Files\Prog.exe" dir=out action=block profile=any
xcopy /s "\\serv\wpkg\softwares\Xx.ini" "%programfiles%\Prog\" /Y

8 answers

  1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    Hello,

    Try replacing the START command with the CALL command

    Or you can add the /WAIT switch to the START command.

    “Artificial intelligence is defined as the opposite of natural stupidity.”
    0
  2. adgm1 Posted messages 306 Status Member 10
     
    the call went well
    thank you!
    0
  3. adgm1 Posted messages 306 Status Member 10
     
    Hello. I'm going back to my post because I see a new issue when I uninstall a program with the command wmic product where name ="<program name="" here="">" call uninstall /nointeractive

    the call works fine if I want to launch a .exe program for example and then run other commands afterwards; it indeed waits for the first one to finish before triggering the next one.

    But with wmic, nothing works; I have to put a delay with timeout /t before the call, otherwise it triggers the next .bat immediately. No issue between two wmic commands; it waits just fine.

    Here is my batch;

    wmic product where "name like 'application1'" call uninstall /nointeractive

    wmic product where "name like 'application2'" call uninstall /nointeractive

    timeout /t 300

    call C:\install_app3.bat

    any idea?</program>
    0
    1. brucine Posted messages 24901 Registration date   Status Member Last intervention   4 177
       
      Hello,

      while we wait for Barnabe, who is more knowledgeable than I am in this area, here are a few comments.

      If the call command doesn’t work as desired within wmic, we can, as suggested, use
      START /WAIT, but the desired behavior relies on CMD /C, in other words, by calling a secondary batch for each wmic command:

      https://ss64.com/nt/start.html

      We can also try piping the wmic output, relying on reading its Exit Codes; I don’t believe this will work too well, with something like this, but still:


      wmic product where "name like 'application1'" call uninstall /nointeractive && wmic product where "name like 'application2'" call uninstall /nointeractive && call :INSTALL

      :INSTALL
      C:\install_app3.bat


      After that, and outside of PowerShell, which is more forgiving in this respect, it becomes more complicated.

      Here we have the solution with START /WAIT, another one looping a SLEEP test based on ERRORLEVEL of the process name of the uninstallation (to be identified in Task Manager) corresponding to the command to finish (i.e., for it to be, its value must be 1):

      https://superuser.com/questions/1152521/how-do-i-make-a-batch-file-that-started-a-process-to-wait-for-the-process-to-ter

      The same should be achievable by testing after the 2 actual wmic commands with 2 others testing the existence of the ProcessID of the uninstallation process; this is not very simple since this numeric PID must be obtained from the process name:
      https://www.developpez.net/forums/d1669516/general-developpement/programmation-systeme/windows/scripts-batch/recuperer-pid-d-processus-variable/
      and then check if this process is still active:
      https://stackoverflow.com/questions/34590331/how-to-get-process-id-of-a-specific-process-using-wmic-and-check-on-error

      Other solutions also involve testing whether the task or process initiated by the wmic command is still active:

      See here in 26, 2, and 1: https://stackoverflow.com/questions/8177695/how-to-wait-for-a-process-to-terminate-to-execute-another-process-in-batch-file

      We owe a complex batch script to Dave Benham, an undisputed wizard in this kind of situation, which loops to test for the existence of a lock file that disappears when the program is finished:

      https://www.dostips.com/forum/viewtopic.php?p=18477
      0
  4. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    Hello adgm1, hello brucine,

    brucine (who is just as competent as I am :-p) has covered the topic and provided some very interesting insights.

    Having done a test myself with the command wmic product call uninstall to uninstall LibreOffice, I find it strange that your script continues on its course while the command on my end waits (for several minutes) until the uninstallation is fully completed before handing back control.

    If you really want to ensure that the command is finished before moving on, you could add a few lines like this:

    wmic product where "name like 'application1'" call uninstall /nointeractive wmic product where "name like 'application2'" call uninstall /nointeractive :waiting timeout /t 1 /nobreak tasklist | findstr /i "wmic.exe" >nul && goto :waiting call C:\install_app3.bat 


    --

    “Artificial intelligence is defined as the opposite of natural stupidity.”
    0
  5. adgm1 Posted messages 306 Status Member 10
     
    Thank you for your response. It's an Office program that I want to uninstall first, then later install a 64-bit Office version. If I don't uninstall the 32-bit version, it says "we cannot install the 64-bit version because we have detected a 32-bit version..."

    It didn't work with the timing line; I assume that wmic.exe is looking in the "tasklist" for the uninstallation program, right? Because what appears in the task manager is Microsoft Setup Bootstrapper (32-bit). I presume that I need to replace wmic with that in the script to indicate to wait as long as this bootstrapper is active to finish the uninstallation

    The version of the script with the && call :INSTALL didn't work either.
    0
  6. adgm1 Posted messages 306 Status Member 10
     
    BINGO!! It worked by modifying the timing part as follows:

    :timing
    timeout /t 3 /nobreak
    tasklist | findstr /i "Microsoft Setup Bootstrapper (32bits)" >nul && goto :timing

    I set a 3-second delay to limit all the overlapping lines. By the way, is there a way to prevent the lines from accumulating as the timing operates? For example, by doing something like "clear screen" so that it doesn't keep adding lines to the MSDOS window, as the uninstallation takes quite a while?
    0
    1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
       
      Well done!

      Regarding the lines that are added, you can redirect the output of the TIMEOUT command:
      timeout /t 3 /nobreak >nul
      0
  7. adgm1 Posted messages 306 Status Member 10
     
    I spoke too quickly; in fact, it didn't work. It's strange, the batch keeps looping to search for the program in Task Manager but can't find it or close it to proceed. The only time it worked, I had to do something manually, but now it doesn't work anymore.
    Any ideas?
    0
    1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
       
      I need to run some tests, which version of Office is it? 2013, 2016, 2019, 2021?
      0
  8. adgm1 Posted messages 306 Status Member 10
     
    It's Office 2007. I also noticed that during uninstallation there is a task msiexec.exe that runs. I tried replacing the bootstrapper with msiexec but now I have a restart issue right after the uninstallation of Office 2007.
    0
    1. brucine Posted messages 24901 Registration date   Status Member Last intervention   4 177
       
      Hello,

      The Bootstrapper only checks if msi.exec exists to continue the uninstallation; under these conditions, it's probably not the Bootstrapper that needs to be delayed, but the subsequent process corresponding to msi.exec.

      I haven't done this in a while, I don't see a reason for it but you never know, I can't remember (testing outside the script) if the uninstallation of Office 2007 prompts (or requires) a restart.
      0