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

WindowsWinapiFile Io

Windows Problem Overview


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?

Windows Solutions


Solution 1 - Windows

Here's a solution to this problem:

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

Solution 2 - Windows

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 - Windows

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:

enter image description here 4.
enter image description here

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 - Windows

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 - Windows

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

Solution 6 - Windows

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.

Solution 7 - Windows

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

Solution 8 - Windows

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.

Solution 9 - Windows

Use bash rm command from Ubuntu on Windows 10

Solution 10 - Windows

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjerry.liuView Question on Stackoverflow
Solution 1 - WindowsSkilldrickView Answer on Stackoverflow
Solution 2 - WindowsCody GrayView Answer on Stackoverflow
Solution 3 - WindowsAmirHosseinView Answer on Stackoverflow
Solution 4 - WindowsLaurynas LazauskasView Answer on Stackoverflow
Solution 5 - WindowsneorobinView Answer on Stackoverflow
Solution 6 - WindowsAryanView Answer on Stackoverflow
Solution 7 - WindowsRyuuGanView Answer on Stackoverflow
Solution 8 - WindowsPHOENiXView Answer on Stackoverflow
Solution 9 - WindowskrekerView Answer on Stackoverflow
Solution 10 - Windowsmer2329View Answer on Stackoverflow