I have a bunch of directories on my file servers that are ridiculously long: think 2,000 directories and 20,000+ characters deep.
There are tons of articles and strategies using Robocopy (w/purge or mir) to get rid of these--none of them worked.
I'm using PowerShell with the Windows API to both recursively get the deepest parts of the path and to delete the contents (using the 'wide' versions of the relevant functions).
So the process I'm performing is:
- Get a recursive list of all of the directory paths of the offending deep target
- Loop through the list (backwards) and RemoveDirectory() each path
The problem I'm running into is that the Kernel32, RemoveDirectory() function appears to be horrendously slow for some reason.
Am I doing something wrong or is the slowness due to the depth of the directory or something else someone could help with?
Add-Type -TypeDefinition $filesearcher
$delete = @'
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern bool RemoveDirectory(string path);
'@
Add-Type -MemberDefinition $delete -Name 'Kernel32'
#[Reflection.Assembly]::LoadWithPartialName('System.Collections.Generic')
$f = @()
$d = @()
$d1 = "\\?\C:\temp\temp"
if (([FileSearcher]::FindNextFilePInvokeRecursiveParalleled($d1, [ref]$f, [ref]$d)))
{
for ($i=($d.Count-1); $i -gt 0; $i--) {
"$($d.Count - ($d.Count - $i))/$($d.Count)"
if (![Microsoft.PowerShell.Commands.AddType.AutoGeneratedTypes.Kernel32]::RemoveDirectory($d[$i].FullName)) {
break
}
}
}
How I'm creating the horrendously deep directory as a test-case:
mkdir 'C:\temp2'
$p = 'C:\temp'
0..5000 | % { $p += '\temp' }
.\Robocopy.exe c:\temp2 $p /MIR /E