Reputation: 163
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)
.
just in case. 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).
Upvotes: 1
Views: 302
Reputation: 27766
You need to do two things to enable PowerShell to process files or folders literally named ...
:
-LiteralPath
instead of -Path
parameter where possible, to prevent PowerShell from parsing the path.\\?\
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
Get-ChildItem
would be incorrect after renaming the first folder.For what you have tried:
-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: \.\.\.
Upvotes: 2