10

After a half hour searching Google, I am surprised I cannot find any way to create a file on Windows with slashes in the name. The customer demands that file names have the following structure:

04/28/2012 04:07 PM 6,781 12Q1_C125_G_04-17.pdf

So far I haven't found any way to encode the slashes so they become part of the file name instead of the path.

Any Suggestions?

user1410995
  • 109
  • 1
  • 1
  • 3

3 Answers3

19

You can't.

The forward slash is one of the characters that are not allowed to be used in Windows file names, see http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

The following fundamental rules enable applications to create and process valid names for files and directories, regardless of the file system:

Use a period to separate the base file name from the extension in the name of a directory or file.

Use a backslash (\) to separate the components of a path. The backslash divides the file name from the path to it, and one directory name from another directory name in a path. You cannot use a backslash in the name for the actual file or directory because it is a reserved character that separates the names into components.

Use a backslash as required as part of volume names, for example, the "C:\" in "C:\path\file" or the "\server\share" in "\server\share\path\file" for Universal Naming Convention (UNC) names. For more information about UNC names, see the Maximum Path Length Limitation section.

Do not assume case sensitivity. For example, consider the names OSCAR, Oscar, and oscar to be the same, even though some file systems (such as a POSIX-compliant file system) may consider them as different. Note that NTFS supports POSIX semantics for case sensitivity but this is not the default behavior. For more information, see CreateFile.

Volume designators (drive letters) are similarly case-insensitive. For example, "D:\" and "d:\" refer to the same volume.

Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

    The following reserved characters:
        < (less than)
        > (greater than)
        : (colon)
        " (double quote)
        / (forward slash)
        \ (backslash)
        | (vertical bar or pipe)
        ? (question mark)
        * (asterisk)

Integer value zero, sometimes referred to as the ASCII NUL character.

Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.

Any other character that the target file system does not allow.

bgporter
  • 35,114
  • 8
  • 59
  • 65
  • 1
    I'd suggest you suggest to your client you use dashes as the date delimiter. – Silas Ray May 22 '12 at 19:12
  • 10
    @ch3ka Using a symbol that is visually indistinguishable from an invalid character that will cause all kinds of problems all over the place is an invitation to headaches and disaster. – Silas Ray May 22 '12 at 21:48
  • @sr2222 sure. but clients get what clients want. 1st rule of freelancing. and hey, it *works*. – ch3ka May 22 '12 at 22:53
  • ...works until someone moves that file onto a FAT32 machine that uses the OEM code page for filenames and things blow up... – bgporter May 22 '12 at 23:05
  • 3
    Or someone tries to write another piece of code that tries to manipulate your files and bangs their head against a wall for 4 hours because you are using a entirely wonky non-standard mechanism. Client gets what client wants, unless what client wants is a patently bad idea. If you can't convince your client that a bad idea is a bad idea, then you aren't really doing your job. – Silas Ray May 23 '12 at 13:18
  • 1
    unicode in filenames is not "a entirely wonky non-standard mechanism". It is there, it works, it is documented. I agree that unicode look-alikes can cause confusion, though. But this is true wherever and whenerver unicode is allowed. – ch3ka May 23 '12 at 13:47
16

At least all windows installation i've seen won't let you create files with slashes in them. Even if it were possible somehow, by doing deepshit magic, it will probably screw up almost all applications, including windows explorer.

you could abuse windows' unicode capabilities, though.

Creating a file with (this is not a forward slash, it is "division slash", see http://www.fileformat.info/info/unicode/char/2215/index.htm ) in it's name works just fine, for example.

ch3ka
  • 11,792
  • 4
  • 31
  • 28
0

Um... forward slash is not a legal character in a Windows file name?

http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

MK.
  • 33,605
  • 18
  • 74
  • 111