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 28290 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