Skip to content

Commit 0aadaf2

Browse files
authored
Merge pull request #57 from bullmoose20/main
Fix for excessive deletions of folders and files
2 parents 39a7e68 + 6c7bd7f commit 0aadaf2

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

PlexPosterMaker.ps1

+20-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ param (
33
[switch]$Testing
44
)
55

6-
$CurrentScriptVersion = "1.0.25"
6+
$CurrentScriptVersion = "1.0.26"
77
$global:HeaderWritten = $false
88
$ProgressPreference = 'SilentlyContinue'
99

@@ -1556,13 +1556,12 @@ function Rotate-Logs {
15561556
)
15571557

15581558
$logFolder = Join-Path $ScriptRoot "Logs"
1559-
$folderPattern = "Logs_*"
1560-
$RotationFolderName = "RotatedLogs"
1561-
$RotationFolder = Join-Path $ScriptRoot $RotationFolderName
1559+
$global:RotationFolderName = "RotatedLogs"
1560+
$RotationFolder = Join-Path $ScriptRoot $global:RotationFolderName
15621561

15631562
# Create Rotation Folder if missing
15641563
if (!(Test-Path -path $RotationFolder)) {
1565-
New-Item -ItemType Directory -Path $ScriptRoot -Name $RotationFolderName -Force | Out-Null
1564+
New-Item -ItemType Directory -Path $ScriptRoot -Name $global:RotationFolderName -Force | Out-Null
15661565
}
15671566

15681567
# Check if the log folder exists
@@ -1572,7 +1571,7 @@ function Rotate-Logs {
15721571
Rename-Item -Path $logFolder -NewName "Logs`_$timestamp"
15731572
# Create Rotation Folder if missing
15741573
if (!(Test-Path $RotationFolder)) {
1575-
New-Item -ItemType Directory -Path $ScriptRoot -Name $RotationFolderName -Force | Out-Null
1574+
New-Item -ItemType Directory -Path $ScriptRoot -Name $global:RotationFolderName -Force | Out-Null
15761575
}
15771576
# Move logs to Rotation Folder
15781577
Move-Item -Path "$logFolder`_$timestamp" $RotationFolder
@@ -1796,6 +1795,8 @@ $LatestScriptVersion = Get-LatestScriptVersion
17961795
##### START #####
17971796
$startTime = Get-Date
17981797
# Rotate logs before doing anything!
1798+
$folderPattern = "Logs_*"
1799+
$global:RotationFolderName = $null
17991800
Rotate-Logs -ScriptRoot $global:ScriptRoot
18001801
Write-Log -Message "Starting..." -Path $global:ScriptRoot\Logs\Scriptlog.log -Type Success
18011802
# Check if Config file is present
@@ -1825,10 +1826,12 @@ if ($maxLogs -le 0) {
18251826
$maxLogs = 1
18261827
}
18271828
# Delete excess log folders
1828-
$logFolders = Get-ChildItem -Path $(Join-Path $global:ScriptRoot $RotationFolderName) -Directory | Where-Object { $_.Name -match $folderPattern } | Sort-Object CreationTime -Descending | Select-Object -First $maxLogs
1829-
foreach ($folder in (Get-ChildItem -Path $(Join-Path $global:ScriptRoot $RotationFolderName) -Directory | Where-Object { $_.Name -match $folderPattern })) {
1829+
$logFolders = Get-ChildItem -Path $(Join-Path $global:ScriptRoot $global:RotationFolderName) -Directory | Where-Object { $_.Name -match $folderPattern } | Sort-Object CreationTime -Descending | Select-Object -First $maxLogs
1830+
foreach ($folder in (Get-ChildItem -Path $(Join-Path $global:ScriptRoot $global:RotationFolderName) -Directory | Where-Object { $_.Name -match $folderPattern })) {
18301831
if ($folder.FullName -notin $logFolders.FullName) {
18311832
Remove-Item -Path $folder.FullName -Recurse -Force
1833+
$fldrName = $folder.FullName
1834+
Write-Log -Message "Deleting excess folder: $fldrName" -Path $global:ScriptRoot\Logs\Scriptlog.log -Type Info
18321835
}
18331836
}
18341837

@@ -2042,11 +2045,13 @@ foreach ($path in $LogsPath, $TempPath, $TestPath, $AssetPath) {
20422045
# Delete all files and subfolders within the temp directory
20432046
if (Test-Path $TempPath) {
20442047
Remove-Item -Path (Join-Path $TempPath '*') -Recurse -Force
2048+
Write-Log -Message "Deleting temp folder: $TempPath" -Path $global:ScriptRoot\Logs\Scriptlog.log -Type Info
20452049
}
20462050

20472051
if ($Testing) {
20482052
if ((Test-Path $TestPath)) {
20492053
Remove-Item -Path (Join-Path $TestPath '*') -Recurse -Force
2054+
Write-Log -Message "Deleting test folder: $TestPath" -Path $global:ScriptRoot\Logs\Scriptlog.log -Type Info
20502055
}
20512056
}
20522057

@@ -2917,10 +2922,13 @@ Elseif ($Testing) {
29172922
$minutes = $executionTime.Minutes
29182923
$seconds = $executionTime.Seconds
29192924
$FormattedTimespawn = $hours.ToString() + "h " + $minutes.ToString() + "m " + $seconds.ToString() + "s "
2920-
Write-Log -Subtext "Poster/Background/TitleCard Tests finished, you can find them here: $(Join-Path $global:ScriptRoot 'test')" -Path (Join-Path $global:ScriptRoot 'Logs\Testinglog.log') -Type Success
2921-
Write-Log -Message "Script execution time: $FormattedTimespawn" -Path $global:ScriptRoot\Logs\Testinglog.log -Type Success
2925+
Write-Log -Subtext "Final cleanup starting..." -Path $global:ScriptRoot\Logs\Scriptlog.log -Type Success
2926+
Write-Log -Subtext "Deleting testimage: $testimage" -Path $global:ScriptRoot\Logs\Scriptlog.log -Type Info
29222927
Remove-Item -LiteralPath $testimage | out-null
2928+
Write-Log -Subtext "Deleting backgroundtestimage: $backgroundtestimage" -Path $global:ScriptRoot\Logs\Scriptlog.log -Type Info
29232929
Remove-Item -LiteralPath $backgroundtestimage | out-null
2930+
Write-Log -Subtext "Poster/Background/TitleCard Tests finished, you can find them here: $(Join-Path $global:ScriptRoot 'test')" -Path (Join-Path $global:ScriptRoot 'Logs\Testinglog.log') -Type Success
2931+
Write-Log -Message "Script execution time: $FormattedTimespawn" -Path $global:ScriptRoot\Logs\Testinglog.log -Type Success
29242932
$gettestimages = Get-ChildItem $global:ScriptRoot\test
29252933
$titlecardscount = ($gettestimages | Where-Object { $_.name -like 'Title*' }).count
29262934
$backgroundsscount = ($gettestimages | Where-Object { $_.name -like 'back*' }).count
@@ -3175,7 +3183,7 @@ else {
31753183
$SeasonNames = $SeasonsTemp.Title -join ','
31763184
$SeasonNumbers = $SeasonsTemp.index -join ','
31773185
$SeasonRatingkeys = $SeasonsTemp.ratingKey -join ','
3178-
$SeasonPosterUrl = ($SeasonsTemp | where { $_.type -eq "season" }).thumb -join ','
3186+
$SeasonPosterUrl = ($SeasonsTemp | Where-Object { $_.type -eq "season" }).thumb -join ','
31793187
}
31803188
$matchesimdb = [regex]::Matches($metadatatemp, $imdbpattern)
31813189
$matchestmdb = [regex]::Matches($metadatatemp, $tmdbpattern)
@@ -4607,6 +4615,7 @@ else {
46074615
}
46084616
if (Test-Path $EpisodeTempImage -ErrorAction SilentlyContinue) {
46094617
$null = Remove-Item -LiteralPath $EpisodeTempImage
4618+
Write-Log -Message "Deleting EpisodeTempImage: $EpisodeTempImage" -Path $global:ScriptRoot\Logs\Scriptlog.log -Type Info
46104619
}
46114620
}
46124621
Else {

Release.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.0.25
1+
1.0.26

0 commit comments

Comments
 (0)