PowerShell: Synchronizing a Folder (and Sub-Folders) Part 13

After a few comments about the logs, I’ve re-written the logging code in addition to fixing some bugs and cleaning up the function names.

A new updated script is here.

The main, large change was adding “LoggingLevel” and “LogToScreen” paramaters as well as making the script create a new logfile on each run.

[string[]]$FileNameSplit=$LogFile.Split(".")
$Suffix=".txt"
if($FileNameSplit.Count -gt 1)
{
    $Suffix="."+$FileNameSplit[$FileNameSplit.Count-1]
}
$Script:LogFileName=$LogFile.Split(".")[0] + "-"+[string](Get-Date -Format "yyyy-MM-dd-HH-mm")
$LogFileNameCount=0
#Check the logfilename is unique, if not add a number from 1 to 9 to it
While($LogFileNameCount -lt 8 -and (Test-Path -LiteralPath ($Script:LogFileName+$Suffix) -PathType Leaf))
{
    $LogFileNameCount+=1;
    $Script:LogFileName=$LogFile.Split(".")[0] + "-"+[string](Get-Date -Format "yyyy-MM-dd-HH-mm")+"-"+[string]$LogFileNameCount
}
$Script:LogFileName+=$Suffix
#If the LogFileName is STILL not unique throw and error
if(Test-Path -LiteralPath $Script:LogFileName -PathType Leaf)
{
    Write-Log -IsError "Unable to create a unique LogFileName" 
}else
{
    Write-Log ("LogFile: " + $Script:LogFileName) -NoFileWrite -WriteHost -IsInfo
}

The above code adds a time-stamp to the logfile name and ensures the new logfile name is unique. If it’s not, it adds an ascending number to the end of the name each time it can’t find a unique one. If it tries 10 times total and still can’t generate a logfile name that’s unique, it gives up with an error.

The two most important bits to the code are first;

[string[]]$FileNameSplit=$LogFile.Split(“.”)
$Suffix=”.txt”
if($FileNameSplit.Count -gt 1)
{
$Suffix=”.”+$FileNameSplit[$FileNameSplit.Count-1]
}

This splits the filename on the “.” character into an array of strings. This allows the code to find the last element of the array which should be the suffix (normally “.TXT”). That means I can add a generated timestamp after the prefix of the filename but before the suffix (like “logfile-2020-01-01-10-05.txt”).

The second key piece of code is the piece that formats the timestamp;

[string](Get-Date -Format “yyyy-MM-dd-HH-mm”)

This makes sure the timestamp is ordered sensibly and that the seperator between the years, months, days etc is a character that won’t cause any problems as a filename (“-“).

Adding a LoggingLevel parameter with three values (0: Errors Only, 1: Errors+Changes, 2: Everything) and LogToScreen parameter was comparitively easy because the Write-Log function already had the necessary code in it;

Write-Log "Getting Matching Files for $($TargetFile.Name)" -IsInfo:($LoggingLevel -gt 1) -WriteHost:$LogToScreen

Here the WriteHost switch on the Write-Log function was only set to true if the LogToScreen switch was specified when the script was run.

Similarly, the IsInfo switch on the function was only called if the LoggingLevel was set to a particular level. In the case above, the “Getting Match Files” log entry was only generated if the LoggingLevel was set to “2”.

MissingFiles and MissingFolders were set to always create an array, even if the code populating them only returned one value. That stopped any potential errors with later ForEach loops (as with the old code they might be running against a single objet rather than array.”

Last, as per general guidance I renamed any functions that didn’t use one of the approved verbs from Microsoft. No affect on the code per se, but should make it easier for people to look through and use.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: