2012 Scripting Games Beginner Event 4: Compare Two Folders

2012 Scripting Games Beginner Event 4: Compare Two Folders

  • Comments 33
  • Likes

 2012 Scripting Games badge

Summary: In Beginner Event 4, you are required to compare two folders after completing to a backup.

About this event

Division

Beginner

Date of Event

4/5/2012 12:01 AM

Due Date

4/12/2012 12:01 AM

 

Event scenario

You are a network administrator for a small company. Each night you use a script to perform a backup of critical data files. This backup copies the contents of the data directory to a networked location. This is a file-based backup, and each night a complete backup occurs. You are not doing a differential or an incremental backup. Recently, your boss has been expressing doubts about this procedure.

Your task is to use Windows PowerShell to illustrate to your boss that the content of the network backup folder is the same as the content of the data folder. Because file sizes and write times change all the time, you only need to prove to your boss that files with the same names exist in both locations. To meet your bosses’ requirements, your solution merely needs to point out files in either folder that do not exist in the other folder. The output that is shown here is acceptable.

Image of command output

The following script should be used to create two folders for you to compare.

SetupBeginnerEvent4.ps1

# This script creates two folders off of the root.

# It then creates 20 empty files in each folder.

# Next it deletes one random file from each folder.

Push-Location

$path = "C:\"

1..2 |

% {

 Set-Location -Path (md -path "$path\$_").fullname

 1..20 | % {New-Item -Name "$_.txt" -ItemType file}

 Remove-Item -Path ("{0}.txt" -f (Get-Random -Minimum 1 -Maximum 20))

 }

Pop-Location

Design points

  • You should return file information objects as the result of your comparison.
  • You only need to compare file names, not file sizes, dates, or contents.
  • Your output should list files that exist in one folder, but not in the other folder.
  • Because the object is to prove to your boss that your backup works, you do not need to write a complicated script. Unnecessary complexity will cost you points.
  • Extra points for using native Windows PowerShell cmdlets, and for simplicity of code.

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

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. Good luck as you compete in this year’s Scripting Games. We wish you well.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed, Is a script a requirement or is a one liner allowed (without alliasses)? So either XX or 1 line of code ;-)

    Since the goal is just to prove that the backup works one line is less work...

  • @Jeff Wouters no a script is not a requirement. If you can write a one linner and meet all the requirements then one line should work.

  • Not so sure if the 'delete a random file' is a good idea.  I've just spent ages trying to figure out why my script wasn't displaying any differences until I realised it had deleted 2.txt from both folders! Argh!

    Will add 2.txt back on one and delete another instead.  :)

  • Ahh.... that's better.

  • Ha!  Mine randomly deleted the same file in each folder!  Had me confused for a second that my code was off, until I actually looked.  Make sure you double-check and don't fall for Ed Wilson's tricks!

  • Are aliases in one-liners permitted?

  • @jlsuperman: Aliasses in one liners will add unnecessary complexity and therefor may cost you points. Feedback I'd got at event 1 from one of the judges that aliasses in scripts are 'not done' but in one liners are allowed.

  • Thanks Jeff!

  • @Jlsuperman yes, if the design guidelines state that a one line script will suffice, then because it is a one line aliases are permitted. Positional parameters would also be permitted. I do not count off if you use or if you do not use aliases. I know they are confusing for some people, especially for beginners. One of the great things about PowerShell is that it is very readable, and aliases generally take away from that readability. Keep in mind, however, that you must NOT use a custom alias that would not exist on the judges system. Therefore be especially careful when using aliases IF you define your own aliases in your profile. It is easy to forget that your special aliases is one that your created yourself when it is always available.

  • Anyone else having trouble submitting their scripts for this event?  I click on the Save button, and get sent back to the landing page, but I don't see the new script listed (only my submissions for previous days).

  • @erunama

    I'm having the same problem. I've "submitted" 5 or 6 times with no success...

  • Yep, having the same problem. Can't post my script for this event.

  • Me too.  Will try again later as deadline's a ways off yet.

  • Wow this one has me... so easy to compair the 2 folders... so hard to get an output that resembles anyth like the example image

  • @Erunama, @JlSuperman, @Joshua_Taylor yes it seems several people are having problems uploading. i will post back when it is back working again.