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 28235 times)

0 Members and 1 Guest are viewing this topic.

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?