Skip to content

Instantly share code, notes, and snippets.

@zailleh
Created February 22, 2013 04:36
Show Gist options
  • Save zailleh/5010757 to your computer and use it in GitHub Desktop.
Save zailleh/5010757 to your computer and use it in GitHub Desktop.
This script takes your command-line input for the root source and dest. folders and will copy (archive) any files older than the $days specified. By doing it folder by folder (not using the recursive parameter and piping results to copy individual files) we build a relatively small list of files and process those before moving to the next folder…
################################################################################
# FILE ARCHIVING SCRIPT #
#*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*&&*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*#
# AUTHOR: Tim Caldwell DATE: 22/02/2013 #
# DESCRIPTION: #
# This script takes your command-line input for the root source and dest. #
# folders and will copy (archive) any files older than the $days specified #
################################################################################
################################################################################
# GET COMMAND-LINE PARAMS
param(
[Parameter(
Mandatory = $true,
Position = 0,
HelpMessage = "Root of the folders or share to archive"
)]
[String]$source,
[Parameter(
Mandatory = $true,
Position = 1,
HelpMessage = "Path of the folder or share of archive"
)]
[string]$target,
[Parameter(
Mandatory = $false,
Position = 2,
HelpMessage = "Age in Days of files to archive"
)]
[int]$days = 30
)
################################################################################
# Initialise our functions
#returns only folders within this level
Function Get-SubFolders ($dir)
{
return (Get-ChildItem -LiteralPath $dir -ErrorVariable $SCRIPT:ErrorLog | `
Where {$_.Attributes -match "Directory"} | Select FullName | `
%{$_.FullName});
}
#returns only files within this level
Function Get-Files($dir)
{
return (Get-ChildItem -LiteralPath $dir -ErrorVariable $SCRIPT:ErrorLog | `
Where {$_.Attributes -notmatch "Directory" `
-and ($SCRIPT:date - $_.lastwritetime).totaldays -gt $SCRIPT:days `
-and $_.extension -ne ".lnk" `
-and $_.extension -ne ".archlnk" `
-and $_.FullName.Length -lt 229} |
Select FullName | %{$_.FullName});
}
# MAIN FUNCTION
#Processes files from each folder passed in and calls itself for subfolders
Function CopyFiles($dir)
{
#Display Progress Bar
Write-Progress -Id 0 -Activity "Processing Files" `
-Status "Processing $dir...";
#Get Files
$files = Get-Files $dir
#only copy if there are files in the folder
if ($files -ne $null)
{
Write-Progress -Id 0 -Activity "Processing Files" `
-Status "Processing $dir..." `
-CurrentOperation "Copying Files...";
#set destination folder
$dest = $dir.replace($SCRIPT:source,$SCRIPT:target);
Copy-Item -LiteralPath $files -Destination $dest -Force `
-ErrorVariable $SCRIPT:ErrorLog;
#Apply Permissions
Write-Progress -Id 0 -Activity "Processing Files" `
-Status "Processing $dir..." `
-CurrentOperation "Applying Permissions...";
ForEach ($file in $files)
{
&icacls $file /c /save Permissions | Out-Null
$destFile = $file.Replace($SCRIPT:Source,$SCRIPT:target)
&icacls (Split-Path $destFile) /restore Permissions | Out-Null
}
}
#Preserve Memory by deleting file variable
Remove-Variable -Name "files" -Force;
#Get and Process sub folders
$folders = Get-SubFolders $dir;
if ($folders -ne $null)
{
ForEach ($subFolder in $folders)
{
$destFolder = $subFolder.Replace($SCRIPT:source,$SCRIPT:target)
if (!(Test-Path $destFolder))
{ [Void] (md $destFolder)}
#Set folder permissions
&icacls $subFolder /c /save Permissions | Out-Null;
&icacls (Split-Path $destFolder) /restore Permissions | Out-Null;
#Copy Files
CopyFiles $subFolder;
}
}
Remove-Variable -Name "folders" -Force;
}
################################################################################
# Script Variables
$SCRIPT:ErrorLog = $null
$SCRIPT:source = $source.TrimEnd("\"); # Getting rid of any trailing slashes is
$SCRIPT:target = $target.TrimEnd("\"); # necessary for icacls to work
$SCRIPT:days = $days;
$SCRIPT:date = Get-Date;
################################################################################
#create C:\Temp -- used to store permissions file during file permission copying
[void] (md "C:\Temp")
[void] (cd "c:\Temp")
#Create base target directory, set permissions to those on source base directory
cls
[void] (md $SCRIPT:source.replace($SCRIPT:source,$SCRIPT:target))
&icacls $SCRIPT:source /c /save Permissions;
&icacls $SCRIPT:target /inheritance:r;
&icacls (Split-Path $SCRIPT:target) /restore Permissions;
#Get Start Time for logging/performance measure purposes
$startTime = Get-Date
################################################################################
$Error.Clear();
CopyFiles $SCRIPT:source;
################################################################################
#Get End Time
$endTime = Get-Date
#Display Results & Errors
$ProcessingTime = $EndTime.Subtract($StartTime)
"ExecutionTime: $ProcessingTime" | Out-Default
"" | Out-Default
$SCRIPT:ErrorLog | Out-Default
"" | Out-Default
$Error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment