ask:
Hello, script expert! How to list all files in a folder in order of creation dates?
-- CL
answer:
Hello, CL. You know, if we are ambitious and work hard, we will sit down and write a script for you to return all the files in a folder using WMI. The script will get information about all of these files and store that data in an unconnected record set. Then, set the sort order in the record set to arrange the files by creation date and time. (We then do a lot of repetitive work to convert the date-time values of WMI into readable date-time format.) Finally, we echo all the values in the record set to the screen. It takes a lot of time and writes a lot of code, but you end up with a sorted list of files and everyone says “Wow, those script experts really do their best for the readers, don’t they?”
And in fact, we are not ambitious and hard-working; we are just script experts. As scripting experts, we are always looking for the fastest and easiest way to solve problems. Therefore, instead of writing a lengthy and complicated script, we got a copy of Log Parser 2.2 and hurriedly completed the following lines of code:
Set objLogParser = CreateObject("")
Set objInputFormat = CreateObject("")
= 0
Set objOutputFormat = CreateObject("")
= -1
strQuery = "SELECT Name, CreationTime FROM 'C:\Scripts\*.*' " & _
"WHERE NOT Attributes LIKE '%D%' ORDER BY CreationTime"
strQuery, objInputFormat, objOutputFormat
Guess it? This script not only works properly, it can also return a list of files (sorted by creation date and time), regardless of the actual situation. Too cool.
Of course, we usually recommend not using solutions built into non-OS; because we don't 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; when you have to obtain information about a set of files, you will find that the Log Parser method is better than WMI or FileSystemObject. Does the seven or eight lines of code of Log Parser compare to the sixty or seventy lines of code of WMI? We will leave the decision to you.
We won't take the time to introduce all information about Log Parser; for more information, you can refer to the Script Stories column All you need is the log (i.e. Log Parser). Now, we just need to note that the script starts with creating an instance of the Log Parser object, using easy-to-memorize names for better memory. Then we create two other objects, the first one specifies the object we are using (in this case the file system, although we can also use event logs, Active Directory, registry, and other entries), and the second one specifies 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 files from any subfolders:
Set objInputFormat = CreateObject("")
= 0
What if we really want to retrieve the values of any or all subfolders? In that case, all we have to do is set the value of the Recurse property to -1:
= -1
At the same time, the following two lines of code create an output object and tell Log Parser to display all data without pausing:
Set objOutputFormat = CreateObject("")
= -1
Alternatively, we can tell Log Parser to display 10 rows of data, 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 every 10 rows, all we have to do is set the value of the rtp attribute to 10:
= 10
Next, configure SQL query to retrieve file information. If you have some understanding of SQL, this query should be relatively easy to analyze; as you can see, we are just queried for Name and CreationTime of all files in C:\Scripts. In addition, we want to sort the returned data in order of creation date and time, with the first file created at the top:
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%'. Not to be too much explanation, this clause filters out folders, thus returning only files. The file system object containing the Directory attribute is a folder; because we don't want folders, we use the WHERE NOT syntax to clear all objects that have the Directory (abbreviated as %D%) attribute.
Finally, call the ExecuteBatch method to run the query and write the returned data to the command window. After a second or two we will get something like this:
We don't need to enter any special commands to get this precise table output; Log Parser handles all the problems for us. It's so awesome, isn't it?
Admittedly, we don't have to work hard to get these results. And it is fast and easy. You can look at it like this: No one has to know that we don’t work hard, doesn’t we?
Hello, script expert! How to list all files in a folder in order of creation dates?
-- CL
answer:
Hello, CL. You know, if we are ambitious and work hard, we will sit down and write a script for you to return all the files in a folder using WMI. The script will get information about all of these files and store that data in an unconnected record set. Then, set the sort order in the record set to arrange the files by creation date and time. (We then do a lot of repetitive work to convert the date-time values of WMI into readable date-time format.) Finally, we echo all the values in the record set to the screen. It takes a lot of time and writes a lot of code, but you end up with a sorted list of files and everyone says “Wow, those script experts really do their best for the readers, don’t they?”
And in fact, we are not ambitious and hard-working; we are just script experts. As scripting experts, we are always looking for the fastest and easiest way to solve problems. Therefore, instead of writing a lengthy and complicated script, we got a copy of Log Parser 2.2 and hurriedly completed the following lines of code:
Copy the codeThe code is as follows:
Set objLogParser = CreateObject("")
Set objInputFormat = CreateObject("")
= 0
Set objOutputFormat = CreateObject("")
= -1
strQuery = "SELECT Name, CreationTime FROM 'C:\Scripts\*.*' " & _
"WHERE NOT Attributes LIKE '%D%' ORDER BY CreationTime"
strQuery, objInputFormat, objOutputFormat
Guess it? This script not only works properly, it can also return a list of files (sorted by creation date and time), regardless of the actual situation. Too cool.
Of course, we usually recommend not using solutions built into non-OS; because we don't 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; when you have to obtain information about a set of files, you will find that the Log Parser method is better than WMI or FileSystemObject. Does the seven or eight lines of code of Log Parser compare to the sixty or seventy lines of code of WMI? We will leave the decision to you.
We won't take the time to introduce all information about Log Parser; for more information, you can refer to the Script Stories column All you need is the log (i.e. Log Parser). Now, we just need to note that the script starts with creating an instance of the Log Parser object, using easy-to-memorize names for better memory. Then we create two other objects, the first one specifies the object we are using (in this case the file system, although we can also use event logs, Active Directory, registry, and other entries), and the second one specifies 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 files from any subfolders:
Set objInputFormat = CreateObject("")
= 0
What if we really want to retrieve the values of any or all subfolders? In that case, all we have to do is set the value of the Recurse property to -1:
= -1
At the same time, the following two lines of code create an output object and tell Log Parser to display all data without pausing:
Set objOutputFormat = CreateObject("")
= -1
Alternatively, we can tell Log Parser to display 10 rows of data, 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 every 10 rows, all we have to do is set the value of the rtp attribute to 10:
= 10
Next, configure SQL query to retrieve file information. If you have some understanding of SQL, this query should be relatively easy to analyze; as you can see, we are just queried for Name and CreationTime of all files in C:\Scripts. In addition, we want to sort the returned data in order of creation date and time, with the first file created at the top:
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%'. Not to be too much explanation, this clause filters out folders, thus returning only files. The file system object containing the Directory attribute is a folder; because we don't want folders, we use the WHERE NOT syntax to clear all objects that have the Directory (abbreviated as %D%) attribute.
Finally, call the ExecuteBatch method to run the query and write the returned data to the command window. After a second or two we will get something like this:
We don't need to enter any special commands to get this precise table output; Log Parser handles all the problems for us. It's so awesome, isn't it?
Admittedly, we don't have to work hard to get these results. And it is fast and easy. You can look at it like this: No one has to know that we don’t work hard, doesn’t we?