Re: Renaming files?
Re: Renaming files?
- Subject: Re: Renaming files?
- From: Clark Cox <email@hidden>
- Date: Wed, 22 Dec 2004 11:30:43 -0500
On Wed, 22 Dec 2004 10:56:08 +0100, Christoffer Lerno <email@hidden> wrote:
> On Dec 20, 2004, at 23:11, Jeremy Dronfield wrote:
>
> > If you're having trouble with spaces, I think there's something going
> > wrong with the way you're feeding pathnames to NSFileManager. I just
> > tried it with files called:
> >
> > Ò!Ó¥¦ ^^ Ž @£## {Û} %\<> .gif
> > ©Æ ¯¸ à åäŸÏò  ĩ » .tiff
> >
> > which I renamed as
> >
> > new0001.gif
> > new0002.tiff
> >
> > using -[NSFileManager movePath:toPath:handler:] No problem. To test it
> > back the other way, I renamed "new0002.tiff" as "U " † © #Û
> > {û}0002.tiff". Again no problem.
>
> You're right (as I said to Clark Cox) it turns out that the encoding is
> different in the filenames when I get them back. Try taking
> new00002.tiff, name it
> Ò!Ó¥¦ ^^ Ž @£## {Û} %\<> .gif by using movePath:toPath:handler: and
> then compare the NSString you kept the new filename in with the
> filenames gotten from - (NSArray *)directoryContentsAtPath:(NSString
> *)path
>
> It would seem that they have different encodings the filenames won't
> match unless I use compare (isEqualToString doesn't work)
>
> I wonder if there is some way to take my NSString from the NSTextField
> and convert that to the filesystem-encoding so they match up with
> isEqualToString?
>
> Basically the problem is that in one place I keep track of all the
> files in a folder, and using kqueue I monitor these changes. When a
> change occurs I check the folder for updates by comparing my register
> with that of the directory. However, if I want to rename something, I
> first update the filename in the register, then run the rename. When
> kqueue signals a change, the register will think no changes have been
> made (ideally). However, since the filename I update with doesn't match
> the filename I get with directoryContentsAtPath, the register will
> think I added one file and removed another, which turns out to be a
> problem in the context of my app.
>
> Basically I want to know what the heck my file is called after it's
> renamed... :) It isn't exactly what I renamed it to, even though it
> prints and looks the same.
You're forgetting canonical equivalence. Many Unicode characters can
be represented multiple ways, but are meant to be considered
canonically equivalent. For example, the character "ü" (u with an
umlaut, if it gets mangled in transmission) can be represented by
either:
A single Unicode codepoint: U+00FC
Or two codepoints: U+0075 U+0308
Additionally, I just noticed that your sample code does not illustrate
the problem. The last if statement needs a '!' to negate it. That is,
NSFileManager is telling you correctly that the file does indeed
exist, but without the 'not', you're interpreting that as not
existing. Here is your example, updated:
----------------------------
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *originalPath = [@"~/Desktop/renametest.txt"
stringByExpandingTildeInPath];
NSString *destinationPath = [[NSString
stringWithUTF8String: "~/Desktop/\xC3\xA5 \xC3\xA4 \xC3\xB6.txt"]
stringByExpandingTildeInPath];
[[NSFileManager defaultManager] removeFileAtPath:originalPath handler:nil];
[[NSFileManager defaultManager] removeFileAtPath:destinationPath handler:nil];
[[NSFileManager defaultManager] createFileAtPath:originalPath
contents:[NSData data] attributes:nil];
if (![[NSFileManager defaultManager] fileExistsAtPath:originalPath])
{
NSLog(@"Incorrect, the file %@ should exist before rename.",originalPath);
}
if ([[NSFileManager defaultManager] fileExistsAtPath:destinationPath])
{
NSLog(@"Incorrect, the file %@ should not exist before
rename.",destinationPath);
}
[[NSFileManager defaultManager] movePath:originalPath
toPath:destinationPath handler:nil];
if ([[NSFileManager defaultManager] fileExistsAtPath:originalPath])
{
NSLog(@"Incorrect, the file %@ should not exist after
rename.",originalPath);
}
if (![[NSFileManager defaultManager] fileExistsAtPath:destinationPath])
{
NSLog(@"Incorrect, the file %@ should exist after rename.",destinationPath);
}
[pool release];
return 0;
}
----------------------------
--
Clark S. Cox III
email@hidden
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden