Skip to content

Instantly share code, notes, and snippets.

@enoch85
Last active September 25, 2025 13:45
Show Gist options
  • Select an option

  • Save enoch85/a3bd063aa6ce35ef0caed407e1c79cc5 to your computer and use it in GitHub Desktop.

Select an option

Save enoch85/a3bd063aa6ce35ef0caed407e1c79cc5 to your computer and use it in GitHub Desktop.
# Test script to debug folder detection logic and regex extraction
# This mimics the behavior in the main rclone script
$BackupConfig = @{
RCLONE_PATH = "C:\rclone.exe"
REMOTE_NAME = "nextcloud"
REMOTE_DIR = "SERVERS_DATA"
}
# Exclude Patterns - copy from main script to test extraction
$ExcludePatterns = @{
SYSTEM_EXCLUDES = @(
"/Windows/**", "Windows/**",
"/Windows.old/**", "Windows.old/**",
"/Program Files/**", "Program Files/**",
"/Program Files (x86)/**", "Program Files (x86)/**",
"/Recovery/**", "Recovery/**",
"/PerfLogs/**", "PerfLogs/**",
"/pagefile.sys", "pagefile.sys"
)
VENDOR_EXCLUDES = @(
"[Rr]icoh/**", "**/[Rr]icoh/**", "*[Rr]icoh*",
"[Xx]elent/**", "**/[Xx]elent/**", "*[Xx]elent*",
"[Ii]nfracom/**", "**/[Ii]nfracom/**", "*[Ii]nfracom*",
"[Dd]atto/**", "**/[Dd]atto/**", "*[Dd]atto*",
"[Mm]icrosoft/**", "**/[Mm]icrosoft/**", "*[Mm]icrosoft*"
)
GENERIC_EXCLUDES = @(
"[Ll]ogs/**", "**/[Ll]ogs/**", "*[Ll]ogs*",
"[Tt]emp/**", "**/[Tt]emp/**", "*[Tt]emp*",
"[Cc]ache/**", "**/[Cc]ache/**", "*[Cc]ache*"
)
}
Write-Host "=== TESTING REGEX FOLDER EXTRACTION ===" -ForegroundColor Cyan
# Test the regex extraction logic from main script
$foldersToCheck = @()
$allExcludeCategories = @(
$ExcludePatterns.VENDOR_EXCLUDES,
$ExcludePatterns.GENERIC_EXCLUDES,
$ExcludePatterns.SYSTEM_EXCLUDES
)
foreach ($excludeCategory in $allExcludeCategories) {
foreach ($pattern in $excludeCategory) {
Write-Host "Processing pattern: '$pattern'" -ForegroundColor Yellow
# Case 1: Character class patterns like "[Rr]icoh/**", "[Xx]elent/**"
if ($pattern -match '^\*?\*?/?(\[[A-Za-z]\][A-Za-z0-9_\-\.]*(?:\[[A-Za-z]\][A-Za-z0-9_\-\.]*)*)/') {
$charClassPattern = $matches[1]
Write-Host " Found character class: $charClassPattern" -ForegroundColor Cyan
# Convert [Rr]icoh to Ricoh and ricoh
$folderBase = $charClassPattern
while ($folderBase -match '\[([A-Za-z])\](.*)') {
$char = $matches[1]
$rest = $matches[2]
# Create both upper and lower case versions
$upperVersion = $folderBase -replace '\[[A-Za-z]\]', $char.ToUpper()
$lowerVersion = $folderBase -replace '\[[A-Za-z]\]', $char.ToLower()
$foldersToCheck += $upperVersion
$foldersToCheck += $lowerVersion
Write-Host " Added: $upperVersion, $lowerVersion" -ForegroundColor Green
break # Process one character class at a time
}
}
# Case 2: Plain folder patterns like "Windows/**", "/Program Files/**"
elseif ($pattern -match '^\*?\*?/?([A-Za-z0-9_\-\.\s\(\)]+)/?\*') {
$folderName = $matches[1]
$foldersToCheck += $folderName
Write-Host " Added: $folderName" -ForegroundColor Green
}
# Case 3: File patterns like "pagefile.sys", skip these
elseif ($pattern -match '\.(sys|log|tmp)$') {
Write-Host " Skipped file pattern: $pattern" -ForegroundColor Gray
}
else {
Write-Host " No match for: $pattern" -ForegroundColor Red
}
}
}
# Remove duplicates
$foldersToCheck = $foldersToCheck | Sort-Object -Unique
Write-Host "`n=== EXTRACTED FOLDERS ===" -ForegroundColor Cyan
Write-Host "Folders to check: $($foldersToCheck -join ', ')" -ForegroundColor Yellow
Write-Host "Total folders: $($foldersToCheck.Count)" -ForegroundColor Yellow
# Test the destination path
$dest = "$($BackupConfig.REMOTE_NAME):$($BackupConfig.REMOTE_DIR)/$env:COMPUTERNAME/C"
Write-Host "`n=== TESTING FOLDER DETECTION ===" -ForegroundColor Cyan
Write-Host "Testing folder detection for: $dest" -ForegroundColor Yellow
# Get remote directory listing
Write-Host "`nRemote directory contents:" -ForegroundColor Cyan
$lsdOutput = & ($BackupConfig.RCLONE_PATH) lsd $dest 2>$null
if ($lsdOutput) {
$lsdOutput | ForEach-Object { Write-Host " '$_'" -ForegroundColor Gray }
Write-Host "`n=== FOLDER MATCHING TEST ===" -ForegroundColor Cyan
$foldersToDelete = @()
foreach ($folder in $foldersToCheck) {
Write-Host " Checking folder: '$folder'" -ForegroundColor Cyan
# Extract all remote folder names first
$remoteFolders = @()
foreach ($line in $lsdOutput) {
$parts = $line -split '\s+'
$remoteFolders += $parts[-1].Trim()
}
Write-Host " Remote folders: $($remoteFolders -join ', ')" -ForegroundColor DarkGray
# Now check for matches using simpler approach
$foundMatch = $false
$matchedFolder = ""
foreach ($remoteFolder in $remoteFolders) {
Write-Host " Testing '$remoteFolder' against '$folder'" -ForegroundColor DarkGray
# Direct string comparisons
if ($remoteFolder -eq $folder) {
Write-Host " ✅ EXACT MATCH!" -ForegroundColor Green
$foundMatch = $true
$matchedFolder = $remoteFolder
break
} elseif ($remoteFolder.ToLower() -eq $folder.ToLower()) {
Write-Host " ✅ CASE INSENSITIVE MATCH!" -ForegroundColor Green
$foundMatch = $true
$matchedFolder = $remoteFolder
break
} elseif ($remoteFolder -like "*$folder*") {
Write-Host " ✅ CONTAINS MATCH!" -ForegroundColor Green
$foundMatch = $true
$matchedFolder = $remoteFolder
break
} elseif ($folder -like "*$remoteFolder*") {
Write-Host " ✅ REVERSE CONTAINS MATCH!" -ForegroundColor Green
$foundMatch = $true
$matchedFolder = $remoteFolder
break
}
}
if ($foundMatch) {
$foldersToDelete += $folder
Write-Host "✅ FOUND: $folder (matched remote folder: $matchedFolder)" -ForegroundColor Green
} else {
Write-Host "❌ Not found: $folder" -ForegroundColor Red
}
}
Write-Host "`n=== SUMMARY ===" -ForegroundColor Cyan
if ($foldersToDelete.Count -gt 0) {
Write-Host "Folders that would be deleted: $($foldersToDelete -join ', ')" -ForegroundColor Yellow
} else {
Write-Host "No folders found for deletion" -ForegroundColor Red
}
} else {
Write-Host "Could not connect to remote or no rclone available" -ForegroundColor Red
}
Write-Host "`n=== END TEST ===" -ForegroundColor Cyan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment