Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colon (:) appears as forward slash (/) when creating file name

Tags:

macos

ruby

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 
like image 790
BlackHatSamurai Avatar asked Nov 08 '12 21:11

BlackHatSamurai


People also ask

How do you escape the forward slash in a filename?

There is no way to escape the character. If a filesystem "supports" this, it's because they either: Use a unicode character or something that looks like a slash but isn't. They have a bug.

Can a colon be used in a filename?

On Windows systems, files and directory names cannot be created with a colon (:). But if a file or directory name is created with a colon on a Linux or Mac operating system, then moved to a Windows system, percent encoding is used to include the colon in the name in the index.

Can filenames have forward slashes?

This may cause some confusion, since in the Finder you can rename files to include a forward slash; however, this is because the Finder converts forward slashes into colon characters when writing the names to disk. The colon character is fine for use in Unix file names, but the forward slash is not.

What is forward slash in file path?

A good way to remember the difference between a backslash and a forward slash is that a backslash leans backwards ( \ ), while a forward slash leans forward ( / ). In Windows, backslashes are used to separate directories in file paths (ex: C:\Program Files\Common Files\microsoft shared\).


1 Answers

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.

like image 69
ldav1s Avatar answered Sep 22 '22 07:09

ldav1s