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
$ 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!


  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.

  2. Hello again,

    I found the solution thanks to another of your posts :

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

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

  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.

  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.

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

    1. The Windows explorer does not allow leading/trailing spaces, but the Windows shell does seem to allow leading whitespaces.

  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.

  8. Interesting.

    I have a client that I believe is emailing ".rar " files as attachments instead of ".rar" files. But outlook 2013/windows 10 seems to just see them as rar files, with the little yellow "I'm something that needs to be unzipped" icon.

    The reason I think there is a space at the end is that I have ftp software that automatically checks this mailbox via pop3 for files matching *.rar, and it doesn't find files. But if I change that file mask to *.rar*, the automated ftp task finds the client's files. In the task's destination folder, the file names do not have the trailing space.

    if I send my own test file, which I know is definitely named test.rar, the ftp software works using the *.rar file mask.

    So my ftp software is seeing the trailing space at the end of the client filename, but windows is either not seeing it or choosing to ignore it. My guess was that they are creating the file in a non-windows environment (I'm not actually allowed to speak to the client to ask them)

  9. @Curtiss, glad you got the issue tracked down! That must have been a bizarre one to troubleshoot.

  10. I just discovered that when file explorer ( windows 10 ) copies files, it strips the leading spaces, but the old dos ( cmd ) copy command preserves the leading spaces


Non-spammers: Thanks for visiting! Please go ahead and leave a comment; I read them all!

Attention SPAMMERS: I review all comments before they get posted, and I REPORT 100% of spam comments to Google as spam! Why not avoid getting your account banned as quickly -- and save us both a little time -- by skipping this comment form and moving on to the next one on your list? Thanks, and I hope you have a great day!