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

Author Topic: Compare file date to current date  (Read 28328 times)

0 Members and 1 Guest are viewing this topic.

silvrwood

    Topic Starter


    Rookie

    Compare file date to current date
    « on: October 17, 2008, 01:55:08 PM »
    How can I compare the date of a file to the current system date?

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Compare file date to current date
    « Reply #1 on: October 17, 2008, 03:14:32 PM »
    File dates come in flavors: dateCreated, dateWritten, and dateAccessed. Decide which you need; you can grab any of them off a dir listing.

    Any of the file dates should never be greater than the system date, but you never know ;D What kind of compare are you doing? Higher or lower (arithmetic compare)? Equal or not equal (string compare)?

    If you post the output of echo %date% we can help you out with the code. If your goal is some sort of aging calculation, batch code may not be the solution.

    Let us know.  8)
    « Last Edit: October 17, 2008, 03:42:59 PM by Sidewinder »
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Dias de verano

    • Guest
    Re: Compare file date to current date
    « Reply #2 on: October 17, 2008, 03:44:40 PM »
    If you rearrange a date to the format YYYYMMDD you will produce an 8 figure number that can be used for equal/older/newer comparisons. 20081017 is greater (newer) than 20081016 and smaller (older) than 20081018.

    spshanth



      Greenhorn

      Re: Compare file date to current date
      « Reply #3 on: October 20, 2008, 12:50:23 AM »
      how to get the datecreated,datewritten or the dateaccessed??

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Re: Compare file date to current date
      « Reply #4 on: October 20, 2008, 05:51:47 AM »
      Code: [Select]
      dateCreated:

          for /f "skip=5 tokens=1-5" %%i in ('dir /tc /a-d') do echo %%i %%m

      dateWritten:

          for /f "skip=5 tokens=1-5" %%i in ('dir /tw /a-d') do echo %%i %%m

      dateAccessed:

          for /f "skip=5 tokens=1-5" %%i in ('dir /ta /a-d') do echo %%i %%m

      For each of the above you will find the appropriate date in the %%i variable and the file name in %%m. To run each command at the command line change each occurrence of %% to %.

       8)

      EDIT: added the skip parameter to the for loops
      « Last Edit: October 20, 2008, 12:01:17 PM by Sidewinder »
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      spshanth



        Greenhorn

        Re: Compare file date to current date
        « Reply #5 on: October 20, 2008, 07:58:01 AM »
        I would like to know how to get the creation date of a folder and to store the date alone in the fomrat mm/dd/yyyy in a variable.

        thanks
        Prashanth

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        Re: Compare file date to current date
        « Reply #6 on: October 20, 2008, 08:37:45 AM »
        Care to give us the name of the folder? Also what is the format of your system date? For all we know the date may already be mm/dd/yyyy.

        Post the output of this command: echo %date%

         8)

        A little imagination goes a long way.
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        silvrwood

          Topic Starter


          Rookie

          Re: Compare file date to current date
          « Reply #7 on: October 20, 2008, 11:36:23 AM »
          I have a program that needs to exit if our fileserver reboots (as it does periodically for updates).  I am thinking of having a script run when the fileserver shuts down that writes to a file.  The program on another system will check, during the window when the fileserver could reboot, to compare the date the flag file was modified to see if it equals the current date.  If it does, it will run a script to stop the program in question. 

          All of that, of course, will lead to a question of how to loop the command until it either receives a true from the comparison, or hits a certain time of the day.... But that will be for another post.

          File dates come in flavors: dateCreated, dateWritten, and dateAccessed. Decide which you need; you can grab any of them off a dir listing.

          Any of the file dates should never be greater than the system date, but you never know ;D What kind of compare are you doing? Higher or lower (arithmetic compare)? Equal or not equal (string compare)?

          If you post the output of echo %date% we can help you out with the code. If your goal is some sort of aging calculation, batch code may not be the solution.

          Let us know.  8)

          Sidewinder



            Guru

            Thanked: 139
          • Experience: Familiar
          • OS: Windows 10
          Re: Compare file date to current date
          « Reply #8 on: October 20, 2008, 01:07:09 PM »
          Quote
          I am thinking of having a script run when the fileserver shuts down that writes to a file

          What happens to this program now if the file server reboots? I think the only way to start a job at shutdown/reboot is through Group Policy (not available on XP Home).

          Do you have the source code for your program? It might be easier to have the program shut itself down if the files being served by the file server are no longer available.

          An alternate solution would be a VBScript monitor to check the file server's event log for a shutdown/reboot event code.

          Happy scripting.  8)
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          silvrwood

            Topic Starter


            Rookie

            Re: Compare file date to current date
            « Reply #9 on: October 20, 2008, 02:10:03 PM »
            When the fileserver shuts down, the program errors and the index fails to update, decimating users' ability to search.

            The fileserver is on Win Server 2003, and I've already written the script to update the flag file, and applied it as the script to run at shut down in the group policy for the OU to which I added the fileserver. 

            Now I just have to figure out how to apply this file modified date comparison to the current date, and a way to loop the whole thing every 10 minutes until the flag is true or 3:00 AM.  :)

            Sidewinder



              Guru

              Thanked: 139
            • Experience: Familiar
            • OS: Windows 10
            Re: Compare file date to current date
            « Reply #10 on: October 20, 2008, 04:08:38 PM »
            Quote
            When the fileserver shuts down, the program errors and the index fails to update, decimating users' ability to search.

            Yeah, that would certainly throw a wrench into everything. ;D

            Where is this flag file? A simple batch file can use the for command to read the contents, extract the date and do the date compare. Using the sleep command you can cycle the file every 10 minutes.

            I guess I'm hung up on the possibility that the flag file is checked and while waiting for ten minutes to pass, the server goes into reboot.

            I still like the idea of monitoring the server's event log. This way the script gets an  immediate notification that a reboot is in progress. If all you want to do is cancel your program, the monitor script can do that for you.

            Let us know. Sounds like an interesting mini-project.

             8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            spshanth



              Greenhorn

              Re: Compare file date to current date
              « Reply #11 on: October 20, 2008, 10:55:09 PM »
              thanks sidewinder..

              But when i give the command dir/tc to get the file creation date,  the time is also coming along with the date. From this only i want to take the date alone and store it in a variable. is it possible?

              Sidewinder



                Guru

                Thanked: 139
              • Experience: Familiar
              • OS: Windows 10
              Re: Compare file date to current date
              « Reply #12 on: October 21, 2008, 03:22:46 AM »
              spshanth,

              It's my fault for encouraging you, but please start your own thread. It is considered rude to hijack a thread and a far as I know this thread is still active by the OP (TS?).

              In your new thread, please post exactly what you did. dir /tc is not the example that was posted. Is there not a space between the date and time?

               8)
              « Last Edit: October 21, 2008, 03:38:31 AM by Sidewinder »
              The true sign of intelligence is not knowledge but imagination.

              -- Albert Einstein

              silvrwood

                Topic Starter


                Rookie

                Re: Compare file date to current date
                « Reply #13 on: October 21, 2008, 09:58:02 AM »
                The flag file is written to the system on which the indexing program resides so that the file can be found by said system whether the fileserver is up or down. 

                Being but a novice at batch writing, I've stuck with what is easy for me (using the flag file).  It sounds like at the expert level there is a benefit to monitoring the server log, but I don't even know where to begin for that.  Since I'm in a time crunch, I'll start with the easy and try to glean enough to undertake what you suggest as time allows.

                Thank you for the hint on a sleep command.  I will search that out.


                Where is this flag file? A simple batch file can use the for command to read the contents, extract the date and do the date compare. Using the sleep command you can cycle the file every 10 minutes.

                I guess I'm hung up on the possibility that the flag file is checked and while waiting for ten minutes to pass, the server goes into reboot.

                I still like the idea of monitoring the server's event log. This way the script gets an  immediate notification that a reboot is in progress. If all you want to do is cancel your program, the monitor script can do that for you.

                Let us know. Sounds like an interesting mini-project.

                 8)


                Sidewinder



                  Guru

                  Thanked: 139
                • Experience: Familiar
                • OS: Windows 10
                Re: Compare file date to current date
                « Reply #14 on: October 21, 2008, 01:34:44 PM »
                This needs work, but the basics are there:

                Code: [Select]
                @echo off
                :loop
                for /f %%i in (flagfile.txt) do (
                set dt=%%i
                )
                if %dt% equ %date% ......
                ping -n 601 localhost > nul
                goto loop

                Not knowing the format of your system date or the flagfile date, I suspect we will need to do some formatting of the dates. The flagfile in the code needs a path pointer. Not knowing if you have access to the sleep command, I substituted ping. And finally the dots represent code you need to write if the dates are equal.

                Other than that  ;), the snippet should be a good framework to start with.

                Just so you know, a batch file is going to tie up a command window.

                Good luck.  8)
                The true sign of intelligence is not knowledge but imagination.

                -- Albert Einstein

                silvrwood

                  Topic Starter


                  Rookie

                  Re: Compare file date to current date
                  « Reply #15 on: October 21, 2008, 02:07:13 PM »
                  You're wonderful!  Thanks!

                  Quote
                  @echo off
                  :loop
                  for /f %%i in (flagfile.txt) do (
                     set dt=%%i
                     )
                  if %dt% equ %date% ......
                  ping -n 601 localhost > nul
                  goto loop


                  If you have time to walk me through understanding what is happening in the code, I would much appreciate it.
                  What does the /f represent? 
                  What about %%i? 
                  What is the -n switch on the ping command?  Is the 601 for 601 seconds/10 minutes?  What is localhost > nul doing?

                  That's pretty much the whole thing, huh?   :-[  I'm familiar with comparisons, looping and for structures through C++, Java, VBA & the like, but obviously don't know much about DOS.

                  ALAN_BR



                    Hopeful

                    Thanked: 5
                    • Computer: Specs
                    • Experience: Experienced
                    • OS: Windows 7
                    Re: Compare file date to current date
                    « Reply #16 on: October 21, 2008, 02:36:19 PM »
                    Ping sends an echo request to a "target" IP address.

                    In this case "localhost" means don't ask a stranger on the Internet - keep it to yourself - you are not really wanting a status reply answer.

                    n 601 means repeat it 601 times.
                    In theory the first one takes almost no time at all, and the others follow at 1 second intervals = 10 minutes total.

                    In practice the intervals are somewhat different than 1 Second on my machine - perhaps another consequence of SP3, perhaps a consequence of my Netgear Firewall and Router, perhaps a consequence of ESET NOD32 Anti-virus or Comodo Firewall thinking "strange action - hmmm, shall I let it proceed - etc. etc."
                    I don't know why, I decided to get on with my life and accept that stuff just happens.

                    Regards
                    Alan

                    silvrwood

                      Topic Starter


                      Rookie

                      Re: Compare file date to current date
                      « Reply #17 on: October 21, 2008, 02:49:39 PM »
                      LOL.  Thanks for the clear explanation. 

                      silvrwood

                        Topic Starter


                        Rookie

                        Re: Compare file date to current date
                        « Reply #18 on: October 23, 2008, 12:36:40 PM »
                        So, I must confess to still wondering what does the /f represent? 
                        What about %%i? 

                        Sidewinder



                          Guru

                          Thanked: 139
                        • Experience: Familiar
                        • OS: Windows 10
                        Re: Compare file date to current date
                        « Reply #19 on: October 23, 2008, 01:01:29 PM »
                        The /f switch represents to the for command that within the parenthesis, will be a:

                        fileset - unquoted
                        command - single quotes
                        literal - double quotes

                        The %%i is an arbitrary variable name. Can be any upper or lower case alpha character. Numbers can be used but add confusion with command line arguments. The variables only live within the for loop.

                        type for /? at a command prompt for all the gory details.

                         8)
                        The true sign of intelligence is not knowledge but imagination.

                        -- Albert Einstein

                        ALAN_BR



                          Hopeful

                          Thanked: 5
                          • Computer: Specs
                          • Experience: Experienced
                          • OS: Windows 7
                          Re: Compare file date to current date
                          « Reply #20 on: October 23, 2008, 01:06:09 PM »
                          Sidewinder just beat me to it.

                          all I can add is

                          also look at  http://www.computerhope.com/forhlp.htm
                          - and if you understood everything else you are doing well !!!

                          Regards
                          Alan

                          silvrwood

                            Topic Starter


                            Rookie

                            Re: Compare file date to current date
                            « Reply #21 on: October 23, 2008, 02:03:43 PM »
                            Beautiful!  Thanks!

                            silvrwood

                              Topic Starter


                              Rookie

                              Re: Compare file date to current date
                              « Reply #22 on: October 24, 2008, 12:38:01 PM »
                              OK.  So I've got the batch file complete (I thought).  I tested it, and it just closes before finishing.  I added some PAUSE and ECHO commands to see what is going on, and it exits after the first pause following comparing the system date to the flag file date.  It doesn't ECHO either ECHO statement I put in at the 2 possible steps after the IF command, but I just can't figure out why it is stopping prematurely.

                              What am I missing?
                              Code: [Select]
                              REM @echo off

                              :loop
                              REM set variable to flag file mod date in sys format
                              for /f "tokens=1,2" %%i in (C:\Admin\flag.txt) do (
                                 set dt=%%i %%j
                                 )
                              REM echo %dt%
                              pause
                              REM compare flag mod date to sys date
                              if %dt% equ %date% GOTO indexer
                              pause
                              echo Not Equal
                              REM dates not same so wait 10 minutes and try again
                              ping -n 601 localhost > nul
                              pause
                              GOTO loop

                              :indexer
                              pause
                              echo Equal
                              REM dates same so close Indexer
                              CALL C:\Admin\EndIndex.bat
                              pause
                              REM wait for fileserver to come back up
                              ping -n 11 localhost > nul
                              REM start Indexer again
                              CALL C:\Admin\StartIndex.bat

                              EXIT

                              Sidewinder



                                Guru

                                Thanked: 139
                              • Experience: Familiar
                              • OS: Windows 10
                              Re: Compare file date to current date
                              « Reply #23 on: October 24, 2008, 02:00:47 PM »
                              Not bad. I think you may want to call :indexer instead of using goto.

                              You're waiting 11 seconds before StartIndex.bat runs. How do you check if the server is up?

                              What values do %%i and %%j resolve to and what format is your %date% variable?

                               8)

                              The true sign of intelligence is not knowledge but imagination.

                              -- Albert Einstein

                              silvrwood

                                Topic Starter


                                Rookie

                                Re: Compare file date to current date
                                « Reply #24 on: October 24, 2008, 02:18:19 PM »
                                I'll try CALL instead.  Thank you.
                                The 11 seconds is just for testing purposes so I don't sit there 15 minutes waiting to see if the final CALL works.  Once testing is successful, I'll change it to 901, acting under the assumption that within 15 minutes the Fileserver will be back up.  If it isn't, I'll see alerts the next morning anyway.   :P

                                %%i holds the day and %%j holds the date, from what I see when I ECHO %dt%, which is what I saw when I put an ECHO in for %date% (FRI 10/24/2008).

                                Not bad. I think you may want to call :indexer instead of using goto.

                                You're waiting 11 seconds before StartIndex.bat runs. How do you check if the server is up?

                                What values do %%i and %%j resolve to and what format is your %date% variable?

                                 8)



                                silvrwood

                                  Topic Starter


                                  Rookie

                                  Re: Compare file date to current date
                                  « Reply #25 on: October 24, 2008, 03:57:01 PM »
                                  I've done some testing with that one IF line, and there is an issue there no matter what command I use when true. 
                                  I am wondering if the %dt% and %date% are incompatible data types.  I believe %dt% is pulling from the file, because when I echo everything, I see it first set dt=File server, which are the first 2 values parsed out of the first line of flag.txt. 

                                  So, I've been digging to see how to get the date accessed of the file instead, or to convert a string to a date.  At the beginning of this thread is a suggestion to use
                                  Code: [Select]
                                  for /f "skip=5 tokens=1-5" %%i in ('dir /tw /a-d') do echo %%i %%m, but where do I pass the file name?

                                  Sidewinder



                                    Guru

                                    Thanked: 139
                                  • Experience: Familiar
                                  • OS: Windows 10
                                  Re: Compare file date to current date
                                  « Reply #26 on: October 24, 2008, 06:11:55 PM »
                                  Try not to over think this. While it's possible to scrape the date off a directory list, it's really not necessary. What format is the date written to the flag file by the shutdown script? Is the %date% variable in this format: ddd mm/dd/yyyy?

                                  If both dates are in the same format, you may just have to modify the for statement to override the default delimiter of space. If not, we can force the date formats to match.

                                  Code: [Select]
                                  for /f "delims=" %%i in (C:\Admin\flag.txt) do (
                                     set dt=%%i
                                     )

                                  Quote
                                  I see it first set dt=File server

                                  Where did this come from? Exactly what is in the flag file?

                                   8)
                                  The true sign of intelligence is not knowledge but imagination.

                                  -- Albert Einstein

                                  silvrwood

                                    Topic Starter


                                    Rookie

                                    Re: Compare file date to current date
                                    « Reply #27 on: October 27, 2008, 10:39:25 AM »
                                    Quote
                                    format is the date written to the flag file by the shutdown script? Is the %date% variable in this format: ddd mm/dd/yyyy?

                                    Yes.

                                    Quote
                                    Where did this come from? Exactly what is in the flag file?

                                    The flag file says:
                                    Fileserver shut down
                                    [date]

                                    When I ECHO the date of the system and that contained in the dt variable, they are both in the same format.  However, when the batch file hits my IF statement, the program exits.  That's regardless of what I have it do if true.  So I was thinking there must be a problem with the comparison.

                                    Dias de verano

                                    • Guest
                                    Re: Compare file date to current date
                                    « Reply #28 on: October 27, 2008, 11:58:49 AM »
                                    when the batch file hits my IF statement, the program exits.  That's regardless of what I have it do if true.  So I was thinking there must be a problem with the comparison.

                                    When you say it "exits", it sounds to me as if you are starting the batch file by double clicking its icon in Windows Explorer. When you are trouble shooting a batch file, it is a very good idea to open a command window in the folder where the batch file is located, and then run the batch file by typing its name at the prompt and hitting ENTER. That way, if the batch file errors out, you are returned to the prompt, and you get to see any error messages that may be generated. Another trick is to temporarily "comment out" with a REM any @echo off line you may have at the beginning of the code and that way you get to see the code being executed line by line, with all variables expanded, and you may see where in the file things are going wrong.
                                    « Last Edit: October 27, 2008, 12:32:49 PM by Dias de verano »

                                    silvrwood

                                      Topic Starter


                                      Rookie

                                      Re: Compare file date to current date
                                      « Reply #29 on: October 27, 2008, 12:53:57 PM »
                                      That was just the tip I needed!  Thank you. 
                                      Quote
                                      When you are trouble shooting a batch file, it is a very good idea to open a command window in the folder where the batch file is located, and then run the batch file by typing its name at the prompt and hitting ENTER. That way, if the batch file errors out, you are returned to the prompt, and you get to see any error messages that may be generated.

                                      Now I can see that it says

                                      Code: [Select]
                                      10/27/2008 was unexpected at this time.
                                      IF Mon 10/27/2008 EQU Mon 10/27/2008 DO ECHO Equal

                                      (I put ECHO Equal for testing purposes)

                                      It looks like I need to parse the date out without the day and compare that way to avoid problems with spaces?  Or is there a way to tell it the whole contents of the variable are one data set?

                                      devcom



                                        Apprentice

                                        Thanked: 37
                                        Re: Compare file date to current date
                                        « Reply #30 on: October 27, 2008, 01:12:27 PM »
                                        try to put Mon 10/27/2008 in quotes ("")
                                        Download: Choice.exe

                                        silvrwood

                                          Topic Starter


                                          Rookie

                                          Re: Compare file date to current date
                                          « Reply #31 on: October 27, 2008, 01:33:03 PM »
                                          Thank you. 

                                           I ended up working through it prior to seeing your post, and I have successfully tested this:

                                          Code: [Select]
                                          @echo off

                                          :loop
                                          REM set variable to flag file mod date in sys format
                                          for /f "tokens=1,2" %%i in (C:\Admin\flag.txt) do (
                                             set dt=%%j
                                             )
                                          REM Create variable from sys date parsed to just mm/dd/yyyy
                                          for /f "tokens=1,2" %%i in ("%date%") do set sysdt=%%j
                                          REM compare flag mod date to sys date
                                          IF %dt% EQU %sysdt% GOTO indexer
                                          REM ECHO Not Equal
                                          REM dates not same so wait 10 minutes and try again
                                          ping -n 601 localhost > nul
                                          GOTO loop

                                          :indexer
                                          REM ECHO equal
                                          REM dates same so close Indexer
                                          CALL C:\Admin\EndIndex.bat
                                          REM wait for fileserver to come back up
                                          ping -n 901 localhost > nul
                                          REM start Indexer again
                                          CALL C:\Admin\StartIndex.bat

                                          EXIT



                                          Thanks to everyone for all your help!!!!

                                          Now, I just have to wait for an update initiated reboot to see if the script that writes to the flag file will run even with 3rd party initiated reboots.  If not, I'll be back to find out more about the suggestion to listen for the reboot event.