How do I delete "spaces.txt "?

May 20, 2008
11,401
99
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
11,401
99
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
4,461
88
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
604
14
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
11,401
99
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
11,401
99
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
4,461
88
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
11,401
99
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
4,461
88
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
11,401
99
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
11,401
99
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
11,401
99
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
604
14
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,503
16
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
604
14
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
604
14
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
11,401
99
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.
 
Similar threads
Thread starter Title Forum Replies Date
D Bug Can't delete empty temporary files. Support 2
A Installers delete EVerything service if already installed Support 0
Chen Touboul When i try to delete an empty folder i got "the dirctoy name is invalid" Support 3
I zip /M fails to delete file after adding to zip file Support 2
D SYNC /W copies back instead of propagating a delete Support 2
M Delete multiple Alternate Datastreams Support 5
D safe delete files command Support 10
P How to? Delete subfolders but not the folder itself Support 11
P Can't do something to a file and then delete it? Support 6
nickles WAD del unable to delete files ending in "." Support 18
gschizas Fixed Cannot use extended path to delete invalid file Support 3
M Can't delete files... Support 8
aedthuio REG ADD/DELETE Support 1
vefatica Cut/delete Support 3
D TCMD16 Delete Toolbar Button bug Support 7
G FOLDERMONITOR created also firing for delete (and maybe other events) Support 7
T How to? Select and Delete text Support 6
Joe Caverly How to? Delete a line from a file Support 4
CWBillow Delete based on dates Support 5
JohnQSmith How to? Delete descriptions Support 2
A How to? Instruct COPY to delete file if only partially copied (via FTP) Support 8
M Why am I unable to delete files? Support 8
R Delete command sets directory attributes to hidden & system Support 46
S 4NT - delete from command prompt and recycle bin Support 12
Phileosophos COPY /W does not delete files in the target Support 4
T Possible to delete the new standard tab? Support 1
P Sync /w won't delete in ftp folder Support 1
vefatica Setting array variable ignores leading spaces (and @execarray doesn't) Support 4
R How to? Dir specific file search patterns with spaces in the pathnames? Support 6
vefatica START /PGM "name with spaces.URL" fails Support 2
C How to? rename files with spaces Support 8
vefatica IDE stuck on "Insert tabs as spaces" Support 0
redwdc Copying files with spaces, commas, etc. Support 5
thorntonpg Handling files with embedded spaces Support 1
WadeHatler pdir broken in 19.1 for directories with spaces Support 1
fpefpe How to? Passing arg to gosub with white spaces Support 2
N Problem with 'do <var> /L ....' and spaces Support 6
T KEYSTACK and double-double quotes dropping spaces Support 2
JohnQSmith ECHOS and trailing spaces Support 4
D %+ not surrounded by spaces in examples.btm Support 1
J Extended directory search - spaces in names Support 5
vefatica Leading spaces in array variables Support 13
R How to? copy /q ABC.txt + DEF.txt is appending hex 1A Support 8
noahcoad c:/hello.txt works in cmd.exe but not tcc.exe Support 1
Roedy reame.txt failure in installer Support 1
Gorlash tcc V11.00.37 - where is history.txt ?? Support 1

Similar threads