Files with illegal filenames [duplicate]

Solution 1:

You cannot delete a file or a folder on an NTFS file system volume

Cause 5: The file name includes a reserved name in the Win32 name space If the file name includes a reserved name (for example, "lpt1") in the Win32 name space, you may not be able to delete the file. To resolve this issue, use a non-Win32 program to rename the file. You can use a POSIX tool or any other tool that uses the appropriate internal syntax to use the file.

Additionally, you may be able to use some built-in commands to bypass the typical Win32 reserved name checks if you use a particular syntax to specify the path of the file. For example, if you use the Del command in Windows XP, you can delete a file named "lpt1" if you specify the full path of the file by using the following special syntax:

del \\?\c:\path_to_file\lpt1

For more information about deleting files with reserved names under Windows NT and Windows 2000, click the following article number to view the article in the Microsoft Knowledge Base:

120716 How to remove files with reserved names in Windows

For more information about deleting files with reserved names under Windows XP, click the following article number to view the article in the Microsoft Knowledge Base:

315226 How to remove files with reserved names in Windows XP

If you open a handle to a file by using the typical Win32 CreateFile mechanism, certain file names are reserved for old-style DOS devices. For backward compatibility, these file names are not permitted and they cannot be created by using typical Win32 file calls. However, this issue is not a limitation of NTFS.

You may be able to use a Win32 program to bypass the typical name checks that are performed when a file is created (or deleted) by using the same technique that you use to traverse folders that are deeper than MAX_PATH. Additionally, some POSIX tools are not subject to these name checks.

Cause 6: The file name includes an invalid name in the Win32 name space 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.

Solution 2:

From my answer to a similar question:

Failsafe Method:

Boot to a Linux LiveCD. Ubuntu has good NTFS support, and Linux handles a lot more wonky-characters-in-filenames than Windows. The perl rename script may be included as the system's rename command, which is handy for batch-modifying many similarly-named files.


In Windows Command Shell with Short Filenames

The DOS command DIR/X shows short filenames, if they exist on your system.

$ cmd
c:\test> dir /x
 Volume in drive E is NUVOL
 Volume Serial Number is 80D3-A96D

 Directory of e:\tor\test

10/04/2009  05:15 AM    <DIR>                       .
10/04/2009  05:15 AM    <DIR>                       ..
10/04/2009  05:11 AM                 0 CLIP-2~1.MOV clip-2009-10-01 21;26;00.mov
               1 File(s)              0 bytes
               2 Dir(s)   5,201,670,144 bytes free

If they do exist, the REN command will move them to a new name; the new name can be a new (valid) long filename.

c:\test> ren CLIP-2~1.MOV "clip-2009-10-01_21-26-00.mov"

That's how to fix one.

To batch process all of them, you need to 1) grab the short filenames of all the files you want to move; 2) convert your listing into a batch file with the appropriate REN commands; and 3) run the resulting batch script. If you are comfortable with perl (or sed/awk, python, whatever), you can script this yourself, or you can craft it by hand from the listing you made in step 1.

But if DIR/X doesn't show the short filenames, your system has them disabled, and this solution won't help.