Thursday, March 06, 2008

Leading/Trailing space characters in filenames

Did you know? On Windows, it's legal for filenames to have leading space characters! I've developed on Windows for years, but didn't realize this until just recently. For example, at a command prompt, in a directory that contains a file named t.txt, the following works:

C:\temp> copy t.txt " t.txt"
        1 file(s) copied.
This results in a directory listing like:
 Directory of C:\TEMP\spaceTest

03/06/2008  05:29 PM    <DIR>          .
03/06/2008  05:29 PM    <DIR>          ..
03/06/2008  05:29 PM                 4  t.txt
03/06/2008  05:29 PM                 4 t.txt
               2 File(s)              8 bytes
What about Linux/Unix? I tested briefly using a couple of servers running HP-UX and RedHat Linux at the office, and found that both Linux and Unix (at least the flavors that I tested) support both leading and trailing spaces in filenames! For example, at a shell prompt, in a folder that initially contains just the file t.txt:
$ ls -Q
"t.txt"
$ cp t.txt " t.txt"
$ cp t.txt "t.txt "
$ cp t.txt " t.txt "
$ ls -Q
" t.txt"  " t.txt "  "t.txt"  "t.txt "
$

(The -Q flag on ls as used in the example above causes filenames in the listing to be displayed enclosed in doublequotes, which was useful for showing that the filenames do indeed include space characters. The -Q flag is supported on the copy of ls on the RedHat Linux server I'm testing on here, but not on the HP-UX server.)

The fact that filenames can contain leading (and, on Linux/Unix, trailing) space characters seems like a good thing for developers to be aware of. Use caution when writing code that trims leading and trailing whitespace from a string that represents a filename!

7 comments:

  1. Hello,

    Nice article (thanks for showing us the "-Q" ls flag).

    I'm currently having troubles with these file/folder names and c# code that lists directory content on a BSD/Linux share. Directory.GetDirectories/GetFiles doesn't deal well with these names. I posted on MSDN forum.

    ReplyDelete
  2. Hello again,

    I found the solution thanks to another of your posts :
    http://blog.jonschneider.com/2006/06/c-string-literals-and-character.html

    Using the @"some path" pattern solves the directory listing !

    ReplyDelete
  3. @Barbara, great, I'm glad this was helpful!

    ReplyDelete
  4. Windows supports trailing spaces in names too. I've been using File Renamer to clean up several hundred file's names. Now I have a bunch with a trailing space. I can just trim 1 character at 0 from the end with File Renamer, but first I need a way to search for only files with a trailing space in the name so I can select them all to drag to Renamer. Searching for * .pdb Doesn't work. With the space it just lists ALL the folders and files under the folder I'm searching within.

    ReplyDelete
  5. @bizzybody:

    Hmm. On WinXP, from a cmd.exe command prompt, I was able to do a:

    > dir "* .txt"

    and it *did* return just the files with a trailing space and a .txt extension. This same search string didn't work for me in Windows Search, though.

    ReplyDelete
  6. I can't get leading spaces to stick in Windows 7 (I use them in XP for an application) Any ideas?

    ReplyDelete
  7. @Anonymous, it still seems to work for me. On my Windows 7 PC, I can use Notepad to create a new file and save it as "c:\temp\ t.txt" (with the leading spaces before the filename); when I browse the c:\temp directory in the command prompt or in Windows Explorer, I can see that the file is present with the leading spaces in the filename.

    ReplyDelete