Summary: Microsoft Scripting Guy, Ed Wilson, counts the images he used in Hey, Scripting Guy! Blog posts through the years.
Microsoft Scripting Guy, Ed Wilson, is here. I have been writing the Hey, Scripting Guy! Blog for a while…actually, quite a while. It is the best job I have ever had—and at Microsoft, I have had some really cool jobs.
I have previously mentioned the file structure that I use on my laptop, but here is a synopsis:
I have all of my data in a folder called, surprisingly enough, Data. Inside my Data folder, I have a folder called ScriptingGuys. It contains all of my Hey, Scripting Guy! stuff. Inside that is a folder for each year, and inside that, is a folder for each week of that year. It is all pretty simple, and when I designed it, I had no idea there would be so many folders, files, and stuff. Well, it has worked, it continues to work, and it makes it really easy for me to do things like I am going to do today.
I was wondering, how many images have I used in my Hey, Scripting Guy! Blog posts. By using Windows PowerShell to find the answer, that question becomes nearly trivial.
Over the years, I have gone from using the bitmap (.bmp) file format, to using the Joint Photographic Experts Group (.jpg) file format, to using the Portable Network Graphics (.png) file format. Luckily, these are the only file formats I need to worry about finding and counting.
When it comes to working with files (or folders), I usually find myself using the Get-ChildItem cmdlet. I use the –Include parameter so that I can specify the array of file formats I am interested in capturing. I also use the –Recurse parameter so that I can force the command to burrow through all of the subfolders. I end the command with a pipeline character so that all of the returned FileInfo objects are piped into my next command. Here is the command I used to find all of my image files in all of my Scripting Guys folders:
Get-ChildItem -path E:\Data\ScriptingGuys -include *.png, *.jpg, *.bmp -Recurse |
I now need to use the Select-Object cmdlet. Because I am interested in annual tabulations, I need access to the year. Because the LastWriteTime property contains a System.DateTime object, I can easily access the year portion of the date.
The problem is that I need to access a specific property from a particular object that is stored in the property of a different object. This process is easier to do that it sounds, because the Select-Object cmdlet contains an –ExpandProperty parameter, which enables me to access a specific property from another object. Here is the command I use (again, I end the command with a pipeline character to send the object to the next cmdlet):
Select-Object -ExpandProperty lastwritetime |
I want to group the objects by the year, and I also want to sort by year. (The year gets renamed to Name because of the grouping). When I do this, I will have a count of all of my image files by year. Here is the command:
Group-Object -Property year -NoElement | Sort-Object name
The complete script is shown here:
Get-ChildItem -path E:\Data\ScriptingGuys -include *.png, *.jpg, *.bmp -Recurse |
Select-Object -ExpandProperty lastwritetime |
Group-Object -Property year -NoElement | Sort-Object name
Here is the script and the associated output in the Windows PowerShell ISE:
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy