-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive.ps1
More file actions
141 lines (118 loc) · 5 KB
/
archive.ps1
File metadata and controls
141 lines (118 loc) · 5 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
param(
[Parameter(Mandatory=$true, HelpMessage="The podcast folder name to process.")]
[string]$PodcastFolder
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Continue"
# Define the current year to skip
$CurrentYear = 2026
# Colors for output
$SuccessColor = "Green"
$WarningColor = "Yellow"
$ErrorColor = "Red"
$InfoColor = "Cyan"
# Helper function to print colored messages
function Write-ColoredOutput {
param(
[string]$Message,
[string]$Color = "White"
)
Write-Host $Message -ForegroundColor $Color
}
# Validate that the podcast folder exists
$PodcastPath = Join-Path -Path (Get-Location) -ChildPath $PodcastFolder
if (-not (Test-Path -Path $PodcastPath -PathType Container)) {
Write-ColoredOutput "ERROR: Podcast folder '$PodcastFolder' not found at '$PodcastPath'" -Color $ErrorColor
exit 1
}
# Get the current directory path properly (remove provider prefix)
$CurrentPath = (Get-Location).Path
if ($CurrentPath -match '^\w+::\\') {
$CurrentPath = ($CurrentPath -split '::')[1]
}
Write-ColoredOutput "`n========================================" -Color $SuccessColor
Write-ColoredOutput "TRANSCRIPT ARCHIVE SCRIPT" -Color $SuccessColor
Write-ColoredOutput "========================================`n" -Color $SuccessColor
Write-ColoredOutput "Current working directory: $CurrentPath" -Color $InfoColor
Write-ColoredOutput "Podcast folder: $PodcastFolder" -Color $InfoColor
Write-ColoredOutput "Podcast path: $PodcastPath" -Color $InfoColor
Write-ColoredOutput "Current year (will skip): $CurrentYear`n" -Color $InfoColor
# Get all year folders
Write-ColoredOutput "Scanning year folders in '$PodcastFolder'..." -Color $InfoColor
$YearFolders = @()
Get-ChildItem -Path $PodcastPath -Directory | ForEach-Object {
if ($_.Name -match '^\d{4}(-\d{4})?$') {
# Extract the first year number from the folder name
$yearMatch = [regex]::Match($_.Name, '^\d{4}')
if ($yearMatch.Success) {
$yearNum = [int]$yearMatch.Value
if ($yearNum -ne $CurrentYear) {
$YearFolders += $_.Name
}
}
}
}
if ($YearFolders.Count -eq 0) {
Write-ColoredOutput "No year folders found (excluding $CurrentYear)" -Color $WarningColor
Write-ColoredOutput "Exiting." -Color $WarningColor
exit 0
}
$YearFolders = $YearFolders | Sort-Object
Write-ColoredOutput "Found year folders: $($YearFolders -join ', ')`n" -Color $SuccessColor
# Process each year folder
$ProcessedCount = 0
$SkippedCount = 0
foreach ($YearFolder in $YearFolders) {
# Use the full path directly from the filesystem
$YearPath = Join-Path -Path $PodcastPath -ChildPath $YearFolder
# Extract the first year for the zip filename
$yearMatch = [regex]::Match($YearFolder, '^\d{4}')
$ZipYear = $yearMatch.Value
$ZipFileName = "$ZipYear.zip"
$ZipPath = Join-Path -Path $YearPath -ChildPath $ZipFileName
Write-ColoredOutput "`n-------------------------------------------" -Color $InfoColor
Write-ColoredOutput "Processing folder: $YearFolder" -Color $InfoColor
Write-ColoredOutput "-------------------------------------------" -Color $InfoColor
# Check if zip file already exists
if (Test-Path -Path $ZipPath -PathType Leaf) {
Write-ColoredOutput " [SKIP] Archive '$ZipFileName' already exists" -Color $WarningColor
$SkippedCount++
continue
}
# Get all .md and .txt files
$FilesToArchive = @()
Get-ChildItem -Path $YearPath -File | Where-Object {
$_.Extension -in @('.md', '.txt')
} | ForEach-Object {
$FilesToArchive += $_
}
if ($FilesToArchive.Count -eq 0) {
Write-ColoredOutput " [SKIP] No .md or .txt files found" -Color $WarningColor
$SkippedCount++
continue
}
Write-ColoredOutput " Found $($FilesToArchive.Count) files to archive:" -Color $InfoColor
$FilesToArchive | ForEach-Object {
Write-ColoredOutput " - $($_.Name)" -Color $InfoColor
}
# Create the zip archive
try {
# Use Compress-Archive with the -Update flag to handle overwrites
$FilesToArchive | ForEach-Object {
Compress-Archive -Path $_.FullName -DestinationPath $ZipPath -Update -ErrorAction Stop
}
Write-ColoredOutput " [OK] Archive created: $ZipFileName" -Color $SuccessColor
Write-ColoredOutput " Location: $ZipPath" -Color $SuccessColor
$ProcessedCount++
} catch {
Write-ColoredOutput " [ERROR] Failed to create archive: $($_.Exception.Message)" -Color $ErrorColor
$SkippedCount++
}
}
# Summary
Write-ColoredOutput "`n========================================" -Color $SuccessColor
Write-ColoredOutput "SUMMARY" -Color $SuccessColor
Write-ColoredOutput "========================================" -Color $SuccessColor
Write-ColoredOutput "Archives created: $ProcessedCount" -Color $SuccessColor
Write-ColoredOutput "Folders skipped: $SkippedCount" -Color $InfoColor
Write-ColoredOutput "`n[OK] Script completed successfully!`n" -Color $SuccessColor