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

Author Topic: datestamp help  (Read 6396 times)

0 Members and 1 Guest are viewing this topic.

x3g

    Topic Starter


    Rookie

    datestamp help
    « on: September 21, 2010, 05:16:52 PM »
    hi all - I need a batch file to search in a directory. If all the files have the current date stamp then execute command1 if files are not current then execute command2.
    thank you.

    DaveLembke



      Sage
    • Thanked: 662
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: datestamp help
    « Reply #1 on: September 21, 2010, 10:10:00 PM »
    This can be done via Perl or a number of other languages which have abilities to compare strings, but I cant see doing this in raw batch.

    Salmon Trout

    • Guest
    Re: datestamp help
    « Reply #2 on: September 22, 2010, 12:51:50 AM »
    This can be done via Perl or a number of other languages which have abilities to compare strings, but I cant see doing this in raw batch.

    It is a commonplace everyday task for batch. It can easily be done... using FOR and the ~t variable modifier you can get the "date" (created or last modified?) of each file, and it is trivially easy to get today's date.

    However we do not know the OP's local date format or the format returned on his system by ~t

    The output of this script will tell us those things.

    Code: [Select]
    @echo off
    echo local date format [%date%]
    echo.>temp$$$
    for /f %%A in ("temp$$$") do echo file date  format [%%~tA]
    del temp$$$
    pause


    For example, I get this

    Code: [Select]
    local date format [22/09/2010]
    file date  format [22/09/2010 07:46 AM]

    But I am worried about this in the question

    Quote
    I need a batch file to search in a directory. If all the files have the current date stamp then execute command1 if files are not current then execute command2.
    thank you.

    All the files? What if 99 do and 1 does not? Likewise for the converse situation.


    x3g

      Topic Starter


      Rookie

      Re: datestamp help
      « Reply #3 on: September 22, 2010, 07:58:01 AM »
      these files will always have the same date. I guess we can look in one particular file to check for the date.
      thanks

      Salmon Trout

      • Guest
      Re: datestamp help
      « Reply #4 on: September 22, 2010, 09:06:38 AM »
      these files will always have the same date. I guess we can look in one particular file to check for the date.
      thanks

      So did you run my script?

      x3g

        Topic Starter


        Rookie

        Re: datestamp help
        « Reply #5 on: September 22, 2010, 09:44:19 AM »
        your script works good but it is missing the logic I need.

        Salmon Trout

        • Guest
        Re: datestamp help
        « Reply #6 on: September 22, 2010, 10:43:16 AM »
        your script works good but it is missing the logic I need.


        I asked you to run the script so I could see your local date format. Then I can make a script for you with the logic that you need.

        x3g

          Topic Starter


          Rookie

          Re: datestamp help
          « Reply #7 on: September 22, 2010, 11:15:43 AM »
          ah here it is
          local date format [Wed 09/22/2010]
          file date  format [09/22/2010 11:14 AM]

          thank you.

          Salmon Trout

          • Guest
          Re: datestamp help
          « Reply #8 on: September 22, 2010, 11:53:13 AM »
          Code: [Select]
          @echo off
          setlocal enabledelayedexpansion

          REM Of course you understand you need to edit this
          set foldername="S:\Test\Batch\After 03-07-2010\test-file-date"

          set pdate=%date%
          set today=%pdate:~4,10%

          REM get file date of latest file in folder
          for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (
          set fdate=%%~tA
          set fdate=!fdate:~0,10!
          )

          if "%fdate%"=="%today%" (
          goto yes
          ) else (
          goto no
          )

          :yes
          echo YES!!!
          rem Code to execute if file date is today
          goto next

          :no
          rem Code to execute if file date is not today
          echo NO!!!

          :next
          REM whatever happens next...

          pause






          x3g

            Topic Starter


            Rookie

            Re: datestamp help
            « Reply #9 on: September 22, 2010, 01:00:59 PM »
            this looks very promising. I'll let you know my feedback once I've tested.
            Thank you.

            x3g

              Topic Starter


              Rookie

              Re: datestamp help
              « Reply #10 on: September 22, 2010, 01:13:29 PM »
              the YES condition works great but the NO logic only works for files that are older than 1 day. If the latest files are from yesterday it will still run the YES logic.

              Salmon Trout

              • Guest
              Re: datestamp help
              « Reply #11 on: September 22, 2010, 01:22:31 PM »
              the YES condition works great but the NO logic only works for files that are older than 1 day. If the latest files are from yesterday it will still run the YES logic.

              are you quite sure about this? Because the test would be

              if "09/21/2010"=="09/22/2010"

              which should fail and therefore the else clause gets triggered.

              Could you insert these diagnostic lines shown in red?


              @echo off
              setlocal enabledelayedexpansion

              REM Of course you understand you need to edit this
              set foldername="S:\Test\Batch\After 03-07-2010\test-file-date"

              set pdate=%date%
              set today=%pdate:~4,10%

              REM get file date of latest file in folder
              for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (
                      set fdate=%%~tA
                      set fdate=!fdate:~0,10!
                      )




              echo today is %today%
              echo fdate is %fdate%
              echo comparing [%fdate%] with [%today%]


              if "%fdate%"=="%today%" (
                      goto yes
              ) else (
                      goto no
                      )

              :yes
              echo YES!!!
              rem Code to execute if file date is today
              goto next

              :no
              rem Code to execute if file date is not today
              echo NO!!!

              :next
              REM whatever happens next...

              pause



              x3g

                Topic Starter


                Rookie

                Re: datestamp help
                « Reply #12 on: September 22, 2010, 01:42:43 PM »
                even though the files are dated 9/21/2010 fdate still shows 9/22

                today is 09/22/2010
                fdate is 09/22/2010
                comparing [09/22/2010] with [09/22/2010]
                YES
                Press any key to continue . . .


                Salmon Trout

                • Guest
                Re: datestamp help
                « Reply #13 on: September 22, 2010, 01:45:10 PM »
                are you quite sure there is no file dated 09/22/2010 in that folder, including hidden files?

                x3g

                  Topic Starter


                  Rookie

                  Re: datestamp help
                  « Reply #14 on: September 22, 2010, 01:46:08 PM »
                  I replaced the 9/21 files with some 9/14 & 9/20 files and get this

                  today is 09/22/2010
                  fdate is ~0,10
                  comparing [~0,10] with [09/22/2010]
                  NO
                  Press any key to continue . . .