How do I delete "spaces.txt "?

May 20, 2008
9,912
71
Syracuse, NY, USA
The file name has three spaces at the end. Explorer, TCC, CMD show it. TCC and CMD complete its name. But none can find it when I try to delete it (or rename it).
Code:
v:\> dir /b sp*
spaces.txt
 
v:\> del "spaces.txt  "
TCC: (Sys) The system cannot find the file specified.
"V:\spaces.txt"
    0 files deleted
 
May 24, 2010
855
0
Northlake, Il
Vince,

I don't really believe it has spaces on the end. Do this:
Code:
[D:\]>"Spaces.txt  "
 
[D:\]*Dir spa* /K /M
6/04/2012  23:54              0  Spaces.txt
 
[D:\]PDir Spac* /(=fn=)
=Spaces.txt=
 
[D:\]Del Spaces.txt
Deleting D:\Spaces.txt
    1 file deleted
 
[D:\]
I was pretty sure that Windows doesn't allow spaces on the end of file names (although it does allow spaces at the beginning of a file name), and I was correct.
 
May 20, 2008
9,912
71
Syracuse, NY, USA
Vince,

I don't really believe it has spaces on the end. Do this:

I was pretty sure that Windows doesn't allow spaces on the end of file names (although it does allow spaces at the beginning of a file name), and I was correct.
Well, I managed to create the file!
Code:
h:\ugly> do f in * (echo "%f")
"spaces.txt  "
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,968
53
Albuquerque, NM
prospero.unm.edu
The file name has three spaces at the end. Explorer, TCC, CMD show it. TCC and CMD complete its name. But none can find it when I try to delete it (or rename it).
Code:
v:\> dir /b sp*
spaces.txt
 
v:\> del "spaces.txt  "
TCC: (Sys) The system cannot find the file specified.
"V:\spaces.txt"
    0 files deleted
FixNames is supposed to be able to fix that. (There are a lot of illegal names I can't fix, but that's one I can.)

Alternatively, you should be able to rename (delete, open, copy, etc.) the file using UNC notation:

Code:
ren "\\?\c:\bin\test\spaces.txt   " spaces.txt
 
Jan 19, 2011
588
12
Norman, OK
The file name has three spaces at the end. Explorer, TCC, CMD show it. TCC and CMD complete its name. But none can find it when I try to delete it (or rename it).
Code:
v:\> dir /b sp*
spaces.txt
 
v:\> del "spaces.txt  "
TCC: (Sys) The system cannot find the file specified.
"V:\spaces.txt"
    0 files deleted
I don't know if you noticed, but in your example you only show 2 spaces between the .txt and the ". That may just be a forum formatting thing though.
 
May 20, 2008
9,912
71
Syracuse, NY, USA
FixNames is supposed to be able to fix that. (There are a lot of illegal names I can't fix, but that's one I can.)

Alternatively, you should be able to rename (delete, open, copy, etc.) the file using UNC notation:
Code:
h:\ugly> del "\\?\h:\ugly\spaces.txt  "
TCC: (Sys) The system cannot find the file specified.
"H:\ugly\spaces.txt"
    0 files deleted
 
h:\ugly> ren "\\?\h:\ugly\spaces.txt  " spaces.txt
TCC: (Sys) The system cannot find the file specified.
"H:\ugly\spaces.txt"
    0 files renamed
Do you still think FixNames can do it? If so, how?
 
May 20, 2008
9,912
71
Syracuse, NY, USA
I don't know if you noticed, but in your example you only show 2 spaces between the .txt and the ". That may just be a forum formatting thing though.
There are definitely three spaces:
Code:
h:\ugly> do f in * (echo "%f" | g:\gnu\tr " " #)
"spaces.txt###"
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,968
53
Albuquerque, NM
prospero.unm.edu
Code:
h:\ugly> del "\\?\h:\ugly\spaces.txt  "
TCC: (Sys) The system cannot find the file specified.
"H:\ugly\spaces.txt"
    0 files deleted
 
h:\ugly> ren "\\?\h:\ugly\spaces.txt  " spaces.txt
TCC: (Sys) The system cannot find the file specified.
"H:\ugly\spaces.txt"
    0 files renamed
Hunh. Have you tried that command in CMD.EXE? TCC may be outsmarting you, correcting the illegal filename before passing it on to the API. (I can't check at the moment -- I have no easy method for creating such broken names!)

You might also try it with wildcards: REN \\?\H:\UGLY\SPACES.* *.TXT

Do you still think FixNames can do it? If so, how?
Try FIXNAMES H:\UGLY\SPACES*
 
May 20, 2008
9,912
71
Syracuse, NY, USA
Hunh. Have you tried that command in CMD.EXE? TCC may be outsmarting you, correcting the illegal filename before passing it on to the API. (I can't check at the moment -- I have no easy method for creating such broken names!)

You might also try it with wildcards: REN \\?\H:\UGLY\SPACES.* *.TXT

Try FIXNAMES H:\UGLY\SPACES*
Well, FIXNAMES renamed it (thanks!) so I can't try your other suggestions. Can you tell me how it did that?
I had tried it with CMD but IIRC only with the actual (bad) file name, not with wildcards.
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,968
53
Albuquerque, NM
prospero.unm.edu
Well, FIXNAMES renamed it (thanks!) so I can't try your other suggestions. Can you tell me how it did that?
First it tries a regular MoveFile(). If that fails, it tries again, but using the UNC syntax for the old name ("\\?\H:\UGLY\SPACES "); and if that fails, and the file has an alternate filename, it tries a third time using the alternate filename. In the case of filenames with trailing spaces or periods, the second (UNC) rename usually succeeds.

I spent several days once fighting with a fourth routine. I had a clever idea for renaming files with utterly illegal names by using the low-level native API, but couldn't make it work thanks to an undocumented limitation in ZwSetInformationFile(). Thanks a bunch, Microsoft.
 
May 20, 2008
9,912
71
Syracuse, NY, USA
First it tries a regular MoveFile(). If that fails, it tries again, but using the UNC syntax for the old name ("\\?\H:\UGLY\SPACES "); and if that fails, and the file has an alternate filename, it tries a third time using the alternate filename. In the case of filenames with trailing spaces or periods, the second (UNC) rename usually succeeds.

I spent several days once fighting with a fourth routine. I had a clever idea for renaming files with utterly illegal names by using the low-level native API, but couldn't make it work thanks to an undocumented limitation in ZwSetInformationFile(). Thanks a bunch, Microsoft.
Hmmm! I had tried DeleteFile() and SHFileOperation() with the exact UNC name and it failed.
 
May 24, 2010
855
0
Northlake, Il
Vince,

Just as a purely out of curiosity question, how did you manage to create a file name of that nature in the first place? Among other things, I'd kind of like to do it, too, to investigate options for trying to delete it (I know, probably a little weird, but that's nothing new! :))

- Dan
 
May 20, 2008
9,912
71
Syracuse, NY, USA
Vince,

Just as a purely out of curiosity question, how did you manage to create a file name of that nature in the first place? Among other things, I'd kind of like to do it, too, to investigate options for trying to delete it (I know, probably a little weird, but that's nothing new! :))

- Dan
Because of its name and its timestamp, I'm pretty sure I created that file myself as some sort of test. But I don't remember how. There are no remnants of a programming project to do it so I must have used some tool ... TCC? ... CMD? ... ?
 
May 20, 2008
9,912
71
Syracuse, NY, USA
Well, creating and deleting it was easy enough with a tiny program:
Code:
#include <windows.h>
 
INT wmain ( INT, WCHAR)
{
    WCHAR szFileName[MAX_PATH] = L"\\\\?\\h:\\ugly\\spaces.txt  ";
    HANDLE hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
    CloseHandle(hFile);
    Sleep(30000);
    DeleteFile(szFileName);
    return 0;
}
During the 30 second sleep I observed this ...
Code:
h:\ugly> do f in * (echo "%f")
"FixNames.dll"
"spaces.txt  "
 
h:\ugly> del "spaces.txt  "
TCC: (Sys) The system cannot find the file specified.
"H:\ugly\spaces.txt"
    0 files deleted
... and afterward, this
Code:
h:\ugly> do f in * (echo "%f")
"FixNames.dll"
 
h:\ugly>
 
Jan 19, 2011
588
12
Norman, OK
Because of its name and its timestamp, I'm pretty sure I created that file myself as some sort of test. But I don't remember how. There are no remnants of a programming project to do it so I must have used some tool ... TCC? ... CMD? ... ?
It's doable, and delete-able, using CMD. Next question... why can't TCC delete it the same way?
HTML:
Z:\>dir
Volume in drive Z has no label.
Volume Serial Number is 58B8-A853
 
Directory of Z:\
 
06/06/2012  10:03    <DIR>          .
06/06/2012  10:03    <DIR>          ..
              0 File(s)              0 bytes
              2 Dir(s)  26,503,299,072 bytes free
 
Z:\>echo > "\\?\z:\spaces.txt  "
 
Z:\>dir
Volume in drive Z has no label.
Volume Serial Number is 58B8-A853
 
Directory of Z:\
 
06/06/2012  10:05    <DIR>          .
06/06/2012  10:05    <DIR>          ..
06/06/2012  10:05                13 spaces.txt
              1 File(s)            13 bytes
              2 Dir(s)  26,503,299,072 bytes free
 
Z:\>del spaces.txt
Could Not Find Z:\spaces.txt
 
Z:\>for %i in (*.*) do (echo "%i"| tr " " "#")
 
Z:\>(echo "spaces.txt  "  | tr " " "#" )
"spaces.txt###"
 
Z:\>del "\\?\z:\spaces.txt  "
 
Z:\>dir
Volume in drive Z has no label.
Volume Serial Number is 58B8-A853
 
Directory of Z:\
 
06/06/2012  10:06    <DIR>          .
06/06/2012  10:06    <DIR>          ..
              0 File(s)              0 bytes
              2 Dir(s)  26,503,307,264 bytes free
 

samintz

Scott Mintz
May 20, 2008
1,370
12
Solon, OH, USA
It seems that TCC strips the spaces, whereas CMD does not. So it's straight forward to create and delete the file using CMD and JohnQSmith's instructions.

I was able to delete it in TCC using the @WinApi function and calling DeleteFileW directly.

Code:
echo %@winapi[kernel32.dll,DeleteFileW,"\\?\R:\LNX\Release\spaces.txt   "]
-Scott
 
Jan 19, 2011
588
12
Norman, OK
Any word on this? Other than using a winapi call to kernel32.dll, why is TCC unable to delete files ending in spaces when using UNC notation?
 
Jan 19, 2011
588
12
Norman, OK
Almost certainly because TCC recognizes trailing spaces as being illegal in a filename, and corrects them for you.
OK... I needed further clarification on that and decided to find out what the exact rules are. Here's the best of what I found.
I quote the part that is of particular interest to this thread.
Do not end a file or directory name with a space or a period. Although the underlying file system may support such names, the Windows shell and user interface does not. However, it is acceptable to specify a period as the first character of a name. For example, ".temp".
 
May 20, 2008
9,912
71
Syracuse, NY, USA
OK... I needed further clarification on that and decided to find out what the exact rules are. Here's the best of what I found.
I quote the part that is of particular interest to this thread.
Although it is said, in places, that they are "illegal", they are obviously not so. But since the shell (in particular) and, no doubt, other "well-behaved" apps (i.e., follows the recommendations) can't deal with them, I suppose TCC has chosen to follow the recommendations. I don't know whether TCC could disallow creating them while still allowing deleting them with the UNC notation. But we have discovered ways of dealing with them.