2012 Scripting Games Beginner Event 4: Compare Two Folders

 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