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

Author Topic: Help with script involving dates and days of week  (Read 12986 times)

0 Members and 1 Guest are viewing this topic.

mcgriff1969

    Topic Starter


    Rookie

    Help with script involving dates and days of week
    « on: August 15, 2010, 05:47:07 PM »
    Hi,

    I use a batch program to download files from a server.  The files are named [name]_[date in yyymmdd format].
    I wrote the program to download files for the last 7 days starting with today.  I have two problems:
    1.  When the program counts backwards I loose the leading zero so 20100804 becomes 201084 which doesn't work.
    2.  When I back up against another month it fails also.

    Can this be fixed.  Also can the day of the week that corresponds to the date be returned as a variable?

    Here is the code:

    SET dwMONTH=%DATE:~4,2%
    SET dwDAY=%DATE:~7,2%
    SET dwYEAR=%DATE:~10,4%
    SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%

    :gs01
    set localroot=[server]
    set localdrv=d:
    copy %localroot%\FL_%plantid%_*%datenow%.txt %localdrv%\rs_input\FL_%plantid%_%datenow%.txt
    copy %localroot%\D1_%plantid%_*%datenow%.txt %localdrv%\rs_input\D1_%plantid%_%datenow%.txt


    SET dwMONTH=%DATE:~4,2%
    SET /A dwDAY=%DATE:~7,2%-1
    SET dwYEAR=%DATE:~10,4%
    SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%

    copy %localroot%\FL_%plantid%_*%datenow%.txt %localdrv%\rs_input\FL_%plantid%_%datenow%.txt
    copy %localroot%\D1_%plantid%_*%datenow%.txt %localdrv%\rs_input\D1_%plantid%_%datenow%.txt

    An help is appreciated

    vishuvishal



      Beginner
    • Thanked: 3
      Re: Help with script involving dates and days of week
      « Reply #1 on: August 15, 2010, 06:10:44 PM »
      Here is the solution. There was small defect.


      Code: [Select]
      SET dwMONTH=%DATE:~4,2%
      SET dwDAY=%DATE:~7,2%
      SET dwYEAR=%DATE:~10,4%
      SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%

      :gs01
      set localroot=[server]
      set localdrv=d:
      copy "%localroot%\FL_%plantid%_*%datenow%.txt" %localdrv%\rs_input\FL_%plantid%_%datenow%.txt
      copy "%localroot%\D1_%plantid%_*%datenow%.txt" %localdrv%\rs_input\D1_%plantid%_%datenow%.txt


      SET dwMONTH=%DATE:~4,2%
      SET /A dwDAY=%DATE:~7,2%
      SET dwYEAR=%DATE:~10,4%
      SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%

      copy "%localroot%\FL_%plantid%_*%datenow%.txt" %localdrv%\rs_input\FL_%plantid%_%datenow%.txt
      copy "%localroot%\D1_%plantid%_*%datenow%.txt" %localdrv%\rs_input\D1_%plantid%_%datenow%.txt



      Checked and verified. This will work

      Please let me know if it all set.

      Thanks will do it.


      Thanks and regards
      vishu

      mcgriff1969

        Topic Starter


        Rookie

        Re: Help with script involving dates and days of week
        « Reply #2 on: August 15, 2010, 07:41:22 PM »
        Hi Vishu,

        I apologize for my lack of clarity the line: SET /A dwDAY=%DATE:~7,2%-1 is not really a defect.  It subtracts 1 day from the date.  the program counts backward for 6 days to copy files dated as I stated in the original post.  The subsequent groups of code have: SET /A dwDAY=%DATE:~7,2%-2, SET /A dwDAY=%DATE:~7,2%-1-3, etc. to SET /A dwDAY=%DATE:~7,2%-6.

        In the end I want 7 sets of files including the current day.

        vishuvishal



          Beginner
        • Thanked: 3
          Re: Help with script involving dates and days of week
          « Reply #3 on: August 16, 2010, 03:39:59 PM »
          Here is the solution. There was small defect.


          Code: [Select]
          SET dwMONTH=%DATE:~4,2%
          SET dwDAY=%DATE:~7,2%
          SET dwYEAR=%DATE:~10,4%
          SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%

          :gs01
          set localroot=[server]
          set localdrv=d:
          copy "%localroot%\FL_%plantid%_*%datenow%.txt" %localdrv%\rs_input\FL_%plantid%_%datenow%.txt
          copy "%localroot%\D1_%plantid%_*%datenow%.txt" %localdrv%\rs_input\D1_%plantid%_%datenow%.txt


          SET dwMONTH=%DATE:~4,2%
          SET /A dwDAY=%DATE:~7,2%-1
          IF /i %dwDAY% LSS 10 set dwday=0%dwDAY%
          SET dwday=%0%%dwday%
          SET dwYEAR=%DATE:~10,4%
          SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%

          copy "%localroot%\FL_%plantid%_*%datenow%.txt" %localdrv%\rs_input\FL_%plantid%_%datenow%.txt
          copy "%localroot%\D1_%plantid%_*%datenow%.txt" %localdrv%\rs_input\D1_%plantid%_%datenow%.txt



          Checked and verified. This will work

          Please let me know if it all set.

          Thanks will do it.


          Thanks and regards
          vishu

          Dusty



            Egghead

          • I could if she would, but she won't so I don't.
          • Thanked: 75
          • Experience: Beginner
          • OS: Windows XP
          Re: Help with script involving dates and days of week
          « Reply #4 on: August 16, 2010, 05:10:51 PM »
          Batch scripting is all but useless in manipulating dates.

          What happens if the day is 01?  The result of SET /A dwDAY=%DATE:~7,2%-1 is that dwDAY will return zero ???, and if the day is 02 and you "count backwards" for six days, dwDay will return -4.

          Also, if the day is 08 or 09 Set /a will treat the values as Octal and throw up the usual error message because 08 and 09 are invalid in octal.

          If the script is run near the beginning of the month, month and day must be decremented.  If you run the script in early January the day, month and year must be decremented.

          Best have a look at date manipulation using VB scripting.

          Good luck.
          One good deed is worth more than a year of good intentions.

          RoyBailey



            Rookie

            Help with script involving dates and days of week
            « Reply #5 on: August 16, 2010, 07:28:58 PM »
            C:test>type griff816.bat
            @echo off
            set /a c=0
            :start
            SET dwMONTH=%DATE:~4,2%
            echo dwMONTH=%dwMONTH%
            SET /A dwDAY=%DATE:~7,2% -%c%
            echo dwDAY=%dwDAY%
            SET dwDAY=%DATE:~7,2%
            SET dwYEAR=%DATE:~10,4%
            SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%


            set localroot=c:test
            set localdrv=c:tmp
            copy  %localroot%\\%datenow%.txt %localdrv%\\%datenow%.txt
            copy  %localroot%\\%datenow%.txt %localdrv%\\%datenow%.txt


            SET dwMONTH=%DATE:~4,2%
            SET /A dwDAY=%DATE:~7,2% -%c%
            echo dwDAY=%dwDAY%
            if %dwDAY% LEQ 0 (
            SET /a dwDAY=31
            SET /a dwMONTH=%DATE:~5,1% -1
            echo dwMONTH=%dwMONTH%
            if %dwMONTH% EQU 7 set /a dwDAY=31
            if %dwMONTH% EQU 8 set /a dwDAY=31
            if %dwMONTH% EQU 6 set /a dwDAY=30
            if %dwMONTH% EQU 2 set /a dwDAY=28
            echo dwDAY=%dwDAY%
            )
            if %dwMONTH% LEQ 9 set dwMONTH=0%dwMONTH%
            echo dwMONTH=%dwMONTH%
            SET dwYEAR=%DATE:~10,4%
            SET Datenow=%dwYEAR%%dwMONTH%%dwDAy%
            echo Datenow=%Datenow%
            if %dwDAY% LEQ 9 set dwDAY=0%dwDAY%

            copy %localroot%\\FL_%plantid%_*%datenow%.txt %localdrv%\\rs_input\\FL_%plantid%_%datenow%.txt
            copy %localroot%\\\\D1_%plantid%_*%datenow%.txt %localdrv%\\\\rs_input\\\\D1_%plantid%_%datenow%.txt
            set /a c=%c% + 1
            if %c% LEQ 7 goto :start


            C:test>
            USA

            mcgriff1969

              Topic Starter


              Rookie

              Re: Help with script involving dates and days of week
              « Reply #6 on: August 23, 2010, 01:51:44 PM »
              Hi Roy,

              I tried your code but it placed a zero in front all dates even if there were already 2 digits.  But I like your logic here.  I'm trying this but it isn't working quite right either. Any thoughts?

              set /a addzero=0
              SET dwMONTH=%DATE:~4,2%
              SET /A dwDAY=%DATE:~7,2%-14
              SET dwYEAR=%DATE:~10,4%
              if (%dwday%) GEQ (10) (
              set /a datenow=%dwYEAR%%dwMONTH%%dwDAy%
              ) else (
              SET /a Datenow=%dwyear%%dwmonth%%addzero%%dwday%
              )

              echo %datenow%

              ghostdog74



                Specialist

                Thanked: 27
                Re: Help with script involving dates and days of week
                « Reply #7 on: August 23, 2010, 07:10:28 PM »
                here comes the expert Roy. Don't worry mcgriff1969, Roy will solve your problem using batch. As for my recommendation, i would suggest you use some other tools able to manipulate dates better, eg vbscript. you can search the forum as there are many such vbscripts lying around.

                mcgriff1969

                  Topic Starter


                  Rookie

                  Re: Help with script involving dates and days of week
                  « Reply #8 on: August 25, 2010, 08:31:58 AM »
                  here comes the expert Roy. Don't worry mcgriff1969, Roy will solve your problem using batch. As for my recommendation, i would suggest you use some other tools able to manipulate dates better, eg vbscript. you can search the forum as there are many such vbscripts lying around.

                  Wow!  No love for Roy?  :o

                   I understand that VB Script is better for this but I have zero knowledge of it.

                  Helpmeh



                    Guru

                  • Roar.
                  • Thanked: 123
                    • Yes
                    • Yes
                  • Computer: Specs
                  • Experience: Familiar
                  • OS: Windows 8
                  Re: Help with script involving dates and days of week
                  « Reply #9 on: August 25, 2010, 09:09:25 AM »
                  Wow!  No love for Roy?  :o
                  No patience for Roy, or any of his other accounts.
                  Where's MagicSpeed?
                  Quote from: 'matt'
                  He's playing a game called IRL. Great graphics, *censored* gameplay.

                  Fields



                    Beginner

                    Thanked: 3
                    Help with script involving dates and days of week
                    « Reply #10 on: August 25, 2010, 09:45:16 AM »

                    I tried your code but it placed a zero in front all dates even if there were already 2 digits.  But I like your logic here.  I\'m trying this but it isnt working quite right either. Any thoughts?

                    echo %datenow%

                    Many suggestions from the Ghost and Helpless about what does not work.

                    But no suggestions or examples from the Ghost and Helpless of what can be done.

                    The Cat Bell?
                    Member of the Human Race; Citizen of the World.

                    Fields



                      Beginner

                      Thanked: 3
                      Help with script involving dates and days of week
                      « Reply #11 on: August 25, 2010, 11:26:00 AM »

                      I tried your code but it placed a zero in front all dates even if there were already 2 digits.  But I like your logic here.  Im trying this but it isnt working quite right either.

                      Any thoughts?


                      C:test>Display  mc.bat

                      @echo off
                      SET dwMONTH=%DATE:~4,2%
                      echo dwMONTH=%dwMONTH%
                      SET /A dwDAY=%DATE:~7,2%-16
                      echo dwDAY=%dwDAY%
                      SET dwYEAR=%DATE:~10,4%
                      echo dwYEAR=%dwYEAR%
                      if %dwday% LEQ 9 set dwday=0%dwday%
                      SET  Datenow=%dwyear%%dwmonth%%dwday%

                      echo Datenow=%Datenow%

                      Output:

                      C:test> mc.bat
                      dwMONTH=08
                      dwDAY=9
                      dwYEAR=2010
                      Datenow=20100809

                      C:test>

                      When dwDAY is not less than or equal to 9, no zero is added. ( We subtracted -14 not -16 )

                      Output:

                      C:test> mc.bat
                      dwMONTH=08
                      dwDAY=11
                      dwYEAR=2010
                      Datenow=20100811

                      C:test>

                      « Last Edit: August 25, 2010, 11:41:52 AM by Fields »
                      Member of the Human Race; Citizen of the World.

                      Fields



                        Beginner

                        Thanked: 3
                        Re: Help with script involving dates and days of week
                        « Reply #12 on: August 25, 2010, 12:17:56 PM »


                        I tried your code but it placed a zero in front all dates even if there were already 2 digits.  But I like your logic here.  Im trying this but it isnt working quite right either. Any thoughts?


                        set /?
                        « Sent to: mcgriff1969 on: Today at 12:14:03 PM »     

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

                         set dwday=0%dwday%

                         set /a dwday=0%dwday%

                        set /a  will not  work 09 is octal not an integer
                        Member of the Human Race; Citizen of the World.

                        Salmon Trout

                        • Guest
                        Re: Help with script involving dates and days of week
                        « Reply #13 on: August 25, 2010, 12:19:11 PM »
                        09 is octal not an integer

                        09 is not octal, and it is an integer.

                        mcgriff1969

                          Topic Starter


                          Rookie

                          Re: Help with script involving dates and days of week
                          « Reply #14 on: August 25, 2010, 03:02:16 PM »
                          C:test>Display  mc.bat

                          @echo off
                          SET dwMONTH=%DATE:~4,2%
                          echo dwMONTH=%dwMONTH%
                          SET /A dwDAY=%DATE:~7,2%-16
                          echo dwDAY=%dwDAY%
                          SET dwYEAR=%DATE:~10,4%
                          echo dwYEAR=%dwYEAR%
                          if %dwday% LEQ 9 set dwday=0%dwday%
                          SET  Datenow=%dwyear%%dwmonth%%dwday%



                          I think you've got it! :)