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!