Lessons Learned from the Scripting Games Advanced Event 1

This is a continuation from my previous blog titled “2013 PowerShell Scripting Games Advanced Event 1 – Parameters Don’t Always Work As Expected“.

This isn’t the exact script, but sections of it. You’ll notice at the bottom of the first image shown below, I retrieve the list of folder names from the files variable to keep from having to make another call to the file system. Going from one variable to another in memory is a cheap operation where as going to disk to retrieve something is more expensive from a resources standpoint. I received this comment from a reviewer: “I thought this was pretty clever: $folders = $files.directory.name | Select-Object -Unique”.

sg-event1ca

Then I iterate through the list of folders one at a time instead of iterating through each file individually. I build my paths, storing them in variables. I check to see if the folder exists and create it if not, and then move the files a folder at a time. My goal was to make as few calls to the file system as possible.

sg-event1d

It’s not only about writing a command that’s syntax is correct, or a command that works, but also a command that’s as efficient as possible. I’ve trimmed out the error checking and verbose output from these examples to make them easier to understand.

Want to see my actual solution? Click this link: Advanced 1: An Archival Atrocity: http://scriptinggames.org/entrylist.php?entryid=279. You’ll have to be signed in to see the solution. If you like my script, please vote for it!

Update 02/09/14
The link to my solution is no longer valid so I’m posting it here since I’ve received some requests for it:

This PowerShell script can also be downloaded from the TechNet script repository.

µ

1 Comment

  1. Dexter (@DexterPOSH)

    Hello Sir,

    It has been a great learning going through your script. I think what you did here :
    $folders = $files.directory.name | Select-Object -Unique

    This is just awesome and efficient 🙂
    Looking forward to learn and imbibe from your scripts and blog posts.
    ~Regards~
    Dex

    Reply

Leave a Reply

%d bloggers like this: