Colon (:) Appears as Forward Slash (/) When Creating File Name

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

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.

How to get a file in Windows with a colon in the filename?

I found a very similar character to a colon, "꞉" it is a unicode character called a Modifier Letter Colon. This has no space like the fullwidth colon and is pretty much exactly the same as a regular colon but the symbol works. You can either copy and paste it from above or you can use the code point, U+A789

How can I make this OneDrive sanitiser work?

The line of code in question is:

if (-f and not 's/^\./' )
  • This succeeds when -f is true and 's/^\./' is false

    • -f tests if $_ is a file
    • In a boolean context, 's/^\./' tests if the given string is not null

Clearly any string that has some characters in it is not null, and so the if test always fails.

Looking at the contents of the string, it is clear that the intention was to insert a regular expression test. The one that is required is the m// form ("match"). The one that has been (almost) provided, is a malformed version of s/// (search and replace).

In Perl, regular expressions generally do not appear as strings. The appropriate form is (m is optional):

if (-f and not m/^\./)

As couple of asides:

  • @argv is not used in your code (and is not the same as @ARGV)
  • use find(\&dirRecurs, @ARGV); to search more than one path (provided as arguments to the perl script)
  • You perform rename even if there has been no change. This can be prevented by wrapping in and if:
if ((my $txt = $_) =~ s/^\ | (?=\.)|[\/!#%^?*&()\\]| $//g) {
rename($_, $txt);
}

# or:

rename($_, $txt)
if (my $txt = $_) =~ s/^\ | (?=\.)|[\/!#%^?*&()\\]| $//g;

How to preserve colon character in filename when using NSData method writeToFile:atomically: ?

The filename does contain the colon; the Finder replaces it with slashes.

This is a holdover from when you couldn't use a colon because it was the path separator on Mac OS. Now, the path separator is the slash, hence the switch.

The Finder still won't let you enter a colon; if you try to enter a slash, it'll succeed, but save the name with a colon in its place.

Pretty much everywhere else, including in Cocoa, a colon is valid (not a path separator) but a slash isn't.

Save email to Windows folder with subject as file name

Not all characters can be used in a Filename. Namely, these.

Asterisk (*)
Backslash (\)
Colon (:)
Angle brackets (< >)
Question mark (?)
Slash (/)
Plus sign (+)
Pipe (|)
Quotation mark (")

There are lots of places online to find a pre-written function which will remove or replace them. Here is one: Remove Illegal Characters from Filename



Related Topics



Leave a reply



Submit