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

Author Topic: Please give me simple vb script or alternative for File time stamp WITH seconds  (Read 11085 times)

0 Members and 1 Guest are viewing this topic.

ALAN_BR

    Topic Starter


    Hopeful

    Thanked: 5
    • Computer: Specs
    • Experience: Experienced
    • OS: Windows 7
    I wish to build a record of when a particular file was last modified.

    The four lines below do almost what I want, BUT only accurate to the nearest minute.
    I would appreciate advice upon getting a resolution down to one second, as seen by Windows Explorer.
    I assume a simple vb script is appropriate, but any suggestions welcome.

    NB the File in question is
    C:\SYSTEM~1\_RESTO~1\DRIVET~1.TXT
    otherwise known as
    C:\System Volume Information\_restore{F6EA6CAA-B744-447E-8F9E-B9A9507C7CB4}\drivetable.txt

    Regards
    Alan

    My existing code is :-
    call :next C:\SYSTEM~1\_RESTO~1\DRIVET~1.TXT
    EXIT
    :next
    echo %~t1 >> MyLog.txt



    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Code: [Select]
    Const ForAppending = 8

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.GetFile("C:\System Volume Information\_restore{F6EA6CAA-B744-447E-8F9E-B9A9507C7CB4}\drivetable.txt")
    Set out = fso.OpenTextFile("myLog.txt", ForAppending, True)
    out.WriteLine f.DateCreated
    out.Close

    If you prefer, you can use f.DateLastAccessed or f.DateLastModified.

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

    -- Albert Einstein

    ALAN_BR

      Topic Starter


      Hopeful

      Thanked: 5
      • Computer: Specs
      • Experience: Experienced
      • OS: Windows 7
      Thank you very much.

      This is working very well, using f.DateLastModified.

      A future enhancement may require that my supervisory *.CMD script should set a variable to this time stamp for additional processing.  Is it possible for a vb script to set an environmental variable which could be accessed by my CMD file immediately after it has run the vb script ?

      Regards and many thanks
      Alan Borer




      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      This little snippet shows how to set a variable in the environment and read it back:

      Code: [Select]
      Set WshShell = CreateObject("WScript.Shell")
      Set WshEnv = WshShell.Environment("SYSTEM")    '
      WshEnv("zTest") = "This is a test"             'set the environment variable
      WScript.Echo WshEnv("zTest")        'read back the environment variable

      SYSTEM can also be USER, VOLATILE, or PROCESS. Each environment returns different variables.

      Environment variables created by VBScript are permanent until set to nulls by a VBScript. Environment variables created by VBScript and destroyed with batch code die only for the current command prompt session.

       8)

      « Last Edit: September 26, 2008, 06:41:26 AM by Sidewinder »
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      ALAN_BR

        Topic Starter


        Hopeful

        Thanked: 5
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Thanks - it works just as you promised.

        Unfortunately I seemed to have wanted the impossible.

        When the CMD script has done some stuff,
        it invokes a VBS script which puts the desired answer into an environmental variable,
        and then CMD continues and tries to process the answer, BUT the answer is not in its current environment.  I now realise I wanted the parent process to inherit the child's environment !!!

        If there is a way to sidestep this barrier, please let me know.

        NB as a wild guess I Googled for WScript.Return and only got 14 hits, one of which turned out to be a Browser Hijack attempt !!!
        But more usefully I found WScript.Quit which is able to return an integer - close but not close enough - I want it to return a string.

        I guess I will have to settle for VBS writing the string to temp.txt
        and CMD performing "SET /P ANSWER=temp.txt".

        Thank you for your efforts.

        p.s.  I just pressed the "Spell Check" button, and it objected to "WScript".
        Is this an omission from my local personal dictionary ?
        Or is it something the Computerhope webmaster could fix ?

        Regards
        Alan

        Sidewinder



          Guru

          Thanked: 139
        • Experience: Familiar
        • OS: Windows 10
        How are you running the VBScript? Try using cscript which should run the script within the current environment.

        Code: [Select]
        .
        .  batch commands
        .
        cscript //nologo yourscript.vbs
        .
        .  more batch commands
        .

        Good luck.  8)

        Psst: I think you meant this: SET /P ANSWER=<temp.txt
        The true sign of intelligence is not knowledge but imagination.

        -- Albert Einstein

        ALAN_BR

          Topic Starter


          Hopeful

          Thanked: 5
          • Computer: Specs
          • Experience: Experienced
          • OS: Windows 7
          Hi

          Firstly - yes, I was incorrectly remembering what I did last month, I forgot the magic "<".

          The VBS script succeeds in placing a (new) value in the environment,
          BUT the CMD script cannot afterwards see it.
          Actually, the CMD script can see it, but not in its own lifetime - only when it is subsequently launched  in another CMD instance with a fresh copy of the environment, i.e. with a desk-top short-cut to TIMESHOW.CMD, a double click launch from the desk-top gives me

          TIME is "22:48:57.96". VBSCRIPT_TIME is "82114850"
          82138440
          TIME is "22:48:59.54". VBSCRIPT_TIME is "82114850"
          Press any key to continue . . .

          the first line shows that the environment holds 82114850 created earlier
          the second line is SHOWTIME.VBS announcing a NEW value of 82138440
          the third line shows that TIMESHOW.BAT still sees the old value of 82114850
          I am sorry for the awkward 82114850 etc. numbers - I intended for VBS to use %TIME% but was unable to get that to work, so for something dynamic I pasted in a single line "FormatNumber(timer*1000,0,0,0,0)" given to me a few weeks ago.

          The New value of 82138440 was correctly put in the environment ready for the next double-click launch, i.e.

          TIME is "22:49:14.02". VBSCRIPT_TIME is "82138440"
          82154410
          TIME is "22:49:15.50". VBSCRIPT_TIME is "82138440"
          Press any key to continue . . .

          and again

          TIME is "22:50:33.85". VBSCRIPT_TIME is "82154410"
          82234210
          TIME is "22:50:35.32". VBSCRIPT_TIME is "82154410"
          Press any key to continue . . .

          etc. etc.

          My actual code is as below
          NB I used "setlocal enabledelayedexpansion" just in case it would help, but it didn't

          TIMESHOW.CMD :-
          @echo off
          setlocal enabledelayedexpansion
          ECHO TIME is "!TIME!". VBSCRIPT_TIME is "!VBSCRIPT_TIME!"
          cscript /nologo SHOWTIME.VBS
          ECHO TIME is "!TIME!". VBSCRIPT_TIME is "!VBSCRIPT_TIME!"
          pause
          exit

          SHOWTIME.VBS :-
          Set WshShell = CreateObject("WScript.Shell")
          Set WshEnv = WshShell.Environment("SYSTEM")    '
          WshEnv("VBSCRIPT_TIME") = FormatNumber(timer*1000,0,0,0,0)
          WScript.Echo WshEnv("VBSCRIPT_TIME")
          Wscript.Quit

          Regards
          Alan

          Dias de verano

          • Guest
          Why don't you make the VBScript echo whatever it is you want the batch script to "see", and use FOR to capture it into a variable?

          e.g. FOR %%A in ('cscript //nologo whatever.vbs') do set string=%%A

          Or am I missing something?

          ALAN_BR

            Topic Starter


            Hopeful

            Thanked: 5
            • Computer: Specs
            • Experience: Experienced
            • OS: Windows 7
            Thank you Dias

            The reason I did not do that is that I was unaware of this possibility !!!

            Some weeks ago I invoked "FOR /?" to find out more about parsing a string,
            and by the time I got to the /F options my eyes were beginning to glaze over,
            and the triple redundant "(`command`) DO command [command-parameters]" was ignored as irrelevant, and so I never learnt what it was for.

            Your solution failed at my first attempt, so after various attempts with reverse slanted apostrophes etc I again tried "FOR /?" and looked more carefully.
            I find that your solution works perfectly given 2 extra items, the code now commences
            For /F "delims=" %%A ....
            so now it executes the command, and will refrain from delimiting the string at the space between the date and the time.

            n.b. whilst floundering without understanding, I noticed you used "//nologo" whereas I was accustomed to "/nologo", so I tried various amounts of /
            Now I have it working I can see no difference between using either 1 or 2 off /
            either way seems to work fine.  In what situations does it matter how many / are used ?

            Thank you Dias, you pointed me to a solution I was not aware of, and the extra research I had to do has helped me to a greater understanding.

            Regards
            Alan

            Dias de verano

            • Guest
            The help visible when you type cscript /? (or more properly cscript //?) at the prompt shows 2 slashes for the switches available, although one will work in a simple case where the script itself is not supplied with any slashed parameters, as you have noticed.

            The scripting host programs cscript.exe and wscript.exe are written to interpret doubly slashed command line switches as intended for themselves. The official advice from Microsoft is to use two slashes // for options specific to the scripting hosts cscript.exe and wscript.exe so that the hosts can distinguish between switches meant for them and those which may be meant for the scripts which they are running.

            You may not have realised that, because the vbs file extension is associated (by Windows) with cscript.exe, the following have identical effect both at the command line and in a batch:

            Code: [Select]
            cscript anyscript.vbs

            anyscript.vbs

            And so do these

            Code: [Select]
            cscript //nologo anyscript.vbs

            anyscript.vbs //nologo

            So if anyscript had a parameter /goose either of these following could be used. Without the double slash convention the second invocation format would be ambiguous: (as to which parameter was for cscript.exe and which one was for anyscript.vbs)

            Code: [Select]
            cscript //nologo anyscript.vbs /goose

            anyscript.vbs //nologo /goose
            « Last Edit: September 28, 2008, 01:15:21 AM by Dias de verano »

            ALAN_BR

              Topic Starter


              Hopeful

              Thanked: 5
              • Computer: Specs
              • Experience: Experienced
              • OS: Windows 7
              Dias

              Thank you for that explanation.

               :-[ :'(  I feel such a fool.  I have spent several decades using DOS, starting before Windows ever happened.  I always knew and used the /? option to view the help embedded within any DOS command.
              I just had not appreciated that the "cscript /nologo ..." I had seen in so many scripts was actually using CSCRIPT.EXE, - I never thought of it as a DOS command that supported /?

              I some how assumed that cscript was a Windows thing and not a DOS thing.

              Please forgive a feeble excuse, but I think few people realise that cscript is a DOS thing.  I have just looked at our http://www.computerhope.com/msdos.htm, and also at DOS COMMAND repositories at three other sites, and nowhere do I see CSCRIPT listed as a DOS thing.  I also searched this entire website for CSCRIPT and got many hits from forum posts, but not a single FAQ. !!!

               :-[ :-[ :-[ :-[ Embarrassed retreat.

              Regards
              Alan

              Dias de verano

              • Guest
              I feel such a fool.  I have spent several decades using DOS, starting before Windows ever happened.

              Alan, I don't think you have any reason whatsoever to reproach yourself so! Quite the reverse. Windows scripting is, although it can be accessed from the command line, definitely part of Windows. It is not actually anything at all to do with Ms-DOS. As such it contains traps and pitfalls for people used to plain old MS-DOS. Like you, I cut my teeth on computers some decades ago, and I have every sympathy with you as you battle to come to terms with the way things are nowadays. I always take great pleasure in answering your posts, because you always ask intelligent questions and you clearly aren't afraid of having a go yourself and are pleasingly capable of picking up a hint and running with it. Furthermore, you have a decent command of the English language which is always refreshing!

              You say you spent several decades using DOS - reflect on this: most of the people on here have barely been alive for one decade. Some considerably less to judge by their posts!

              All the best

              DDV

              PS Keep up the good work!

              ALAN_BR

                Topic Starter


                Hopeful

                Thanked: 5
                • Computer: Specs
                • Experience: Experienced
                • OS: Windows 7
                Dias

                Thank you for that

                Regards
                Alan