How to delete Windows files with trailing spaces

The syntax that actually worked was:

  del "\\?\c:\path\file-with-trailing-space "

However, if the file was created on a Mac, there will be an invisible pointer file named

  "._file-with-trailing-space "

that you will also have to remove


From: http://www.tuketu.com/technology/windows/windows_file_names.htm

Windows File Names: NTFS, odd file names (aux, com, trailing/leading spaces)

NTFS filesystem limitations on file names differ from Win32 specifications. Therefore it possible, by going around win32, to create file names that not legal Win32 file names and that will cause problems for Windows Explorer and other file browsing utilities. This is a common trick for hackers to create files that can not be deleted or easily manipulated from Windows. If Windows Explorer has no security tab for a file on NTFS, or indeed will not display any attributes at all for the file, this may be the problem. The common tricks include leading and trailing spaces in a file name or using DOS device names (aux, com1, com2, or some such) which are reserved and not permitted by default in Win32.

An easy way to determine if a filename has trailing spaces, etc, is to drag the file name from windows explorer to cmd prompt window. The spaces will clearly show up.

The way to deal with these files names is to use the "\\?\c:\some-bad-filename" or "\\.\c:\some-bad-filename" naming conventions.

E.g.: from a command line, del "\\?c:\file-with-trailing-space " will delete a file with a trailing space.

See the following Microsoft knowledge base articles to deal with the issue:

  • Q320081
  • Q315226
  • Q115827
  • Q303074
  • Q120716

From: http://support.microsoft.com/kb/120716/EN-US/

How to Remove Files with Reserved Names in Windows

 Article ID	:	120716
 Last Review	:	May 6, 2003
 Revision	:	2.0

This article was previously published under Q120716

Another option is to use a syntax that bypasses the typical reserve-word checks completely. For example, you can possibly delete any file with a command such as:

 DEL \\.\driveletter:\path\filename

For example:

 DEL \\.\c:\somedir\aux

If the name in the file system appears as a directory, use the following syntax.

For example, you can possibly delete any directory with a command such as:

 RD \\.\<driveletter>:\<path>\<directory name>

For example:

 RD \\.\c:\somedir\aux

-or-

 RmDir \\.\<driveletter>:\<path>\<directory>

For example:

 RmDir \\.\C:\YourFTP_ROOT's_PATH\COM1 /s /q

/s-This switch removes all directories and files in the specified directory and also the directory itself. This switch also removes a directory tree.

/q-This switch stands for Quiet mode. Do not ask if you can remove a directory tree that contains the /s switch.


Page last modified on March 23, 2005, at 12:21 AM