Use the VBS implementation to list all files in a folder in the order created by date _vbs

Source: Internet
Author: User
Ask:
Hello, Scripting Guy! How do I list all the files in a folder in the order of date created?

--CL

For:
Hello, CL. You know, if we're ambitious and working hard, we'll sit down and write a script for you to use WMI to return all the files in a folder. The script gets information about all of these files and stores the data in an disconnected recordset. Then, set the sort order in the recordset to arrange the files by the date and time they were created. (We then do a lot of repetitive work to convert the WMI date-time value to the readable date-time format.) Finally, we echo all the values in the recordset back to the screen. It takes a lot of time and writes a lot of code, but eventually you get a sorted list of files, and everyone says, "Wow, those Scripting Guys are really dedicated to the reader, aren't they?" ”

In fact, we are not ambitious and hard working; we are just Scripting Guys. As a Scripting Guy, we've been looking for the quickest and easiest way to solve a problem. So instead of writing a lengthy and complex script, we got a Log Parser 2.2 and hurried through the following lines:

Copy Code code as follows:

Set Objlogparser = CreateObject ("MSUtil.LogQuery")
Set Objinputformat = CreateObject ("MSUtil.LogQuery.FileSystemInputFormat")
Objinputformat.recurse = 0

Set Objoutputformat = CreateObject ("MSUtil.LogQuery.NativeOutputFormat")
OBJOUTPUTFORMAT.RTP =-1

strquery = "Select Name, creationtime from ' C:\scripts\*.* '" & _
"WHERE not Attributes like '%d% ' ORDER by CreationTime"
Objlogparser.executebatch strquery, Objinputformat, Objoutputformat

Can you guess? Not only does this script work correctly, it can also return a list of files (sorted by creation date and time), regardless of the actual situation. It's so cool.

Of course, we usually recommend that you do not use a non-operating system-built solution because we do not want people to download and install something that is not absolutely necessary. However, when you want to enumerate files, it is necessary to download and install log Parser, and when you have to get information about a set of files, you will find that the log Parser method is superior to WMI or FileSystemObject. Does the Log Parser seven or eight lines of code compare to WMI's sixty or seventy lines of code? We'll leave the decision to you.

We don't take the time to introduce all the information about log Parser; For more information, you can refer to the Scripting Story column all you need is a log (that is, the logs Parser). Now, let's just note that the script starts with the instance of creating the Log Parser object, using the MSUtil.LogQuery name to make it easier to remember. Then we create two other objects, the first one to specify the objects we're using (in this case, the file system, although we can also use event logs, Active Directory, registry, and other items), the second indicates the type of output we want to use (in this example script, All we have to do is write the data to the Command window. The following two lines of code create the input object and tell Log Parser not to retrieve the file from any subfolders:

Set Objinputformat = CreateObject ("MSUtil.LogQuery.FileSystemInputFormat")
Objinputformat.recurse = 0

What if we really want to retrieve the values of any or all of the subfolders? In that case, all we have to do is set the value of the Recurse property to-1:

Objinputformat.recurse =-1

Also, the following two lines of code create the output object and tell Log Parser to display all the data without pausing:

Set Objoutputformat = CreateObject ("MSUtil.LogQuery.NativeOutputFormat")
OBJOUTPUTFORMAT.RTP =-1

Alternatively, we can tell Log Parser to display 10 rows of data and then pause until we press a key on the keyboard and then display the next 10 rows of data. To display data in a batch of 10 lines, all we have to do is set the RTP property value to 10:

OBJOUTPUTFORMAT.RTP = 10

Next, configure the SQL query to retrieve the file information. If you have some knowledge of SQL, this query should be relatively easy to analyze; As you can see, we are querying the Name and creationtime of all the files in C:\Scripts. In addition, we want to make the returned data in the order of creation date and time, the first file to be created at the front:

strquery = "Select Name, creationtime from ' C:\scripts\*.* '" & _
"WHERE not Attributes like '%d% ' ORDER by CreationTime DESC"

The only unusual thing in this query is the WHERE clause: where not Attributes like '%d% '. Without too much explanation, this clause filters out the folder so that only the file is returned. The file system object that contains the directory attribute is the folder, because we do not want the folder, so use the WHERE not syntax to clear all objects with the directory (abbreviated%d%) attribute.

Finally, call the ExecuteBatch method to run the query and write the returned data to the command window. After two seconds, we'll get what we see below:


We do not need to enter any special commands to get this precise form output; Log Parser handles all the problems for us. It's really great, isn't it?

It is true that we do not have much effort to get these results. And it's both quick and easy. You can look at it this way: no one needs to know we don't work hard, does it?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.