Can't delete a folder on Windows 7 with a trailing space [closed]
Asked Answered
A

5

26

Issue: I have a Windows 7 sub-directory which I can't delete.

While I know others here, and many more elsewhere on the Internet have asked about this general class of Windows 7 file system problem, my question here specifically relates to the specific class of un-deletable files on Windows 7 which have a trailing space in the directory name.

Is there a better tool to inspect and/or edit my filesystem (in hex if need be)?


OS: I'm running x64 professional and it's fully updated.

What has been tried: I have read many web pages on this subject and tried many potential solutions. I have been studding the problem most recently using PowerShell which seems to be fully capable of dealing with system internals. At this point I am looking for something like a hex editor for the filesystem.

What it's not caused by:

  • a long file-name, or
  • by being located in a lengthy path,

What it's not fixed by:

  • Renaming using the old DOS file naming scheme
  • Running CHKDSK of the entire file system
  • Shutting down all other programs which might be accessing it
  • Disabling virus software
  • Using the Delinvfile.exe 4.5 utility. Note: Delinvfile says that it can't fix, "Files and Folders with a shortname that contains invalid characters. These include the characters [which are disallowed in file-names]:
<  -  Less than symbol
>  -  Greater than symbol
:  -  Colon
"  -  Quotation Mark
/  -  Forward Slash
|  -  Vertical Bar
?  -  Question mark
*  -  Asterisk

What caused it? In my case the un-deletable sub-directory was created some months ago with a custom PHP program that I use for source tree backups. It appears to have either a space or other bad character in the name, but I can't be sure. It is visible in a file directory, but unavailable to delete, rename, rmdir, etc.

Investigation: I can move it around on my file system and have placed it inside a sub-directory called, 'holds bad subdir' on C:.

Here you can see it with PowerShell. First I show it with a Get-ChildItem (which is the same as the alias 'dir'):

PS C:\holds bad subdir> Get-ChildItem


    Directory: C:\holds bad subdir


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        1/9/2014   3:01   AM            20120530-04

If I try to delete it in a cmd window by typing 'del "2' + tab, it completes the file name expansion as follows: del "20120530-04 ", showing that there is a space at the end of the directory name. When I execute this command the result is:

Could Not Find C:\holds bad subdir\20120530-04

If I try to delete it with del 2*, the system returns as if though it had deleted it, but does not.

If I issue this same command in PowerShell, and also with the Force option, it reports, "An object at the specified path C:\holds bad subdir\20120530-04 does not exist.", as follows:

PS C:\holds bad subdir> Remove-Item 2* -Force  Remove-Item : An object at the specified path C:\holds bad subdir\20120530-04  does not exist.At line:1 char:1
+ Remove-Item 2* -Force
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-Item], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.RemoveItemCommand

This is sort of strange because the directory can clearly see it, but any methods apparently either can't see it or can't be applied to it.

I can also view the un-deletable sub-directory in Windows Explorer. When I browse into it, it says, "this folder is empty". And if I try to delete it there I get:

Enter image description here

Also if I view the properties of this folder I can see that the name is "20120530-04 ", that is, with an extra space at the end.

Enter image description here

Also interestingly, the Security tab reports "(X) The requested security information is either unavailable or can't be displayed."

And it's not Read-only, nor Hidden.


Scope of issue: Now, this is not a big problem, it is easy to bury this sub-directory inside an out-of-the-way sub-directory and just not worry about it.

But for me this has turned into an intellectual challenge and partly a way to learn more about the guts of Windows 7. I guess I am amazed that such a bug in Windows could exist at such a low level, and with so many systems installed in the world. It's hard at this point to know if this is a Windows bug, bad data (that a bug let in), or just bad data.

Arrangement answered 12/1, 2014 at 8:32 Comment(2)
I'm wondering whether the alleged space is really some strange (unicode?) character. Perhaps a DIR >textfile might show more to a hex editor?Whang
My issues was exactly the same, except with a small detail: I couldn't even move the folder around. The command from the accepted solution finally removed it. Originally the folder was created by WinRAR, which ended up with the extra space by accident.Asleyaslope
H
92

According to You cannot delete a file or a folder on an NTFS file system volume (requires JavaScript to display), the following should work (notice it uses a UNC path).

rd "\\?\C:\holds bad subdir\20120530-04 "

Be sure to do this with cmd.exe. It does not seem to work with PowerShell's Remove-Item (rd).

Also see:

Hying answered 12/1, 2014 at 12:4 Comment(6)
Nice work Lars. You're a genius. I wouldn't have thought to use a network path. I owe you one. ..Even though the microsoft web page cautioned that this sol'n wasn't for Win 7. It was just what it took, to go thru some other OS interface to get to the thing.Arrangement
A little embarrassed I didn't find the links you cited when I researched this. I was searching for the wrong thing at the time, and hadn't yet focused in on that it had to do with trailing spaces, which I wasn't really sure of. I changed my heading as an afterthought to better characterize the problem after I posted, but then didn't re-search with new heading. Note to self: one's google search is only as good as the keywords given, so best to try more queries than one.Arrangement
finally removed the dir. tnx @Lars Truijens. in case if anybody got "directory is not empty" as "/s" at the endDomeniga
This works for me, anoying folders xDArchivolt
I reaffirm that this command works for my case. I used to try several instructions but those was failed.Currish
More than 20 years after NTFS, the bug is still there on Windows 10. And Lars workaround is still working.Grigsby
E
2

I've got a few suggestions.

Method 1: The default Path parameter in cmdlets are known to have problems with special characters. LiteralPath however should support all characters and often solves problems like the one you have.

Get-ChildItem 2012* | % { Remove-Item -LiteralPath $_.FullName -Force }

Method 2: You can try to use the shortname (8.3 filename) for the folder. This is a cmd.exe approach. You could also wrap the two commands inside cmd /c " YOUR COMMAND " to run them in PowerShell.

D:\> dir /x
 Volume in drive D is Storage
 Volume Serial Number is *******

 Directory of D:\

12.01.2014  12:29    <DIR>          APPLEI~1     Apple iOS 7 GM


D:\> rd /s d:\APPLEI~1
 d:\applei~1, Are you sure (Y/N)? y

Method 3: You could also see if a WMI approach works:

#Remember to use \\ instead of \ in the path
$fold = Get-WmiObject -Query "select * from Win32_Directory where Name = 'C:\\holds bad subdir\\20120530-04'"
$fold

If this returns nothing, try adding a space at the end in the filename. If it returns an object, run:

$fold.Delete()

If it doens't return an object both with and without the space at the end, try the following apporach using wildcard (this can take everything from 1-15 minutes to run).

#Remember to use \\ instead of \ in the path
$fold = Get-WmiObject -Query "select * from Win32_Directory where Name like 'C:\\holds bad subdir\\20120530-04%'"
$fold

And delete it if it returns the correct folder:

$fold.Delete()
Estabrook answered 12/1, 2014 at 9:54 Comment(2)
Thank you. With method 1 the 'Get-ChildItem 2012*' failed before it ever got to the pipe. And I had pretty much already tried method 2 already. And you lost me in Method 3, I'm still googling to find out what you're doing. But as it turned out the other solution here worked, and since I only had this one directory to delete, I can't test it again. But thank you for your help Frode.Arrangement
Oh, man. Thank you for keeping me from going insane with the -LiteralPath thing.Coping
W
1

Have you tried

rd "C:\holds bad subdir\20120530-04 "

or

rd /s "C:\holds bad subdir"

You say you've tried del which deleted files, but you haven't mentioned rd or its synonym rmdir wich removes directories.

Whang answered 12/1, 2014 at 8:45 Comment(1)
Yes thank you. Also in PowerShell it's the same thing: Remove-item, "SYNOPSIS Deletes files and folders."Arrangement
N
0

Boot up a Live Linux Ubuntu CDROM and see how that file manager goes.

Windows isn't supposed to support trailing spaces, but does support leading spaces.

Nanon answered 12/1, 2014 at 10:57 Comment(3)
Ubuntu is done downloading; will install if only to see how that all works. Thanks for the idea.Arrangement
Don't install, boot it off a bootable cdrom. Write the ISO to a cd or create the bootable USB version.Nanon
Nice. 4mb too big now for a cd but a usb stick worked. Thanks.Arrangement
W
0

From the parent directory, do the following, hoping to trick any helpful translation CMD is doing:

for /f "delims=" %i in ('dir /b /ad') do rd /s /q "%i"

(While you're there, it wouldn't hurt to try "%~si" as the target too...)

Whang answered 12/1, 2014 at 12:11 Comment(2)
Thank you again, but sorry, this fails Here is what is returned: Line 1of2: C:\holds bad subdir>rd /s /q "20120530-04 ", and line2of2: The system cannot find the file specified. C:\holds bad subdir>Arrangement
I've just had the same problem on Win2008 R2. I solved it using an elevated CMD shell, and doing DIR * /X - which listed out the old file-naming convention as well. There were two sub-folders, "websites" and "websites " in the same folder. However, the latter came up with an alias of WEBSIT~2 and I was able to use REN WEBSIT~2 BAD-DIR to rename it and eventually delete it. What I'd like to know is how it got there.Peterec

© 2022 - 2024 — McMap. All rights reserved.