Home > Blockchain >  Can't delete Windows folder that contains a subfolder named "..." which contains a su
Can't delete Windows folder that contains a subfolder named "..." which contains a su

Time:08-07

I'm stumped. I have a folder called c:/data/ToDelete. It used to contain files as well as a single subfolder called ..., which contained the same files as well as single subfolder named ... and this went on ad nauseum.

I moved the contents of the ToDelete folder elsewhere, but the subfolder/subfolder structure remains, and I can't get rid of it.

This is what it looks like in Explorer This PC > Data > ToDelete > ... > ... > ... > ... > etc clicking on the above reveals the path: C:\Data\ToDelete\

I was able to rename the first folder (from c:/data/temp/ to c:/data/ToDelete/) but whenever I try to rename the subfolder I get the "This folder is already open" (probably because it is just referring back to it's parent folder).

Things I've tried (none worked)

  • the basics: Delete key, Shift Delete, drag to recycle bin, send to recycle bin
  • command prompt delete, including trying the ASCII code for . just in case.
  • I tried zipping it with 7zip
  • I did a chkdsk and got no errors
  • I tried removing with Revo Uninstaller Pro (the Delete folder option).
  • I tried renaming the subfolders with this powershell script that I found online thinking I could then delete them but got an error:
    Get-ChildItem -Recurse | where {$_.attributes -eq "Directory"} | Where-Object {$_.Name -match '...'} | Rename-Item -NewName { $_.Name -replace '...' , 'ToDelete' }

The funny thing is I found the folder in my backup directory as well, so somehow the entire thing can get copied (which means I have to remove it in multiple places now).

CodePudding user response:

You need to do two things to enable PowerShell to process files or folders literally named ...:

  • Use -LiteralPath instead of -Path parameter where possible, to prevent PowerShell from parsing the path.
  • Prepend \\?\ to the full path to tell the underlying Windows APIs to disable all string parsing and to send the string that follows it straight to the file system.

Example:

# For demonstration, create two nested sub folders literally named '...'
New-Item -ItemType Directory -Path '\\?\c:\data\ToDelete\...\...'

# Rename all '...' sub folders to 'ToDelete'
Get-ChildItem -LiteralPath '\\?\c:\data\ToDelete' -Directory -Recurse -Filter '...' | 
    Sort-Object { $_.Fullname.Length } -Desc | 
    Rename-Item -NewName ToDelete
  • Before renaming the folders, we must sort by path length (descending) to make sure that deepest nested folders will be renamed first. Otherwise paths enumerated by Get-ChildItem would be incorrect after renaming the first folder.

For what you have tried:

  • Note that -match '...' doesn't find folders named literally like "...". The -match operator's RHS operand is a regular expression pattern and as such would match the first sequence of any three characters in the string. The same can be said about -replace '...'. To literally match "...", use -eq '...' or escape the dots like this when used in RegEx pattern: \.\.\.
  • Related