Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Forcing wait-to-finish for each command in batch files  (Read 35755 times)

0 Members and 1 Guest are viewing this topic.

Soniq2

    Topic Starter


    Newbie

    Forcing wait-to-finish for each command in batch files
    « on: May 19, 2009, 11:42:29 AM »
    When running a batch file, each command does not seem to wait until a previous command has finished.   :o

    For example: Given the following code snippet, the test to see if DEST.DOC exists must not run until after the COPY command has completed.

    Code: [Select]
    COPY /Y SOURCE.DOC DEST.DOC
    IF EXIST DEST.DOC (
        ECHO DEST.DOC EXISTS
    ) ELSE (
       ECHO DEST.DOC DOES NOT EXIST

    How can I program the batch file commands to wait-until-complete?     :||x

    Thanks

    -Mike
    « Last Edit: May 19, 2009, 11:53:28 AM by Soniq2 »

    wbrost



      Intermediate
    • Thanked: 11
      Re: Forcing wait-to-finish for each command in batch files
      « Reply #1 on: May 20, 2009, 07:51:44 AM »
      you could always just set up a test kinda of like what you have but if the file dose not exist then you can goto the top and wait for a few seconds then try again.

      example:

      Code: [Select]

      COPY /Y SOURCE.DOC DEST.DOC

      :loop

      ping -n 1 -w 3000 1.1.1.1 > nul

      IF EXIST DEST.DOC (
          ECHO DEST.DOC EXISTS
      ) ELSE (
         GOTO loop
      )

      Soniq2

        Topic Starter


        Newbie

        Re: Forcing wait-to-finish for each command in batch files
        « Reply #2 on: May 20, 2009, 01:06:48 PM »
        Thanks wbrost,

        Yes, I thought about doing something like what you suggest.

        However, I've used an option or something in the past would instruct DOS not to proceed until the current command has completed.
        I just can't remember what that option was.
        It was something like placing a ; or something like this at the end of the command line.

        Any one remember what this is?

        Thanks

        -Mike

        Helpmeh



          Guru
        • Roar.
        • Thanked: 122
          • Yes
          • Yes
        • Computer: Specs
        • Experience: Familiar
        • OS: Windows 8
        Re: Forcing wait-to-finish for each command in batch files
        « Reply #3 on: May 20, 2009, 03:08:56 PM »
        Thanks wbrost,

        Yes, I thought about doing something like what you suggest.

        However, I've used an option or something in the past would instruct DOS not to proceed until the current command has completed.
        I just can't remember what that option was.
        It was something like placing a ; or something like this at the end of the command line.

        Any one remember what this is?

        Thanks

        -Mike
        Start /wait commandORprogram

        Will wait for the command or program to terminate before continuing. I'm not 100% sure that it works for commands, it should though.
        Where's MagicSpeed?
        Quote from: 'matt'
        He's playing a game called IRL. Great graphics, *censored* gameplay.

        NEILD



          Rookie

          Re: Forcing wait-to-finish for each command in batch files
          « Reply #4 on: September 04, 2009, 04:03:17 AM »
          Hi everyone...

          I'm a complete newbie to DOS...so please be gentle with technical terms...

          I use PDMS at work and DOS/bat files  set everything up...

          The problem is I have a .bat file that fires up a simple PDMS .mac file...
          The .mac creates a text file....
          Then the idea was the original .bat would RENAME/MOVE this text file...

          What I suspect is happening is that the .mac is creating the text file but my .bat file is carrying on and as there is no text file to RENAME/MOVE it's finishing...

          Is there a way to say to DOS check/keep checking that C:\text.txt has been fully built and ready to be renamed...

          I tried to use the suggestion on this post, see below :-

          C:\AVEVA\pdms11.6.SP1\pdms.bat TTY SEA SYSTEM/XXXXXX /TEST/DESIGN $m/c:\PDMS\Run_Dice.mac

          :loop

          ping -n 1 -w 3000 1.1.1.1 > nul

          if exist C:\AVEVA\pdms11.6.SP1\pdmsuser\dice.txt (

            echo Document exists

          rename C:\AVEVA\pdms11.6.SP1\pdmsuser\dice.txt KUPE-DICE-CHECK.txt

          MOVE C:\AVEVA\pdms11.6.SP1\pdmsuser\KUPE-DICE-CHECK.txt C:\PDMS\Results\Dice

          ) else (

          echo document doesn't exist

          go to loop

          )

          Cheers
          Neil

          oldun



            Intermediate

            Thanked: 30
            • Experience: Beginner
            • OS: Windows 7
            Re: Forcing wait-to-finish for each command in batch files
            « Reply #5 on: September 04, 2009, 04:39:17 AM »
            Try "calling" the first batch file.
            Code: [Select]
            CALL C:\AVEVA\pdms11.6.SP1\pdms.bat TTY SEA SYSTEM/XXXXXX /TEST/DESIGN $m/c:\PDMS\Run_Dice.mac
            « Last Edit: September 04, 2009, 04:54:00 AM by oldun »
            Oldun

            NEILD



              Rookie

              Re: Forcing wait-to-finish for each command in batch files
              « Reply #6 on: September 04, 2009, 05:49:03 AM »
              Cheers That worked great....the code is now continueing...

              The problem now is the code is continueing as I wanted...but by the time the code goes back to the .bat :-

              if exist C:\AVEVA\pdms11.6.SP1\pdmsuser\dice.txt (
                echo Document exists
              rename C:\AVEVA\pdms11.6.SP1\pdmsuser\dice.txt KUPE-DICE-CHECK.txt

              The file is in existance...but it's still been compiled...so cannot be coppied yet...

              Is there a way to check the compiling is finished before it continues to the rename/move ?

              Cheers
              Neil


              wbrost



                Intermediate
              • Thanked: 11
                Re: Forcing wait-to-finish for each command in batch files
                « Reply #7 on: September 04, 2009, 11:01:39 AM »
                you could check to see if the process is still active using tasklist.

                Code: [Select]
                tasklist /FI "imagename eq firefox.exe"

                change firefox.exe to the name of what you are looking for. If you need to know run the process and watch task manager.

                NEILD



                  Rookie

                  Re: Forcing wait-to-finish for each command in batch files
                  « Reply #8 on: September 04, 2009, 12:34:03 PM »
                  Hi...thanks for the reply...not sure if I've included it into my code correctly...

                  But you are correct...when the adm.exe completes running then I want to carry on to the rest of the .bat code...

                  See below how I've used it....please tell me if it's correct...as it doesn't seem to be making any difference...

                  -- --------

                  CALL C:\AVEVA\pdms11.6.SP1\pdms.bat TTY SEA SYSTEM/XXXXXX /TEST/DESIGN $m/c:\PDMS\Run_Dic.mac

                  tasklist /F1 "imaginename eq adm.exe"

                  rename C:\AVEVA\pdms11.6.SP1\pdmsuser\dice.txt KUPE-DICE-CHECK.txt

                  MOVE C:\AVEVA\pdms11.6.SP1\pdmsuser\KUPE-DICE-CHECK.txt C:\PDMS\Results\Dice

                  -- --------

                  Cheers
                  Neil

                  wbrost



                    Intermediate
                  • Thanked: 11
                    Re: Forcing wait-to-finish for each command in batch files
                    « Reply #9 on: September 04, 2009, 01:50:28 PM »

                    tasklist /F1 "imaginename eq adm.exe"


                    first thing I see is that the command is /FI not /F1
                    second is the image name is incorrect it should be "imagename eq adm.exe"

                    I will work on this and see if I can come up with something. There might be some one that already has an example for you to use. 

                    wbrost



                      Intermediate
                    • Thanked: 11
                      Re: Forcing wait-to-finish for each command in batch files
                      « Reply #10 on: September 04, 2009, 02:20:04 PM »
                      ok this will check and see if the process is still active and if not then stop for 15 seconds and try again.

                      Code: [Select]
                      @ECHO OFF

                      :loop
                      CLS

                      set errorlevel=

                      tasklist /fi "imagename eq adm.exe" | find /i "adm.exe"> NUL

                      if /i %errorlevel% GTR 0 goto yes

                      ECHO adm is working!
                      PING -n 1 -w 15000 1.1.1.1 > nul
                      GOTO loop

                      :yes
                      ECHO.
                      ECHO.
                      ECHO adm is finished
                      pause

                      NEILD



                        Rookie

                        Re: Forcing wait-to-finish for each command in batch files
                        « Reply #11 on: September 05, 2009, 02:25:24 AM »
                        wbrost thanks so much for helping me with this...

                        I've added your changes to my .bat

                        -- -------------
                        CALL C:\AVEVA\pdms11.6.SP1\pdms.bat TTY SEA SYSTEM/XXXXXX /TEST/DESIGN $m/c:\PDMS\Run_Dic.mac

                        @ECHO OFF

                        :loop
                        CLS

                        set errorlevel=

                        DEL C:\AVEVA\pdms11.6.SP1\pdmsuser\temp.txt ..................$$$$ coding stops at this point $$$$............

                        tasklist /fi "imagename eq adm.exe" l find /i "adm.exe"> NUL

                        DEL C:\AVEVA\pdms11.6.SP1\pdmsuser\temp1.txt

                        if /i %errorlevel% GTR 0 goto yes

                        DEL C:\AVEVA\pdms11.6.SP1\pdmsuser\temp2.txt

                        ECHO adm is working!
                        DEL C:\AVEVA\pdms11.6.SP1\pdmsuser\temp3.txt
                        PING -n 1 -w 15000 1.1.1.1 > nul

                        GOTO loop

                        :yes
                        ECHO.
                        ECHO.
                        ECHO adm is finished

                        rename C:\AVEVA\pdms11.6.SP1\pdmsuser\dice.txt KUPE-DICE-CHECK.txt

                        MOVE C:\AVEVA\pdms11.6.SP1\pdmsuser\KUPE-DICE-CHECK.txt C:\PDMS\Results\Dice
                        -- ------------------------------------------------------
                        It doesn't seem to completing the full code...it stops part way thgrough...

                        Before I tell you the problem can I confirm that this symbol in the centre is the one on the backslash key on the keyboard...not sure of it's name:-
                        exe" | find

                        I created some dummy files and added delete code...to see how far the code got...

                        The code made it to and past :-
                        set errorlevel=

                        It stopped at this line :-
                        tasklist /fi "imagename eq adm.exe" | find /i "adm.exe"> NUL

                        Have I made another typo ?

                        Cheers
                        Neil

                        NEILD



                          Rookie

                          Re: Forcing wait-to-finish for each command in batch files
                          « Reply #12 on: September 06, 2009, 03:59:33 AM »
                          I got it working by modifying one line :-

                          Old
                          tasklist /fi "imagename eq adm.exe" l find /i "adm.exe"> NUL

                          New
                          tasklist /fi "imagename eq adm.exe" goto goto loop

                          -- ---------------------------

                          So it now looks like this :-

                          @ECHO OFF

                          :loop
                          CLS
                          ECHO adm is working!
                          PING -n 1 -w 15000 1.1.1.1 > nul
                          errorlevel=

                          tasklist /fi "imagename eq adm.exe" goto goto loop

                          if /i %errorlevel% GTR 0 goto yes

                          :yes
                          ECHO.
                          ECHO.
                          ECHO adm is finished


                          FOR /F "TOKENS=1-4 DELIMS=/ " %%i in ('date /t') DO (SET DATE=%%i-%%j-%%k)
                          rename C:\AVEVA\pdms11.6.SP1\pdmsuser\dice.txt KUPE-DICE-CHECK-%DATE%.txt
                          MOVE C:\AVEVA\pdms11.6.SP1\pdmsuser\KUPE-DICE-CHECK-%DATE%.txt C:\PDMS\Results\Dice

                          -- ---------------------------

                          Thanks for the help...
                          Neil