From 77e62f43bacb0f89e2c48838a41b5e2817160880 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 30 Nov 2026 23:32:43 +0000 Subject: [PATCH] filesync: Duplicates handles paths over 260 characters Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01DU3cH4EF57KeQS5Ysv1G3i --- filesync.ps1 | 66 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/filesync.ps1 b/filesync.ps1 index 9449248..7fc9814 100644 --- a/filesync.ps1 +++ b/filesync.ps1 @@ -25,7 +25,8 @@ # and only an older copy with a newer one, after showing the list and asking. # Only Duplicates (8) deletes, and only to the Recycle Bin, after showing the list # and asking. Network and mapped drives have no Recycle Bin, so there it warns -# that the delete is permanent and asks you to type yes. +# that the delete is permanent and asks you to type yes. A file whose path is too +# long for the Recycle Bin is first moved to \~filesync-bin, then recycled. # OneDrive / SharePoint folders work too; online-only files are downloaded when # read or copied. Local, network and mapped drives are fine, and so are paths # longer than 260 characters. @@ -271,6 +272,12 @@ public static class LongPath { static extern bool CreateDirectoryW(string path, IntPtr sa); [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] static extern uint GetFileAttributesW(string path); + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + static extern bool DeleteFileW(string path); + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + static extern bool RemoveDirectoryW(string path); + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + static extern bool MoveFileExW(string src, string dst, uint flags); static string P(string path) { if (path.StartsWith(@"\\?\")) return path; if (path.StartsWith(@"\\")) return @"\\?\UNC\" + path.Substring(2); @@ -285,6 +292,9 @@ public static class LongPath { public static bool FileExists(string path) { uint a = GetFileAttributesW(P(path)); return a != 0xFFFFFFFFu && (a & 0x10u) == 0; } public static bool DirExists(string path) { uint a = GetFileAttributesW(P(path)); return a != 0xFFFFFFFFu && (a & 0x10u) != 0; } public static void Copy(string src, string dst, bool overwrite) { if (!CopyFileW(P(src), P(dst), !overwrite)) throw Err(); } + public static void Delete(string path) { if (!DeleteFileW(P(path))) throw Err(); } + public static void RemoveDirectory(string path) { if (!RemoveDirectoryW(P(path))) throw Err(); } + public static void Move(string src, string dst) { if (!MoveFileExW(P(src), P(dst), 0)) throw Err(); } public static void CreateDirectory(string path) { path = path.TrimEnd('\\'); if (path.Length <= 3 || DirExists(path)) return; @@ -309,6 +319,15 @@ function New-Folder([string]$path) { function Copy-File([string]$src, [string]$dst, [bool]$overwrite) { if ($LongPaths) { [LongPath]::Copy($src, $dst, $overwrite) } else { [IO.File]::Copy($src, $dst, $overwrite) } } +function Remove-File([string]$path) { + if ($LongPaths) { [LongPath]::Delete($path) } else { [IO.File]::Delete($path) } +} +function Remove-Folder([string]$path) { + if ($LongPaths) { [LongPath]::RemoveDirectory($path) } else { [IO.Directory]::Delete($path) } +} +function Move-File([string]$src, [string]$dst) { + if ($LongPaths) { [LongPath]::Move($src, $dst) } else { [IO.File]::Move($src, $dst) } +} # Folder part of a path without the .NET length check function Get-ParentPath([string]$path) { $path.Substring(0, $path.LastIndexOf('\')) } @@ -672,11 +691,30 @@ function Test-RecycleBin([string]$folder) { try { return ((New-Object IO.DriveInfo $root).DriveType -ne 'Network') } catch { return $false } } -# Delete through the Windows shell: the only route that behaves on OneDrive folders -function Remove-ToBin([string]$path, [bool]$bin, [bool]$folder = $false) { - $how = if ($bin) { 'SendToRecycleBin' } else { 'DeletePermanently' } - if ($folder) { [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory($path, 'OnlyErrorDialogs', $how) } - else { [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($path, 'OnlyErrorDialogs', $how) } +# Delete through the Windows shell: the only route that behaves on OneDrive folders. +# The shell cannot take paths over 260 characters, so such a file is first moved (same +# drive, so instant) to a short name under $Holding and recycled from there; an empty +# folder that long is simply removed. $Holding is $null when deleting permanently. +function Remove-ToBin([string]$path, [bool]$bin, [bool]$folder = $false, [string]$Holding = $null) { + if (-not $bin) { + if ($folder) { Remove-Folder $path } else { Remove-File $path } + return + } + if ($folder) { + if ($path.Length -gt 247) { Remove-Folder $path } + else { [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteDirectory($path, 'OnlyErrorDialogs', 'SendToRecycleBin') } + return + } + if ($path.Length -gt 259 -and $Holding) { + New-Folder $Holding + $leaf = $path.Substring($path.LastIndexOf('\') + 1) + $script:HoldingN++ + $short = "$Holding\$script:HoldingN-$leaf" + if ($short.Length -gt 259) { $short = $short.Substring(0, 259) } + Move-File $path $short + $path = $short + } + [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile($path, 'OnlyErrorDialogs', 'SendToRecycleBin') } function Invoke-Duplicates([string]$Which) { @@ -740,6 +778,8 @@ function Invoke-Duplicates([string]$Which) { } Add-Type -AssemblyName Microsoft.VisualBasic + $holding = if ($bin) { "$root\~filesync-bin" } else { $null } + $script:HoldingN = 0 $done = 0; $n = 0 $failed = New-Object System.Collections.Generic.List[string] $dirs = @{} @@ -747,8 +787,8 @@ function Invoke-Duplicates([string]$Which) { $n++ try { Say " [$n/$($todo.Count)] $($d.Rel)" 'DarkGray' - Remove-ToBin $d.File.FullName $bin - $dirs[$d.File.DirectoryName] = $true + Remove-ToBin $d.File.FullName $bin $false $holding + $dirs[(Get-ParentPath $d.File.FullName)] = $true $done++ } catch { $failed.Add("$($d.Rel): $($_.Exception.Message)") @@ -760,16 +800,20 @@ function Invoke-Duplicates([string]$Which) { $cur = $dir while ($cur.Length -gt $root.Length -and $cur.StartsWith($root, [StringComparison]::OrdinalIgnoreCase)) { try { - if (-not [IO.Directory]::Exists($cur)) { break } - if (@([IO.Directory]::EnumerateFileSystemEntries($cur) | Select-Object -First 1).Count) { break } + if (-not (Test-PathQuiet $cur 'Container')) { break } + if (@(Get-ChildItem -LiteralPath $cur -Force -ErrorAction Stop).Count) { break } Remove-ToBin $cur $bin $true $removed++ } catch { break } - $cur = [IO.Path]::GetDirectoryName($cur) + $cur = Get-ParentPath $cur } } + if ($holding -and (Test-PathQuiet $holding 'Container')) { + try { if (-not @(Get-ChildItem -LiteralPath $holding -Force -ErrorAction Stop).Count) { Remove-Folder $holding } } catch {} + } $where = if ($bin) { 'sent to the Recycle Bin' } else { 'deleted' } Say " DONE - $done of $($todo.Count) $where$(if ($removed) { ", $removed empty folder(s) removed" })" $(if ($failed.Count) { 'Yellow' } else { 'Green' }) + if ($script:HoldingN) { Say " $script:HoldingN file(s) had paths too long for the Recycle Bin, so they were moved to $holding first; restoring them puts them back there." 'Yellow' } Show-Errors $failed }