Skip to content

Instantly share code, notes, and snippets.

@crisleo94
Created February 13, 2022 00:51
Show Gist options
  • Save crisleo94/2115a7df5357dc0bb6d7d5c8502ad9bb to your computer and use it in GitHub Desktop.
Save crisleo94/2115a7df5357dc0bb6d7d5c8502ad9bb to your computer and use it in GitHub Desktop.
#Parameters
#The script should take 2 arguments $source and $destination (for the source and destination folders).
param([string]$source, [string]$destination)
#Functions
#2) Functions
#Create a function named CheckFolder that checks for the existence of a specific directory/folder that is passed
#to it as a parameter. Also, include a switch parameter named create. If the directory/folder does not exist and
#the create switch is specified, a new folder should be created using the name of the folder/directory that was
#passed to the function.
function Check-Folder() {
param([string]$folder, [switch]$create)
$exists = Test-Path $folder
if(!$exists) {
Write-Host "The folder $folder doesn't exist"
} else {
Write-Host "Folder $folder exists!"
}
if(!$exists -and $create) {
mkdir $folder
Write-Host "Folder $folder created!"
}
}
#Create a function named DisplayFolderStatistics to display folder statistics for a directory/path that is passed
#to it. Output should include the name of the folder, number of files in the folder, and total size of all files in
#that directory.
function DisplayFolderStadistics($folder) {
$folder_name = Get-ItemProperty $folder
$items_size = ls $folder -Recurse | Measure-Object -Property Length -Sum
$values = "" | Select Name, Count, Size
$values.Name = $folder_name.Name
$values.Count = $items_size.Count
$values.Size = [math]::round($items_size.Sum/1MB, 2)
return $values
}
#3) Main processing
#a) Test for existence of the source folder (using the CheckFolder function).
Check-Folder $source
#b) Test for the existence of the destination folder; create it if it is not found (using the CheckFolder function
#with the –create switch).Write-Host "Testing Destination Directory - $destination"
Check-Folder $destination -create
#c) Copy each file to the appropriate destination.
#get all the files that need to be copied
Copy-Item -Path ($source + "\*") -Destination $destination -Recurse
#c-i) Display a message when copying a file. The message should list where the file is being
#moved from and where it is being moved to.
Write-Host "Copying files from $source to $destination"
#d) Display each target folder name with the file count and byte count for each folder.
DisplayFolderStadistics $source
DisplayFolderStadistics $destination
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment