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

Author Topic: Why is my FOR loop not working?  (Read 2741 times)

0 Members and 1 Guest are viewing this topic.

KenL

    Topic Starter


    Beginner
    Why is my FOR loop not working?
    « on: October 13, 2006, 01:14:45 PM »
    Hello everyone, just found this forum today and have already searched over 10 pages of history as well as used the search but no luck in the last step.

    Here is what I have so far.
    I have the contents that are here inside the FOR loop in another .bat file and it tests out ok by itself.
    I can't get the FOR loop to call the second procedure OR execute the commands in FOR loop directly.


    dir/b ?.????????????.*.*.*.job > schedules.dat  (this works)

    FOR %%A in (schedules.dat) do  runjob.bat %1 %2 %A

    OR-----

    FOR %%A in (schedules.dat) do (
    set CURDATETIME=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%%TIME:~0,2%%TIME:~3,2%
    echo %CURDATETIME%

    set JOBFILE=%1
    set RUNDATETIME=%JOBFILE:~2,12%
    echo %RUNDATETIME%

    if %RUNDATETIME% LEQ %CURDATETIME% echo OK TO RUN

    )

    Any help would really be appreciated.
    Ken
    Windows XP PRO/Server 2003. Date Format DD/MM/YYYY

    GuruGary



      Adviser
      Re: Why is my FOR loop not working?
      « Reply #1 on: October 13, 2006, 11:34:00 PM »
      I need more information on your code to help.  Please post your results of:
      echo %date%
      echo %time%
      What OS are you running?
      Please provide an example of the exact file names of a couple of .JOB files
      Please provide the what parameters you are passing to this batch file

      Instead of dir/b ?.????????????.*.*.*.job > schedules.dat why not use dir /b *.job >scuedules.dat? If the formatting needs to be more specific, I think you can at least minimize it to dir/b ?.????????????.*.job > schedules.dat

      I can't really troubleshoot very much of the the logic of your code without the information above, but here are some possible issues that I see in your code:

      Code: [Select]
      dir/b ?.????????????.*.*.*.job > schedules.dat  (this works)
       
      FOR %%A in (schedules.dat) do[highlight] call [/highlight]runjob.bat %1 %2 [highlight]%%A[/highlight]
       
      OR-----
       
      FOR[highlight] /f [/highlight]%%A in [highlight]('type schedules.dat')[/highlight] do (
      set CURDATETIME=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%%TIME:~0,2%%TIME:~3,2%
      echo %CURDATETIME%
       
      set JOBFILE=%1
      set RUNDATETIME=%JOBFILE:~2,12%
      echo %RUNDATETIME%
       
      if %RUNDATETIME% LEQ %CURDATETIME% echo OK TO RUN
      )

      KenL

        Topic Starter


        Beginner
        Re: Why is my FOR loop not working?
        « Reply #2 on: October 16, 2006, 06:55:28 AM »
        Thanks for the response, I have tried the suggestions listed and have modified my scripted with the information you asked for.
        My Environment is listed in my Signature file WinXPPro and Server2003

        Ken


        @echo off
        REM ;---------------------------------------------------------------------
        REM ;/T/ Batch queue
        REM ;
        REM ;/D/ Passing Parameters:
        REM ;/D/ Field                         Position
        REM ;/D/ ----------------------------- --------
        REM ;/D/ Version                       P1       (ie V60)
        REM ;/D/ Batch Queue                   P2       (ie dayq)
        REM ;---------------------------------------------------------------------
        call %CLAIMS%\bat\setup.bat %1

        REM Change location to the batch queue folder
        cd %QUEUES%\%2

        REM Check to see if the queue is running
        if not exist schedules.dat goto END

        REM Check to see if this job has already started, but not completed
        if exist running.dat goto END

        REM Set a running flag to stop jobs from being picked up twice
        copy %CLAIMS%\bat\exit.bat running.dat

        REM List every job file in the Queue and redirect it to a file
        REM The results of this command are:
        REM 1.200610111230.KEN.j12345.User_Listing.job
        REM 3.200610111230.KEN.j12345.Testing_thisProcess.job
        REM 5.200609111259.DON.j155.UserSecurity.job
        dir/b ?.????????????.*.job > schedules.dat

        REM Loop thru the file and run the job
        FOR /f %%A in ('type schedules.dat') do (
          REM Set up the current date and time
          set CURDATETIME=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%%TIME:~0,2%%TIME:~3,2%

          REM show me the results until this works, then remove this line
          echo %CURDATETIME%
          REM 20061016 845  (THIS IS A PROBLEM, the blank in the time)

          REM Check the run time of the job
          set JOBFILE=%A
          set RUNDATETIME=%JOBFILE:~2,12%

          REM show me the results until this works, then remove this line
          echo %RUNDATETIME%

          REM Run the job if the date/time is less than current date/time
          if %RUNDATETIME% GTR %CURDATETIME% goto ENDOFLOOP

          REM Rename the job so it runs and doesn't get picked up again
          move %A %A.bat

          REM Run the job
          call %A.bat > %QUEUES%\%2\logs\%A

          REM Archive the job
          move %A.bat %A.done
        :ENDOFLOOP
        )

        REM Remove the running flag
        DEL running.dat

        REM ;---------------------------------------------------------------------
        :END
        Windows XP PRO/Server 2003. Date Format DD/MM/YYYY

        KenL

          Topic Starter


          Beginner
          Re: Why is my FOR loop not working?
          « Reply #3 on: October 16, 2006, 11:39:00 AM »
          OK I GOT IT.....

          I had to combined the two suggestions you made and it works for now....not the best way...but it works.

          Here is what I have done.



          The first file is called from CRON.
          @echo off
          REM ;---------------------------------------------------------------------
          REM ;/T/ Batch queue
          REM ;
          REM ;/D/ Passing Parameters:
          REM ;/D/ Field                         Position
          REM ;/D/ ----------------------------- --------
          REM ;/D/ Version                       P1       (ie V60)
          REM ;/D/ Batch Queue                   P2       (ie dayq)
          REM ;---------------------------------------------------------------------
          call %CLAIMS%\bat\setup.bat %1

          REM Change location to the batch queue folder
          cd %QUEUES%\%2

          REM Check to see if the queue is running
          if not exist schedules.dat goto END

          REM Check to see if this job has already started, but not completed
          if exist running.dat goto END

          REM Set a running flag to stop jobs from being picked up twice
          copy %CLAIMS%\bat\exit.bat running.dat

          REM List every job file in the Queue and redirect it to a file
          REM The results of this command are:
          REM 1.200610111230.KEN.j12345.User_Listing. job
          REM 3.200610111230.KEN.j12345.Testing_thisP rocess.job
          REM 5.200609111259.DON.j155.UserSecurity.jo b
          dir/b ?.????????????.*.job > schedules.dat

          REM Loop thru the file and run the job
          FOR /f %%A in (SCHEDULES.DAT) do call %CLAIMS%\bat\runjob.bat %1 %2 %%A

          REM Remove the running flag
          DEL running.dat

          REM ;---------------------------------------------------------------------
          :END


          The runjob.bat file is called from the first file and it works now.
          @echo off
          REM ;------------------------------------------------------------------
          REM ;/T/ Run Job (called from batchq)
          REM ;
          REM ;/D/ Passing Parameters:
          REM ;/D/ Field                         Position
          REM ;/D/ ----------------------------- --------
          REM ;/D/ Version                       P1
          REM ;/D/ Batch Queue                   P2
          REM ;/D/ Job File                      P3
          REM ;------------------------------------------------------------------

          REM Set up the current date and time
          set CURDATETIME=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%%TIME:~0,2%%TIME:~3,2%

          REM Check the run time of the job
          set JOBFILE=%3
          set RUNDATETIME=%JOBFILE:~2,12%

          pause
          REM Run the job if the date/time is less than current date/time
          if %RUNDATETIME% GTR %CURDATETIME% goto END

          REM Rename the job so it runs and doesn't get picked up again
          move %3 %3.bat

          REM Run the job
          call %3.bat > %QUEUES%\%2\logs\%3

          REM Archive the job
          move %3.bat %3.done

          REM ;------------------------------------------------------------------
          :END



          Thanks for you help.
          Ken
          Windows XP PRO/Server 2003. Date Format DD/MM/YYYY

          GuruGary



            Adviser
            Re: Why is my FOR loop not working?
            « Reply #4 on: October 16, 2006, 04:37:36 PM »
            You're welcome.