The 2008 Winter Scripting Games

Beginner Event 7: Squashing Bugs

Important: The deadline for this event has passed. Solutions are available in VBScript, Windows PowerShell, and Perl.

In Squashing Bugs, competitors will be required to debug a script that contains a number of errors.

Not a native speaker of English? These event instructions are also available in the following languages: Chinese (Simplified); Chinese (Traditional); French; German; Japanese; Portuguese Brazilian; Russian; and Spanish. For more information, and to access these localized instructions, see the Scripting Games International page.

*
On This Page
About This EventAbout This Event
Event ScenarioEvent Scenario

About This Event

Division

Beginner

Deadline

Thursday, February 28, 2008 (8:00 AM PST)

Points Awarded

10

Top of pageTop of page

Event Scenario

Anyone who’s written scripts, or even had to modify existing scripts, knows that a large percentage of time is spent debugging. For this event, you’re going to have the opportunity to test out your debugging skills. We’re going to give you a script, and you need to find the bugs in it and make it work. The point of this exercise is not to rewrite the script; you need to work with the given script. We’ll tell you what it’s supposed to do, and you make the modifications necessary to make the script work. You can move lines of code and modify lines, but you shouldn’t remove lines or add completely new lines.

VBScript and Windows PowerShell

These scripts are supposed to do the following:

Search the C:\Scripts folder and all its subfolders.

Within each folder, search for all the text files (files with at .txt extension) and check the creation date of each file.

Copy each .txt file that was created more than 10 days ago to the folder C:\Old.

Echo the file name (not the full path, only the file name) of each file copied. (In VBScript, these names should be displayed to the console window when run under CScript. Optionally, you can display a single message box with all the file names listed.)

Echo back the number of files that were copied.

Hint: We introduced 10 errors to the working versions of each of these scripts to create the bug-filled versions.

Here are the scripts (also available in the 2008 Scripting Games Competitor’s Pack):

VBScript:

Set objFSO = CreateObject("FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\scripts")

dtmOld = DateAdd("m", -10, Now)
 
AllFolders objFolder

CopyTextFiles objFolder

Sub AllFolders Folder
    For Each objFolder in Folder.SubFolders

        CopyTextFiles subfolder
        AllFolders Subfolder

    Next
End Sub


Sub CopyTextFiles(subFolder)

    colFiles = SubFolder.Files

    For Each objFile in colFiles
        arrSplitName = Split(objFile.Name, ".")
        strExtension = arrSplitName(UBound(arrSplitName) - 1)
        If strExtension = "txt" and objFile.DateCreated > dtmOld Then
            objFSO.CopyFile objFile.Path, "C:\old\"
            Wscript.Echo objFile.Name
            i = i + 1
        Edn If
    Next

End

Wscript.Echo
Wscript.Echo "Total Files: " & i

PowerShell:

for ($i in Get-ChildItem C:\Scripts)
(
    if {($i.CreationTime -gt ($(Get-Date).AddMonths(-10))) and ($i.Extension = "txt")}
    {
         Copy-Item $i.FullName C:\old
         $i.Name
         $x = $x + 1
    }
)

""
"Total Files: " + $y

Perl

The Perl edition of this event is a little different. Dealing with dates in Perl can be pretty tricky, especially if you haven’t installed specific modules. We didn’t want to assume everyone had these modules installed, and we didn’t want to get really tricky for this event. So the script we’re about to show you needs to do only the following:

Search the C:\Scripts folder and all its subfolders.

Within each folder, search for all the text files (files with at .txt extension).

Copy each .txt file to the folder C:\Old.

Echo the file name (not the full path, only the file name) of each file copied.

Echo back the number of files that were copied.

Hint: We introduced 5 bugs to the working script to produce this script.

Here’s the buggy script (also available in the 2008 Scripting Games Competitor’s Pack):

use File::Copy;
File::Find;

File::Find::find({wanted => \&wanted}, 'C:\Scripts');
print "\nTotal Files: " . $count;

exit;

sub wanted (

    my $count = 0;

    /\.txt$/ or return;
    my $filename = $_;
    print "$filename \n";
    my $newfile = "C:\\old\\" . "$_";

    copy($File::Find::name, $newfile) or die "Copy failed: $!";
    $count = $cout + 1

)

Top of pageTop of page