1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do I delete "spaces.txt "?

Discussion in 'Support' started by vefatica, Jun 5, 2012.

  1. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    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
     
  2. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    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.
     
  3. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    Well, I managed to create the file!
    Code:
    h:\ugly> do f in * (echo "%f")
    "spaces.txt  "
     
  4. thedave

    Joined:
    Nov 13, 2008
    Messages:
    254
    Likes Received:
    2
    Maybe try
    Code:
    del "spaces.txt *"
     
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    Doesn't work.
     
  6. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,277
    Likes Received:
    38
    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
    
     
  7. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    560
    Likes Received:
    8
    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.
     
  8. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    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?
     
  9. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    There are definitely three spaces:
    Code:
    h:\ugly> do f in * (echo "%f" | g:\gnu\tr " " #)
    "spaces.txt###"
     
  10. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,277
    Likes Received:
    38
    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*
     
  11. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    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.
     
  12. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,277
    Likes Received:
    38
    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.
     
  13. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    Hmmm! I had tried DeleteFile() and SHFileOperation() with the exact UNC name and it failed.
     
  14. mathewsdw

    Joined:
    May 24, 2010
    Messages:
    855
    Likes Received:
    0
    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
     
  15. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    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? ... ?
     
  16. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    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>
     
  17. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    560
    Likes Received:
    8
    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
     
  18. samintz

    samintz Scott Mintz

    Joined:
    May 20, 2008
    Messages:
    1,177
    Likes Received:
    11
    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
     
  19. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    560
    Likes Received:
    8
    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?
     
  20. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,277
    Likes Received:
    38
    Almost certainly because TCC recognizes trailing spaces as being illegal in a filename, and corrects them for you.
     
  21. JohnQSmith

    Joined:
    Jan 19, 2011
    Messages:
    560
    Likes Received:
    8
    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.
     
  22. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,785
    Likes Received:
    29
    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.
     

Share This Page