23

Is there any special character that cannot be a part of the path in Windows or Unix that I can use it as a separator?

user269354
  • 233
  • 1
  • 2
  • 4

6 Answers6

71

what about the delimiter for PATH environment variable? ; for windows, and : for Linux.

Dyno Fu
  • 8,753
  • 4
  • 39
  • 64
8

Wikipedia helpfully lists the reserved characters for different filesystems. Neither NTFS nor POSIX will accept the null or slash (/) characters in filenames. The slash character is obviously not a good separator, since it's common in POSIX paths, so maybe you could use null.

Of course null isn't suited to all situations (e.g. it isn't usually visible when printed), in which case you might have to use some sort of escaping scheme.

Java, which aims to work across different platforms, doesn't even try to find a common path separator. Instead each platform has its own character, accessible through an API.

Rich Dougherty
  • 3,231
  • 21
  • 24
8

Path separator are platform dependent :

For windows, it’s \ and for unix it’s /.

António Almeida
  • 9,620
  • 8
  • 59
  • 66
Nilesh Thakkar
  • 133
  • 1
  • 2
  • 11
    You're talking about directory separators, not path separators. Path separators are the characters (semicolons on Windows, colons on Unix) that separate individual elements of a value that represents multiple paths. – Alan Apr 29 '15 at 14:53
  • 1
    "directory separators" are also widely known as path separators, somewhat confusingly. In this case I'm pretty sure OP was talking about file paths, rather than PATH. – Timmmm Jun 30 '20 at 09:27
  • Also worth pointing out that `/` works perfectly well on Windows unless an app goes out of its way to stop it working. – Timmmm Jun 30 '20 at 09:28
3

Technically, Unix does allow any character in a folder/filename, except / of course, which would be interpreted as as part of the path. Windows does only support printable characters and some special characters excluding \ / : * ? " < > |.

Bobby
  • 11,419
  • 5
  • 44
  • 69
  • So, for Unix, there is no any way to join several paths in one string? – user269354 Feb 09 '10 at 09:42
  • 2
    @user269354 - Maybe you can use a home-made-separator like: /home/user/tiutalk/_____SEPARATOOOOORRR_____/var/www/_____SEPARATOOOOORRR_____/bin/dump/ – Thiago Belem Feb 09 '10 at 09:45
  • or string like "#p#p...#p#p", where # is number of characters in the path and p is the path – user269354 Feb 09 '10 at 09:48
  • @user269354: Would be a possibility, as TiuTalk suggested, just use a pattern which will not be part of the paths and which you can easily recognize and extract. If you can be sure that there'll never be numbers in those paths, then yes, use that one. – Bobby Feb 09 '10 at 10:12
  • 1
    @user269354, Unix uses `:` (colon) to seperate paths, but since Windows has `:` in root directory names like `C://`, it uses `;` (semicolon) to seperate paths – smac89 Mar 30 '17 at 16:53
1

In java you can use:

WindowsNTFileSystem
s.split(File.pathSeparator) for windows it will give ; (semicolon)
s.split(File.separator) for windows it will give \ (backward)

Linux
s.split(File.pathSeparator) for windows it will give : (colon)
s.split(File.separator) for windows it will give / (forward)
rbyndoor
  • 699
  • 4
  • 14
-1

I would be careful with custom separators because they might break in the future, e.g. if someone uses unicode and your custom separator is part of another character.

Thorsten79
  • 10,038
  • 6
  • 38
  • 54