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

Author Topic: Retrieve date modified with batch file  (Read 30005 times)

0 Members and 1 Guest are viewing this topic.

gsnidow

    Topic Starter


    Rookie

  • Just a guy trying to make work stuff easier
    • Experience: Beginner
    • OS: Unknown
    Retrieve date modified with batch file
    « on: November 23, 2011, 06:20:05 AM »
    Greetings all.  A couple of months ago I was able to create a batch file that writes a list of the file paths and file names of all files in a particular folder whose file name ends with a specific string (with much help from the good folks here, that is).  Now, I need to be able to also retrieve the date modified of each file, so I can determine which is the most recent.  Now, I know I could maybe just take the last file listed, but I need to use the date modified as a variable in creating another file name.  Does anyone know if this is possible?  Below is the code that I have so far.  I should note, I left the REMs in there because they are helpful to me when debugging.  Any help would be greatly appreciated.  Thank you.

    @ECHO OFF
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET dt=%date:~10,4%%date:~4,2%%date:~7,2%
    SET DtStr=%dt%
    SET FilePath="c:\test\%DtStr%TEST_LIST.txt"
    REM ECHO %DtStr% >> %FilePath%
    REM ECHO %FilePath%
    REM PAUSE
    FOR /r "C:\Test" %%X IN (*"Test.txt") DO (
       SET var=%%X
       ECHO !var! >> %FilePath%   
       )

    Salmon Trout

    • Guest
    Re: Retrieve date modified with batch file
    « Reply #1 on: November 23, 2011, 12:47:28 PM »
    if %%X is a file name, %%~tX expands to a string (in your local format) which is the date & time the file was last written. If you run a test and copy and paste an example of the string you get, we can probably help you get a string suitable for use as a file name

    I can't help wondering... what is the purpose of the second line of this code?

    Code: [Select]
    SET dt=%date:~10,4%%date:~4,2%%date:~7,2%
    SET DtStr=%dt%

    gsnidow

      Topic Starter


      Rookie

    • Just a guy trying to make work stuff easier
      • Experience: Beginner
      • OS: Unknown
      Re: Retrieve date modified with batch file
      « Reply #2 on: November 23, 2011, 12:57:59 PM »
      Salmon, I was just about to thank you for your help I found here: http://www.computerhope.com/forum/index.php/topic,111027.0.html.  I was able to get to where I can fiddle with trial and error by working your solution in that post into mine, as below...

      REM @ECHO OFF
      SETLOCAL ENABLEDELAYEDEXPANSION
      SET dt=%date:~10,4%%date:~4,2%%date:~7,2%
      SET DtStr=%dt%
      SET FilePath="c:\test\%DtStr%TEST_LIST.txt"
      REM ECHO %DtStr% >> %FilePath%
      REM ECHO %FilePath%
      REM PAUSE
      FOR /r "C:\Test" %%X IN (*"Test.txt") DO (
         For /f "delims=" %%A IN (' cscript //nologo c:\test\vbtest.vbs "%%X" ') DO (
         SET var=%%A
         ECHO !var! >> %FilePath%
         )   
         )


      As to your question about DtStr, it does not need to be there.  I was fooling around with the variables, and left it in.  I had a whole tangled mess of stuff trying to get the dates worked out that I ended up eliminating.  I can get rid of DtStr now too, I believe. 

      One question, what does //nologo mean?  Also, it will probably be Monday before I can try your ~t method, but I will let you know how it comes out.  Thanks.

      Raven19528



        Hopeful
      • Thanked: 30
        • Computer: Specs
        • Experience: Experienced
        • OS: Windows 7
        Re: Retrieve date modified with batch file
        « Reply #3 on: November 23, 2011, 01:19:51 PM »
        One question, what does //nologo mean?

        From cscript /?: //Nologo   Prevent logo display: No banner will be shown at execution time

        Essentially, whenever cscript runs, there is a display on the screen letting the user know that a VBScript is running:

        Code: [Select]
        Microsoft (R) Windows Script Host Version 5.8
        Copyright (C) Microsoft Corporation. All rights reserved.

        //nologo just makes it so the script runs without that message showing.
        "All things that are
        Are with more spirit chased than enjoy'd" -Shakespeare

        Salmon Trout

        • Guest
        Re: Retrieve date modified with batch file
        « Reply #4 on: November 23, 2011, 01:32:50 PM »
        I see Raven jumped in while I was busy typing and uploading screen shots to Photobucket... no matter [rolls eyes]

        One question, what does //nologo mean?

        Windows has two Visual Basic Script "script engines" that you can use to run scripts. They are Wscript.exe and Cscript.exe. Wscript.exe as you might guess, is Windows GUI oriented, and Cscript.exe is command line oriented.

        Consider the following VBScript one-liner: (let's call it HelloWorld.vbs)

        Code: [Select]
        wscript.echo "Hello World"
        If you run it with the Wscript engine by typing Wscript.exe HelloWorld.vbs at the prompt this is what you get:



        With default settings if you run it with Cscript.exe at the prompt this is what you get:



        The top 2 lines are the "logo" or "banner". If we are calling a vbscript from a batch using FOR we just want the output of the vbscript; the logo will screw things up so we can suppress it either by using the //nologo switch:

        cscript //nologo HelloWorld.vbs

        so all that happens is that "Hello World" gets echoed to the console. Just like "echo Hello World" in a batch

        or change the default to "no logo" by typing:

        cscript //nologo /S

        at the prompt. This will be saved and become the default for all future sessions. You can change it back by typing

        cscript //logo /S

        Note that most cscript and wscript switches start with two slashes to distinguish them from switches to be passed to the script being run.



        gsnidow

          Topic Starter


          Rookie

        • Just a guy trying to make work stuff easier
          • Experience: Beginner
          • OS: Unknown
          Re: Retrieve date modified with batch file
          « Reply #5 on: November 25, 2011, 09:25:53 AM »
          Thanks to both of you for using your good time to consider my problem.  The '~t'  modifier is what I needed, so I won't need to use any vb scripts.  If only I had seen this first...http://www.computerhope.com/forhlp.htm  Anyhow, this is the final answer, as it does exactly what I need it to do...

          @ECHO OFF
          SETLOCAL ENABLEDELAYEDEXPANSION
          SET dt=%date:~10,4%%date:~4,2%%date:~7,2%
          SET FilePath="c:\test\%dt%TEST_LIST.txt"
          FOR /r "C:\Test" %%X IN (*) DO (
             SET FileName=%%~nxX
             SET ModDt=%%~tX
             ECHO !FileName!: !ModDt! >> %FilePath%   
             )

          gsnidow

            Topic Starter


            Rookie

          • Just a guy trying to make work stuff easier
            • Experience: Beginner
            • OS: Unknown
            Re: Retrieve date modified with batch file
            « Reply #6 on: November 28, 2011, 06:12:15 AM »
            FOR /r "C:\Test" %%X IN

            Ok, now I have problems again.  For /r is finding every instance of my desired file in all folders in C:\test.  For example, let's say I have c:\test and c:\test\subfolder.  If I am searching for 'test.txt' in c:\test, I only want to look in c:\test, even if 'test.txt' also exists in c:\test\subfolder.  Is there a way to not look in sub folders and directories of the target directory?  Thank you.

            kimsland



              Intermediate

            • Leaving, CH can obviously do without my support.
            • Thanked: 10
              • Experience: Guru
              • OS: Windows XP
              Re: Retrieve date modified with batch file
              « Reply #7 on: November 28, 2011, 06:38:50 AM »
              /R Causes the command to be executed recursively through the sub-directories of an indicated parent directory

              gsnidow

                Topic Starter


                Rookie

              • Just a guy trying to make work stuff easier
                • Experience: Beginner
                • OS: Unknown
                Re: Retrieve date modified with batch file
                « Reply #8 on: November 28, 2011, 07:25:11 AM »
                I did know about /r, but the code won't run without it, so I was wondering if there was something else I am missing.  Thanks.

                Salmon Trout

                • Guest
                Re: Retrieve date modified with batch file
                « Reply #9 on: November 28, 2011, 11:11:21 AM »
                Ok, now I have problems again.  For /r is finding every instance of my desired file in all folders in C:\test.  For example, let's say I have c:\test and c:\test\subfolder.  If I am searching for 'test.txt' in c:\test, I only want to look in c:\test, even if 'test.txt' also exists in c:\test\subfolder.  Is there a way to not look in sub folders and directories of the target directory?  Thank you.

                If you wish to find a file (or files) in a single directory, then using the FOR command with the /R switch is not the way to go. The /R switch makes FOR "walk the directory tree" all the way down, directory by directory.

                From your previous posts it seems like you wish to find all files in one folder (example: C:\test) whose names end with the characters "test" and which have the extension ".txt".

                This FOR syntax will do that:

                for %%X in (c:\test\*test.txt) do (

                Don't forget to use quotes if any part of the path is liable to contain spaces:

                for %%X in ("c:\test with spaces\*test.txt") do (

                In fact the quotes will do no harm whether the path has spaces or not. (I always use them)

                ... just a small point:

                I wonder why you are doing this in the loop:


                   SET FileName=%%~nxX
                   SET ModDt=%%~tX
                   ECHO !FileName!: !ModDt! >> %FilePath%   


                ... when you might just as well do this:

                ECHO %%~nxX: %%~tX >> %FilePath%

                In other words, why copy the contents of the FOR variable into another variable when you can use it directly?

                In fact you can get the whole thing on one line, assuming you don't want to do any more things in the loop:

                for %%X in ("c:\test with spaces\*test.txt") do ECHO %%~nxX: %%~tX >> %FilePath%









                « Last Edit: November 28, 2011, 11:22:58 AM by Salmon Trout »

                gsnidow

                  Topic Starter


                  Rookie

                • Just a guy trying to make work stuff easier
                  • Experience: Beginner
                  • OS: Unknown
                  Re: Retrieve date modified with batch file
                  « Reply #10 on: November 28, 2011, 01:09:58 PM »
                  Salmon, thank you so much for the tips.  The reason I am creating variables that seem un-necessary, is just so I can see what is going on.  Since I don't have a big need for batch files, maybe 1 or 2 per year, I usually start with every variable I think I might need, then get rid of them as I can.  I definitely like the one line approach. 

                  Salmon Trout

                  • Guest
                  Re: Retrieve date modified with batch file
                  « Reply #11 on: November 28, 2011, 01:32:05 PM »
                  The reason I am creating variables that seem un-necessary, is just so I can see what is going on.  Since I don't have a big need for batch files, maybe 1 or 2 per year, I usually start with every variable I think I might need, then get rid of them as I can.

                  I understand; I thoroughly agree with your approach.

                  Squashman



                    Specialist
                  • Thanked: 134
                  • Experience: Experienced
                  • OS: Other
                  Re: Retrieve date modified with batch file
                  « Reply #12 on: November 30, 2011, 06:55:32 PM »
                  I agree with Salmon Trout. There is no need to assign the loop variable to another variable. Because you do this you need delayed expansion. I avoid using delayed expansion whenever possible.

                  Salmon Trout

                  • Guest
                  Re: Retrieve date modified with batch file
                  « Reply #13 on: December 01, 2011, 12:01:40 AM »
                  I avoid using delayed expansion whenever possible.

                  I don't see why; it's just a feature that is there to be used.

                  Squashman



                    Specialist
                  • Thanked: 134
                  • Experience: Experienced
                  • OS: Other
                  Re: Retrieve date modified with batch file
                  « Reply #14 on: December 01, 2011, 06:31:21 AM »
                  I don't see why; it's just a feature that is there to be used.
                  I have the unfortunate fortune to work with clients who like to put Exclamation points into their data files!