Scripting Games 2010. Advanced Event 3 (Creating Files and Folders)

Третье задание:

Event Scenario

You need to create text files in a folder off the root, such as c:\fso. The text files are named after the class a student is taking, and the numerical year, month, and day. The text files will be used as templates a student can use to take notes in class. The contents of one of the files is seen in the following image.

 

The six classes being taken are: Shakespeare, World Literature, Calculus 2, Physics, Music Appreciation, and American Literature Since 1850.

 

Design Criteria

• Each file name is made up of the date and the class name.

• Each file should include the class name and the date listed inside the file.

• Your script should allow the user to choose the folder in which the files are created.

• Your script should check to see if the folder exists and create the folder if it does not exist.

• Extra design points awarded if the script allows for multiple weeks’ worth of files and automatically calculates the new file names.

• Extra design points awarded if the script permits the student to supply an array of class names and therefore override the default class names.

• Extra design points awarded if the script stores the current class names in the registry, and permits default class names or allows for an updated registry key to store new defaults.

• Style points awarded if the script uses a GUI to permit selecting the target folder

 

 

Мой скрипт:

<#
	Make-Files.ps1 PowerShell 20100430 shs

.Synopsis
	Make files for notes in the folder
	Make-Files.ps1 [Folder] [Clasess] [Date]

.Parameter	Folder
	the folder where script have to create files for notes
.Parameter Classes
	Names of Classes
.Parameter Date
	Date for notes

.Example
	Make-Files.ps1 c:\fso "Russian","English","Computer science"

#>
param ([string]$folder, $classes=("Shakespeare","World Literature","Calculus 2","Physics","Music Appreciation","American Literature Since 1850"),[datetime]$Date=(Get-Date))
cls
##############################################################################
#
#		Function Select Folder
#		This function show Select Folder box dialog
#
function Select-Folder($message='Select a folder', $path = 0,[int]$handle=0) {
    $object = New-Object -comObject Shell.Application

    $folder = $object.BrowseForFolder($handle, $message, 0, $path)
    if ($folder -ne $null) {
        $folder.self.Path
    }
}
###############################################################################
#
#=========================== Script Entry point ==========================
#
#if folder name was not passed to the script then
#show select folder dialog box
if (!$folder) {
	Write-Verbose "Folder name was not passed to the script`nShow Select Folder Dialog Box"
	$folder=Select-Folder
}
#if user don't push "Cancel" button...
if ($folder) {
	#folder exist?
	if (!(Test-Path $folder)) {
		#Try to create directory...
		try {
			New-Item $Folder -type directory -ErrorAction Stop |Out-Null
			Write-Verbose "Folder $Folder was created"
		}
		#...if get error then stop the script
		catch { Write-Host "Error creating directory"; break}
	}
	#remove last "\" from folder name if it exist
	$folder = $folder -replace "\\$"
	#Ok. In this point of script the destination folder exist already
	#Let's try to make files for each class
	foreach ($class in $classes) {
		Out-File -FilePath "$folder\$($Date.Year)_$($Date.Month)_$($Date.Day)_$class.txt"`
				-InputObject "CLASS: $class`r`nDATE: $($Date.ToShortDateString())`r`nNOTES:`r`n" -Encoding Default
	}
}
Write-Host "All done"

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.