2

In some common operating systems, including Windows, both the forward slash and backslash are reserved characters that cannot be used in filenames.

Yet sometimes one has a good reason to want to include such characters in a filename. What I currently do is replace all forward slashes with the string !fslash! and all backslashes with the string !bslash! when I want to indicate one of those characters in a filename.

Is there a better hack than what I am already doing?

4

If you do not need it to look like a slash, you can use a different substitution character.

If you want the character to actually look like a slash, you can use the division slash, unicode U+2215.

The best option for the backslash alternative is probably the double backslash character, unicode U+244A.

1
  • 2
    Just keep that this approach will likely confuse any other use trying to find a folder called like the first part of the file. Apr 23 '21 at 19:14
1

I'm thinking that making your hack vaguely compatible with existing solutions is more important than human convention, which you are obviously breaking anyway.

There are some common encodings which you might find easier to use because there are existing tools etc which can help you manage the conventions you choose, though of course there is no OS support for making it work reliably and transparently.

  • URLs have percent encoding; a literal per cent sign followed by two hex digits; so to represent "5/5%=0" you would end up with 5%2F5%25=0 (the literal per cent sign obviously also needs to be encoded)
  • Email (or more generally MIME) has quoted-printable encoding, which is quite similar, except the "magic" character is an equals sign. So to represent "5/5%=0" you would end up with 5=2F5%=3D0 (where obviously the literal equals sign needs to be encoded).
    • If you need to make it reasonably obvious what you are doing, at the expense of legibility and convenience, the email header RFC2047 encoding should look vaguely familiar to many computer-literate users; =?us-ascii?Q?5=2F5%=3D0?=
    • There is also a separate encoding for file names etc in RFC2231 but it's probably more complex than you like.
  • Many programming languages allow a notation like \uFEDC but the backslash is clearly cumbersome for other reasons. This is obviously restricted to Unicode strings (so you can't encode arbitrary sequences of bytes like you could with e.g. URL encoding like %88%88%88)

For your own ad-hoc convention, perhaps at least choose symbolic names which are used by other encodings, too; !solidus! for forward slash is obscure, but gets some support from various Unicode libraries etc (though !slash! would probably also work, as that is a synonym in many contexts); Unicode calls backslash REVERSE SOLIDUS but !backslash! could also work, and is a known synonym (see e.g. https://www.fileformat.info/info/unicode/char/5c/index.htm)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.