40

I am using date and time to label a new file that I'm creating, but when I view the file, the colon is a forward slash. I am developing on a Mac using 10.7+

Here is the code I'm using:

 File.open("#{time.hour} : 00, #{time.month}-#{time.day}-#{time.year}", "a") do |mFile|
        mFile.syswrite("#{pKey} - #{tKey}: \n") 
        mFile.syswrite("Items closed: #{itemsClosed} | Total items: #{totalItems} | Percent closed: % #{pClosed} \n") 
        mFile.syswrite("\n")
        mFile.close
     end

Here is the output (assuming the time is 1pm):

13 / 00, 11-8-2012

Why is this happening and how can I fix it? I want the output to be:

13:00, 11-8-2012
2
  • related: stackoverflow.com/questions/9559701/… (no solution)
    – user166390
    Commented Nov 8, 2012 at 22:07
  • 6
    The filename actually does have the colon character, it’s Finder that displays it as a slash. Try looking at the files in the terminal to see the difference.
    – matt
    Commented Nov 8, 2012 at 22:31

3 Answers 3

53

Once upon a time, before Mac OS X, : was the directory separator instead of /. Apparently OS X 10.7 is still trying to fix up programs like that. I don't know how you can fix this, if you really need the : to be there. I'd omit it :-).

EDIT: After a bit more searching this USENIX paper describes what is going on. The rule they use apparently is this:

Another obvious problem is the different path separators between HFS+ (colon, ':') and UFS (slash, '/'). This also means that HFS+ file names may contain the slash character and not colons, while the opposite is true for UFS file names. This was easy to address, though it involves transforming strings back and forth. The HFS+ implementation in the kernel's VFS layer converts colon to slash and vice versa when reading from and writing to the on-disk format. So on disk the separator is a colon, but at the VFS layer (and therefore anything above it and the kernel, such as libc) it's a slash. However, the traditional Mac OS toolkits expect colons, so above the BSD layer, the core Carbon toolkit does yet another translation. The result is that Carbon applications see colons, and everyone else sees slashes. This can create a user-visible schizophrenia in the rare cases of file names containing colon characters, which appear to Carbon applications as slash characters, but to BSD programs and Cocoa applications as colons.

8
  • Interesting. Do you know how can I fix it so I get the output I want? Commented Nov 8, 2012 at 21:55
  • 1
    The colon remains an illegal filename character under both OS X (/HFS+) and Windows (/NTFS); I'm not sure your implicit belief that this is a lingering attempt by OS X to retain backwards compatibility is accurate.
    – Tommy
    Commented Nov 8, 2012 at 22:02
  • @Tommy On windows it would simply result in an invalid operation (if not part of the drive qualifier or an ADS in NTFS); not a silent conversion ..
    – user166390
    Commented Nov 8, 2012 at 22:04
  • @Tommy I never said : was a filename character. I'm not entirely certain that it is OS X converting it to /, either. But something remembers the past and is trying to fix it, and I think it is OS X more strongly than it is Ruby.
    – ldav1s
    Commented Nov 8, 2012 at 22:09
  • 7
    From ~ on Mac OS, if I touch Desktop:foo I get a file called Desktop/foo in the Finder, and called Desktop:foo at the command-line. It's Apple's slight-of-hand in the Finder, not anything in the OS underneath it. Remember, Finder is responsible for the visualization of the file system. The OS is responsible for managing it. Commented Nov 8, 2012 at 23:26
20

While OS X "is" a unix operating system, it also derives quite a bit its code, APIs, standards, etc from Mac OS 9. In unix, file paths have "/" separating the elements and ":" is allowed in the names of individual files and directories. In Mac OS 9, it was the other way around: file paths had ":" between elements and "/" was allowed in individual filenames. When Apple developed OS X, they wound up having to support some APIs that used unix-style file paths, and some APIs that used OS 9-style paths, and they had to both be able to work on the same filesystem.

What they did is to swap delimiters and allowed characters depending on context. If you write (/run) a program that uses unix APIs to access the filesystem, you'll see files with colons in their names and slashes separating path elements. If you write (/run) a program that uses the old OS 9 APIs (or their derivatives), you'll see files with slashes in their names and colons separating path elements. See Apple's developer Q&A #1392 and notes on specifying paths in AppleScript for a bit more discussion.

(There are some other differences as well. A unix path is absolute if it starts with the delimiter ("/"), and absolute paths start at the top of the root volume. An OS 9 path is absolute if it doesn't start with a delimiter, and absolute OS 9 paths start with a volume name. Thus, the unix path "/tmp/foo:bar" is equivalent to the OS 9 path "Macintosh HD:tmp:foo/bar".)

So, which character is really in the filename, a slash or a colon? Well, a filename is a rather abstract thing, but if you're asking about the bytes that're actually stored on the disk... if it's on an HFS+ (aka Mac OS Extended) volume, it's being stored in a filesystem that was designed to work with the OS 9 (well, technically Mac OS 8.1) APIs, so it allows slashes but forbids colons, so on an HFS+ volume the file will "really" have a slash in the name. OTOH if you store the file on a unixish volume, it'll be stored using the unix convention, and "really" have a colon in the name. But the difference doesn't really matter unless you're reading raw bytes off the disk or writing a filesystem driver...

Finally, why does the Finder display the controversial filename character as slash rather than colon? I'm pretty sure it's mostly inertia. The Finder isn't even entirely consistent about this, since if you use its Go To Folder option (Command-Shift-G) and type in "/Users/Shared", it treats that as a unix path. If you type in "Macintosh HD:Users:Shared", it has no idea what you're talking about. Furthermore, if you run touch /tmp/foo:bar, then try to get to it with Go To Folder:

  • Entering "/tmp/foo:bar" works.
  • Entering "/tmp/fo" then pressing tab autocompletes it to "/tmp/foo/bar/", which works.
  • Entering "/tmp/foo/bar/" fails, even though it's exactly the same as the autocomplete.
  • Entering "/tmp/foo" then pressing tab autocompletes to "/tmp/foo/", which cannot be autocompleted any further and doesn't work at all.

Update: as Konrad Rudolph pointed out, the Go To Folder behavior has changed as of El Capitan, and I there's no longer any way to use it to get to folders containing the controversial character.

3
  • 1
    So doestouch ":..:bar"would create a file in the parent directory ? Commented Nov 16, 2015 at 20:47
  • 1
    @user2284570: No, touch uses the unix APIs, so it treats ":" as a part of a filename, not as a path delimiter. Suppose your current directory was /Users/fred/Documents. It would create a file that the unix APIs would see as "/Users/fred/Documents/:..:bar". Mac OS 9-derived APIs, on the other hand, would see it as "Macintosh HD:Users:fred:Documents:/../bar". Commented Nov 16, 2015 at 22:03
  • 2
    Interestingly, El Capitan apparently changed the Finder semantics yet again …. Now “Go To Folder” no longer supports navigating to a folder containing slash/colon in its name at all. None of the ways enumerated at the end of your post works. Commented May 20, 2016 at 14:00
4

To avoid as many problems as possible when dealing with File names, paths, and various OSes, you really should take advantage of the built-in File methods, like join, dirname, basename, extname, and split. They try to avoid system dependencies and try to give you a programmatic way to generate valid filenames cross-platform.

This problem was a lot worse back when Apple used the old Macintosh operating system. The move to Mac OS helped, because they dropped using : as a separator, however those people who were manually building filenames found code breaking because it generated the wrong delimiters, whereas taking advantage of the libraries handled the problem.

Because this particular problem isn't a bug, nor is it in Ruby's control but Apple's, I'd say it's not a Ruby problem at all, it's a visualization issue, and if you want the filename to resemble what the Finder displays code accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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