Search code examples
windowspowershellwindows-server-2012-r27zippowershell-5.0

How to remove a directory that ends with a dot (.) on windows?


I created a file by mistake, and for the life of me, I cannot remove it. If you have 7-Zip installed, you can produce it. If not, it is easy to install.

Here is how the file gets created:

PS C:\temp> mkdir abc
PS C:\temp> cd abc

PS C:\temp\abc> & 'C:\Program Files\7-Zip\7z.exe' a -spf .\DuoWindowsLogon.admx,.\en-us\DuoWindowsLogon.adml

Creating archive: .\DuoWindowsLogon.admx,.\en-us\DuoWindowsLogon.adml

Later, I learned the correct command I should type is:

& 'C:\Program Files\7-Zip\7z.exe' a abc.7z -spf DuoWindowsLogon.admx en-us\DuoWindowsLogon.adml

After realizing the error, I tried to remove the directory erroneously created:

PS C:\temp\abc> ls
d-----       11/15/2020  11:09 PM                DuoWindowsLogon.admx,.

PS C:\temp\abc> ls D*
ls : Could not find item C:\temp\abc\DuoWindowsLogon.admx,..

PS C:\temp\abc> ls | rm
rm : Cannot find path 'C:\temp\abc\DuoWindowsLogon.admx,.' because it does not exist.

PS C:\temp\abc> rm *
rm : An object at the specified path C:\temp\abc\DuoWindowsLogon.admx,. does not exist.

PS C:\temp\abc> cd ..
PS C:\temp> rm -recurse abc
rm : Could not find a part of the path 'C:\temp\abc\DuoWindowsLogon.admx,'.

As you see, nothing worked. I also tried the file explorer (GUI), it does not work either. How can I have the directory deleted?

PS:

I tried "del /s" on CMD prompt, it did not produce an error but the file is not deleted:

c:\>del /s c:\temp\abc
c:\temp\abc\*, Are you sure (Y/N)? Y

c:\>dir c:\temp\abc
11/15/2020  11:09 PM    <DIR>          DuoWindowsLogon.admx,.

Solution

  • You can remove the directory that ends with a "." by prefixing it with \\?\

    So, in your case you can use:

    Remove-Item `\\?\C:\temp\abc\DuoWindowsLogon.admx,.` -Recurse
    

    This is documented here: You can't delete a file or a folder on an NTFS file system volume

    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.