Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
Markdown Monster - The Markdown Editor for Windows

Deleting Problem Folders and Files on Windows: Could not find this Item Error


:P
On this page:

I ran into a nasty folder naming issue recently where I was unable to delete a number of folders on my server.

After some digging I figured out that the folders were created by the FTP server and a client application that was uploading files via FTP, but failed to trim trailing spaces of input on the publish folder. It's an old legacy application and the field input defaults to the extra spaces that weren't trimmed off before sending to the server resulting in the server happily creating folders with spaces.

Windows apparently does not like folders (or files) with trailing spaces.

The problem is that Explorer can see the files, but can't delete them. Some applications would see the folders (Explorer primarily) while others (like the Windows Command Prompt) don't. When I tried to delete the folders I'd get this lovely error message:

Could not Find Item

This no longer located in <folder/file location>. Verify the item's location and try again.

The file is obviously there and I'm pointing at the folder to delete, but alas... the folder won't delete. Welcome to Windows using multiple different APIs to work with file information.

It's There - and it's Not

Some interesting things happen with this: If I do a directory listing of the parent folder, the 'misnamed' folder(s) (there are several of them in this root folder) using the command window or PowerShell they don't show in the listing:

Notice that none of the problem folders are showing up in the directory listing except the gorgeview-guidebook which was manually created without trailing spaces. Even using DIR /x which should show short/fixed up filenames, in a command window didn't show those missing folders.

I tried a bunch of stuff that didn't work:

  • Renaming the folder (file can't be found or file exists already)
  • Moving the folder to a new folder to delete the parent (didn't find to move)
  • Using Windows Terminal commands (REN/DEL) even with full paths

None of that worked. What's going on?

Part of the confusion seems to be that some operations/applications can see the files and others can't, sometimes even inside of the same application - like seeing the file in Explorer and then not finding it trying to delete it. It looks like two different APIs being used - one that can deal with extended files and one that cannot.

Files and Folders with Trailing Spaces

It turns out the issue is in my case is trailing spaces in the folder names. The folder names include trailing spaces and this appears to throw off some of the older Windows APIs that return directory information.

Here's what this looks like in Explorer:

Notice how the cursor in the folder edit textbox is way out to the right which is indicative of the extra spaces in the file name. Renaming here by trimming the spaces just fails, as does trying to move the folder to a new location.

This behavior is similar to files that have full paths longer than the old 256 char MAX_LENGTH. If an application creates filenames or nested paths that are longer than 256 chars long Explorer and most commands can't deal with those either using standard file operations.

Deleting the Folders with Extended Path Format

The solution to deleting the files is to use the Windows Extended Path Format (\\?\ path prefix) when deleting the folder from the command line. Windows since version 7 has long path and extra characters in file names support via a special extended path syntax that uses a path-like prefix.

File Deletions are Permanent!

The following commands will delete files on your system and you won't be able to restore them. So be very, very careful and if possible run non-destructive commands like listings first before deleting files. Use the following commands at your own risk.

Rather than using just a simple path like the following , which does not work:

rd /s "C:\Web Sites\docs.west-wind.com\faq                           "

you have to use the extended path syntax that supports long paths and apparently various special cases like trailing spaces.

This does work:

rd /s "\\?\C:\Web Sites\docs.west-wind.com\faq                           "

The \\?\ relates to Extended Path Syntax that supports long filenames as well as apparently being more lax with spaces in path names. The paths I use above still needs to be fully qualified and must include the trailing spaces! To capture those trailing spaces I go into Explorer and copy the path from the address bar and paste it into the command line surrounded by quotes.

There's more info on the extended path syntax in the Microsoft Docs:

Naming Files, Paths and Namespaces

This article talks mainly about long file names, but it looks like that's basically the same issue of file names that the Classic Windows file APIs can't deal with. Although I'm having this issue with folders here, the same problem with extra spaces applies to files as well.

I didn't try renaming once I got the folders to delete, but I suspect you can also use other commands like ren or del to manipulate files as long as you use the extended path syntax all files involved in the operation.

Yay!

Summary

It's good to know that the extended path syntax using the \\?\ prefix can solve some funky filename issues. It solved the trailing space issue here and it can also be useful for dealing deeply nested file and folder paths or extra long file names in nested paths that exceed 256 characters.

It'd be even nicer if Windows just worked with long and 'spaced out' file names all the time, but there are a million different file APIs and most of them only support extended format paths with the \\?\ syntax which host applications likely aren't using consistently. Heck even Explorer is using the legacy APIs it seems.

The \\?\ syntax is a good reminder to - and an easy one to forget since I've been here before - and one of the reasons I decided to write this down in this post. Hopefully this might prove useful to a few others as well.

this post created and published with the Markdown Monster Editor
Posted in Windows  

The Voices of Reason


 

Joy Tan
February 11, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Hi Rick,

I had this problem for the longest time ever!! You got this solved!! YAY!!!

Joy


Earthbound_X
April 11, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Worked for me too, thanks!


Xiul
April 19, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Hi, great solution, I can't delete that folder with all others options... but yours works!!! yeahhhh....


Franck
May 03, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

I spent a morning on a doomned folder that I couldnt remove and I finally tried these command. All I can say is THANK YOU for sharing this. I really didnt know this command.


DamienBrock
May 08, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

โ€œLong Path Toolโ€ is very helpful for this error! best solution for your problem.


Dan
May 08, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Thank you for this. Maybe someone should inform the MS staff on the support forums about this so I don't waste my time reading their solutions that don't work!


Harold Lee
May 14, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Hey Rick,

Have you tried the 'LongPathTool' program. After my free download, virtually two years ago, it has never let me down.

Thank Me later.

Regards,

Lee.


Eniela
May 18, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

I use LongPathTool and it has been pretty easy. ๐Ÿ˜ƒ


Brian
May 22, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Thank you, thank you, thank you. I've tried a dozen different methods and none worked until this.


Krisztina
June 24, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Due to a programming error I accidentally created dozens of nested folders on my hard drive, 39GB data in a fraction of a second. I tried everything to delete the folders but due to the nested structure the path became so long that Explorer simply couldn't handle it. Your solution was the only thing that worked and I was able to delete all that redundant data, thank you so much!


Jon K
July 27, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Been digging through so many outdated articles and posts from various website the past couple hours to no avail, until I finally came across this one. Thank you so so much for an actual solution. My OCD thanks you.


damien brock
August 07, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Ever I have a problem not being able to delete a file or folder because the filename is invalid, I search this issue in internet, there is a Tool called long path tool, I used it and it works for me


Robin
August 09, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

You've made an old man very happy!

[That's rather sad as it was just 800 kb or so in one stray file but the little b was had been annoying me for days ๐Ÿ˜ƒ ]


CJ
December 12, 2020

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

I have tried SO Many things and None of them worked until I found your article.

Pesky files Gone! Thank you!


Morten79
January 19, 2021

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

OMG you're such a nice man. After digging and searching for over two hours I finally found your blog article. I finally got rid of that mysterious folder and I thank you many times. I was nearly getting crazy ๐Ÿ˜„


Scott
February 28, 2021

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

Hello, I tried using this for deleting a problematic file, and it ended up deleting all files on my desktop (and they're not in the recycle bin). Is there any way I can recover them? Thanks,


Scott Claus
March 07, 2021

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

I tried using this command to get rid of an errant file (it worked) and it ended up deleting (or making everything disappear) on my desktop...I'm getting some things back here and there by using Windows Recovery...I see some things I can't get to at the path \?\C:...any suggestions how to get the folders back that were obliterated by using this command? Gladly will donate if I can solve this problem...


Rick Strahl
March 08, 2021

# re: Deleting Problem Folders and Files on Windows: Could not find this Item Error

@Scott - if you deleted files, they are deleted and gone and you can't get them restored as you're doing it at the command line - they're not going to the recycle bin.

You can try tools like Recuva which can restore deleted files if they haven't been overwritten otherwise. This doesn't always work though especially if you don't do it immediately.


West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024