How to delete a folder that name ended with a dot (".")?

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?


Solution 1:

Here's a solution to this problem:

rd /s "\\?\C:\Documents and Settings\User\Desktop\Annoying Folder."

Solution 2:

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)

Solution 3:

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!

Solution 4:

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./

Solution 5:

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"
)