-
Notifications
You must be signed in to change notification settings - Fork 166
/
cleanup.ps1
85 lines (71 loc) · 3.16 KB
/
cleanup.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Define relative paths
$targetFolders = @(".\static\includes", ".\static\images")
$contentDir = ".\content"
$includesDir = ".\static\includes"
$layoutsDir = ".\layouts"
$dataPropertiesDir = ".\data\properties"
$configFile = ".\config.toml"
# Initialize counters
$totalFiles = 0
$reviewedFiles = 0
$deletedFilesCount = 0
$deletedFiles = @()
# Get the current date and time
$dateTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Get the user who initiated the process
$userName = (Get-ItemProperty "HKCU:\\Software\\Microsoft\\Office\\Common\\UserInfo\\").UserName
# Process started indicator
Write-Output "Process started... Reviewing snippets and image files."
foreach ($targetFolder in $targetFolders) {
# Get all files in the target folder and its subdirectories
$files = Get-ChildItem -Path $targetFolder -Recurse -File
$totalFiles += $files.Count
foreach ($file in $files) {
$reviewedFiles++
$filename = $file.Name
$foundInContent = Get-ChildItem -Path $contentDir -Recurse -File | Select-String -Pattern "\b$filename\b"
$foundInIncludes = Get-ChildItem -Path $includesDir -Recurse -File | Select-String -Pattern "\b$filename\b"
$foundInLayouts = Get-ChildItem -Path $layoutsDir -Recurse -File | Select-String -Pattern "\b$filename\b"
$foundInDataProperties = Get-ChildItem -Path $dataPropertiesDir -Recurse -File | Select-String -Pattern "\b$filename\b"
$foundInConfig = Get-Content $configFile | Select-String -Pattern "\b$filename\b"
# If the file is not found in any of these locations, delete it
if (-not $foundInContent -and -not $foundInIncludes -and -not $foundInLayouts -and -not $foundInDataProperties -and -not $foundInConfig) {
Remove-Item $file.FullName
# Log the deletion
$deletedFiles += $file.FullName
$deletedFilesCount++
}
# Update progress
Write-Progress -Activity "Reviewing files in $targetFolder" -Status "$reviewedFiles of $totalFiles reviewed" -PercentComplete (($reviewedFiles / $totalFiles) * 100)
}
# Indicate completion for the current folder
Write-Progress -Activity "Reviewing files in $targetFolder" -Status "Done" -PercentComplete 100
Write-Output "Done processing $targetFolder"
}
# Prepare log content
$logFilePath = ".\cleanup_log.txt"
$logContent = @()
$logContent += "Process time: $dateTime"
$logContent += "Initiated by: $userName"
$logContent += ""
$logContent += "$reviewedFiles files reviewed."
$logContent += "$deletedFilesCount files deleted."
if ($deletedFilesCount -gt 0) {
$logContent += "The following files were deleted:"
$logContent += $deletedFiles
} else {
$logContent += "All files currently in use."
}
$logContent += ""
$logContent += "-" * 40 # Horizontal divider line
# Append log content to the beginning of the file
if (Test-Path $logFilePath) {
$existingContent = Get-Content $logFilePath
$logContent + $existingContent | Set-Content $logFilePath
} else {
$logContent | Set-Content $logFilePath
}
# Output final status
Write-Output "$reviewedFiles files reviewed."
Write-Output "$deletedFilesCount files deleted."
Write-Output "Log file updated: $logFilePath"