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

Author Topic: datestamp help  (Read 6394 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 . . .

                  Salmon Trout

                  • Guest
                  Re: datestamp help
                  « Reply #15 on: September 22, 2010, 01:49:12 PM »
                  Are you sure, as you showed before, that there is a leading zero in the file date?

                  What does this show?

                  Code: [Select]
                  @echo off
                  setlocal enabledelayedexpansion
                  set foldername="Your folder"
                  REM get file date of all files in folder
                  for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (
                  set fdate=%%~tA
                  echo !fdate!
                  )
                  pause

                  x3g

                    Topic Starter


                    Rookie

                    Re: datestamp help
                    « Reply #16 on: September 22, 2010, 01:52:04 PM »
                    no hidden files. No file dated today.

                    Salmon Trout

                    • Guest
                    Re: datestamp help
                    « Reply #17 on: September 22, 2010, 01:52:58 PM »
                    Are you sure, as you showed before, that there is a leading zero in the file date?

                    What does this show?

                    Code: [Select]
                    @echo off
                    setlocal enabledelayedexpansion
                    set foldername="Your folder"
                    REM get file date of all files in folder
                    for /f "delims=" %%A in ( ' dir /b /a-d /od %foldername% ' ) do (
                    set fdate=%%~tA
                    echo !fdate!
                    )
                    pause


                    x3g

                      Topic Starter


                      Rookie

                      Re: datestamp help
                      « Reply #18 on: September 22, 2010, 01:53:26 PM »
                      windows does not display the leading 0 but DOS does.

                      Salmon Trout

                      • Guest
                      Re: datestamp help
                      « Reply #19 on: September 22, 2010, 01:55:17 PM »
                      so what does my test script show?

                      x3g

                        Topic Starter


                        Rookie

                        Re: datestamp help
                        « Reply #20 on: September 22, 2010, 01:58:04 PM »
                        ECHO is off.
                        09/22/2010 09:46 AM
                        09/22/2010 01:11 PM
                        Press any key to continue . . .

                        that's weird cause these files are dated 9/21 if I do a DIR


                        C:\test\1>dir
                         Volume in drive C has no label.
                         Volume Serial Number is E40F-A8C4

                         Directory of C:\test\1

                        09/21/2010  05:04 PM    <DIR>          .
                        09/21/2010  05:04 PM    <DIR>          ..
                        09/21/2010  05:05 PM               359 CurrDate.bat
                        09/21/2010  05:05 PM                17 notepad.bat
                        09/21/2010  05:02 PM               716 test.txt
                                       3 File(s)          1,092 bytes
                                       2 Dir(s)  116,920,819,712 bytes free

                        C:\test\1>

                        x3g

                          Topic Starter


                          Rookie

                          Re: datestamp help
                          « Reply #21 on: September 22, 2010, 02:05:26 PM »
                          Salmon,
                          I think I've found the culprit. I made a copy of a file dated 9/21 and updated it in another folder but somehow the original file which is dated 9/21 is still linked to the copy and the copied file is dated 9/22. Even though they are in 2 separate locations the script still picks up the date of the copy.
                          I created a whole new folder and copied 2 files from 9/21 and the script works like a charm.
                          thank you very much for your help. You're a hero.

                          Salmon Trout

                          • Guest
                          Re: datestamp help
                          « Reply #22 on: September 22, 2010, 02:05:36 PM »
                          possibly the script is picking up the creation date and DIR is picking up the modified date i.e.when they were moved.

                          try adding the /tC or /tA or /tW switches (only one at a time) both in the script and with DIR at the prompt

                          eg DIR /tC

                          or else...

                          1. when did you last do a disk check?

                          2. Is this a network folder?







                          Salmon Trout

                          • Guest
                          Re: datestamp help
                          « Reply #23 on: September 22, 2010, 02:06:38 PM »
                          Our posts crossed... glad it's OK now... I'm here if it goes wrong...