105

I got some folders created by malware whose name ended with a dot like C:\a.\ or C:\b.\, etc.

I found a solution that can remove such folder with command rd /q /s "C:\a.\" but if I call win API RemoveDirectory, it returns ERROR_FILE_NOT_FOUND.

And I just wonder how to write a function to delete such directory, thanks

I test on my own Windows XP SP3 system like this

create a folder C:\>mkdir a..\\\ and I cannot double click to access this folder. and I can remove with command rd /q /s "C:\a.\"

what Windows system API(s) that rd /q /s command call?

2
  • I cannot believe this still happens in Windows 10, by Windows update itself (it could not delete the Windows.old after updating). Jun 16, 2018 at 9:54
  • Is it realistic for you to open a command shell programmatically from within your program and call the RD command?
    – bduhbya
    Mar 18, 2022 at 23:53

10 Answers 10

189

Here's a solution to this problem:

rd /s "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder."
7
  • 2
    The question wants an API function to remove the folder, and states it can be removed with rd.
    – Jcl
    Jun 4, 2014 at 8:26
  • 4
    Fair enough - maybe that's why it's not marked as the accepted answer, but it is nevertheless very useful for those of use stuck with the same problem but where we can use rd. Feb 4, 2015 at 13:09
  • 11
    Note that this only works from CMD.exe NOT PowerShell
    – Lorne K
    Apr 13, 2016 at 15:40
  • 2
    also try to run CMD.exe in administrator mode too if it does not work in the non elevated command prompt
    – greg
    Aug 30, 2017 at 17:15
  • 1
    @Gábor By default, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, prepend "\\?\" to the path. For more information, see Naming Files, Paths, and Namespaces. Jul 10, 2023 at 10:16
83

Solution: When you call RemoveDirectory, make sure that you prefix the path with the string "\\?\".


Explanation: It has everything to do with the dot. According to MSDN, there are certain cases where you may not be able to delete a file or folder on an NTFS volume, specifically when the file name is invalid in the Win32 name space (which is why you are unable to open the file using the normal methods in Windows Explorer).

You may not be able to delete a file if the file name includes an invalid name (for example, the file name has a trailing space or a trailing period or the file name is made up of a space only). To resolve this issue, use a tool that uses the appropriate internal syntax to delete the file. You can use the "\\?\" syntax with some tools to operate on these files, for example:
del "\\?\c:\path_to_file_that contains a trailing space.txt "
The cause of this issue is similar to Cause 4. However, if you use typical Win32 syntax to open a file that has trailing spaces or trailing periods in its name, the trailing spaces or periods are stripped before the actual file is opened. Therefore, if you have two files in the same folder named "AFile.txt" and "AFile.txt " (note the space after the file name), if you try to open the second file by using standard Win32 calls, you open the first file instead. Similarly, if you have a file whose name is just " " (a space character) and you try to open it by using standard Win32 calls, you open the file's parent folder instead. In this situation, if you try to change security settings on these files, you either may not be able to do this or you may unexpectedly change the settings on different files. If this behavior occurs, you may think that you have permission to a file that actually has a restrictive ACL.

(Source: http://support.microsoft.com/?kbid=320081)

5
  • 1
    Worked perflectly for me to delete a file with its name finishing by ". ". Thank you very much !
    – AFract
    Jan 23, 2015 at 14:33
  • 2
    It worked switching del to rd like rd "\\?\C:\Users\ahmed\Desktop\buggy."
    – guneysus
    Feb 18, 2015 at 10:00
  • 1
    This works great on cmd but is there an equivalent for PowerShell? rd in pwsh is an alias of remove-item which does not support `\\?\`. Nov 17, 2017 at 2:19
  • 2
    Sure it does. I just used PowerShell's rd to delete such a file, and it worked just fine. Double-check your syntax. May 28, 2018 at 22:59
  • 1
    Wow. Stumbled on this issue while trying to do some Natural Language Processing on a dataset (every file in the dataset is named {IncrementalNumber}.), and I was very confused about why my data processing scripts were failing. This now makes sense, but it seems like the behavior is so unexpected that it could have security ramifications due to applications incorrectly handling filenames ending in a space or period.
    – Spencer D
    Sep 12, 2019 at 21:48
56

Ive posted this on SU and I decided to post it here too. Its the simplest and fastest and easiest way to achieve this. I am now laughing at how much simple it is.

  1. Install WinRAR
  2. Follow the Step by Step procedure from pictures:
  3. enter image description here
  4. enter image description here
  5. enter image description here
  6. enter image description here

I myself had WinRaR installed so I decided to demonstrate the workaround in it.
This workaround is also possible by using 7zip.

One another thing I should mention is that, as it seems the problem is caused by using windows explorer and any other file browser (like winrar file browser itself, ftp explorers etc.) will treat this files as normal.
You could try using any file browser and simply delete those files and not bother archiving them though! Cheers!

4
  • 7
    What a creative solution!
    – Nosgoroth
    Aug 18, 2016 at 13:42
  • it's works on file but not works on directory Sep 1, 2021 at 7:37
  • 2
    it works with 7zip :)) Nov 7, 2022 at 9:51
  • @HảoNguyễn: I just opened 7ZFM.exe and saw the bad directory. I could not directly manually delete it (Which was not the suggestion given here, I know.), but I could rename it. And delete it. All directly with 7ZFM. Oct 24, 2023 at 18:10
16

If you have git installed (you can get ir from here) then it is as simple as:

  1. Navigate File Explorer to location where problematic folder is located.
  2. Context menu (right mouse button) > Git Bash Here.
  3. rm -rf Foldername./
2
  • rm: cannot remove ‘.bin’: Directory not empty
    – Green
    Oct 25, 2017 at 2:11
  • 1
    @Green if you have permissions to delete folder content, the -rf part should take care of folder contents. Oct 25, 2017 at 7:27
8

When you see the name is "a.", but the actual name is "a.."

Try this:

rd /q /s "C:\a..\"

And you can try explore the folder by this code:

for /f "tokens=3 delims=<>" %%a in ('dir /ad /x "C:\*" ^| findstr " a\.\.$"') do (
  for /f "tokens=1" %%b in ("%%a") do start "" "%%~fb"
)
5

I used "WinRar" A simple RAR, ZIP processor. You can use any sort of file name editor. Just open the directory where your file is into WinRar and select rename after right clicking the file/folder you want to rename and fill in the new name.

3
  • How is that supposed to help?
    – Tavo
    Aug 14, 2015 at 16:49
  • 1
    Helps you browse the file/folder and rename the file to whatever...and done.
    – Aryan
    Aug 14, 2015 at 17:01
  • 2
    This actually helped me; I had used WinRar to unzip a bunch of files, one of which included a folder ending in a period. Windows couldn't delete it or really rename it, but I could rename it no problem in WinRar and then delete it in Windows Explorer (win 10). Thanks!
    – may
    Jun 24, 2016 at 14:36
5

If you need to keep the data you can also use the \\?\ trick for renaming the folder.

ren "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder." "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder"

This is an ideal solution if you need to know what is inside the folder or if the data is important.

This works in both Command Prompt and PowerShell.

2
  • You can also CD into the folder with Command Prompt using its short name and see the files without renaming. However, to open files in external programs, you will need to copy them elsewhere, as the path will be unreachable. Jul 10, 2023 at 10:24
  • This did not worked for me. System can't find file, even with "\\?\". Used CMD. What can definitely work for renaming is using dir /x command to get short form name like this DECEIV~1. Then use ren with this short name to rename the folder. Jul 10, 2023 at 10:30
0

Try to use unlocker program to delete files and folders that you can't delete normally.

2
  • 5
    I'm curious about how can unlocker do this!
    – jerry.liu
    Nov 2, 2010 at 8:05
  • @jerry.liu probably by using low level APIs with "\\?\" prefix. Jul 10, 2023 at 10:25
0

if you want to keep the files theres options in bash as well.
you will require the Windows Subsystem for Linux package (i have Ubuntu installed)

to keep the files. open a command prompt and cd over to where the file or folder is located.
now type "bash"
this will open bash in the prompt. now enter mv '[folder or file you want to move]' '[new name (can include path)]' (theres more to mv so if you want to read up on all of its options use 'man mv' this will open its manual page (then use q to return to bash))
the mv command is short for move, but its has a secondary function of renaming things.
also in bash use 'single quotes' and not a normal "double quote", as bash expects 'single quotes'.

heres a example. assume your folder is named "data 1." located in c:\users (so the full path to the error folder is c:\users\data 1.
1. open command prompt using any method
2. enter cd c:\users
3. now type bash this loads bash in the folder you previously were in
4. finally type mv 'data 1.' 'data 1'
5. the folder is now accessible and you can choose to delete it.

-2

Use bash rm command from Ubuntu on Windows 10

1
  • 1
    rm: cannot remove ‘.bin’: Directory not empty
    – Green
    Oct 25, 2017 at 2:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.