Re: Automatic file numbering ideas...
Re: Automatic file numbering ideas...
- Subject: Re: Automatic file numbering ideas...
- From: Andrew Merenbach <email@hidden>
- Date: Sun, 23 Nov 2008 12:18:43 -0800
Hi!
Thank you for the code, Chaitanya. Unfortunately, unless I'm missing
something, I'm not sure that it'll work in my particular case, since I
want sequential numbering, rather than random numbering; in other
words, this isn't for a unique identifier of sorts, but rather to
indicate how many existing files there are with the same prefix
("Picture", etc.). I do appreciate, however, that you took the time
to write that!
Cheers,
Andrew
On Nov 23, 2008, at 11:47 AM, chaitanya pandit wrote:
I'm not sure if you guys want the prefix to be "Picture" etc. but i
simply get a random number using rand(); and check if a file exists
with that name.
Heres a code snippet,
+ (NSString *)getUniqueNameForFile: (NSString *)aFilePath
{
// Initialise the return path
NSString *pathToReturn = aFilePath;
// Check if a file at the given path already exist and get a unique
path for the same
BOOL fileExists = [[NSFileManager defaultManager]
fileExistsAtPath:aFilePath];
while (fileExists)
{
NSString *pathExcludingFileName = [pathToReturn
stringByDeletingLastPathComponent];
NSString *lastPathComponent = [pathToReturn lastPathComponent];
NSString *fileExtension = [lastPathComponent pathExtension];
NSString *fileName = [lastPathComponent
stringByDeletingPathExtension];
fileName = [NSString stringWithFormat:@"%d",rand()];
// Add the path extension only if one exists
if ([fileExtension length] >0)
pathToReturn = [pathExcludingFileName
stringByAppendingPathComponent:[fileName
stringByAppendingPathExtension:fileExtension]];
else
pathToReturn = [pathExcludingFileName
stringByAppendingPathComponent:fileName];
fileExists = [[NSFileManager defaultManager]
fileExistsAtPath:pathToReturn];
}
return pathToReturn;
}
I'd be happy to know about any suggestions that may optimize the
above code.
-Chaitanya
On 24-Nov-08, at 12:30 AM, Jean-Nicolas Jolivet wrote:
You're right, hadn't thought of that! Well, I'll implement it like
that for now but I'll keep an eye on this thread, see if anyone has
a better idea! :)
On 23-Nov-08, at 1:55 PM, Andrew Merenbach wrote:
Hi,
I used something pretty much identical in a program of mine, but
quickly realized that, although it worked, it wasn't scalable
enough -- it made me feel a little uneasy. For instance, if you
are saving a file over a slow network, it'll have to check each
file's existence every time -- but it could take significantly
longer for each file in such a case. I'm not sure what would be
best, frankly. I hope that someone else will chime in with a
built-in solution!
Best,
Andrew
On Nov 23, 2008, at 10:49 AM, Jean-Nicolas Jolivet wrote:
mmm I guess I'm a little slow today hehe...
something like this would probably work (pseudo-code):
cnt = 1
filename = "Picture " + cnt
while(file exist (filename))
cnt ++
On 23-Nov-08, at 1:45 PM, Jean-Nicolas Jolivet wrote:
I would love to implement something like the default screenshot
utility for OS X does when it names its pictures.. (i.e. Picture
1, Picture 2, Picture 3.. etc.)
I was wondering if there's already something in place to do
that? If not, I don't mind implementing it myself but I can't
really think of a simple way to do it? Counting the files in a
directory is not really a reliable way to do it (i.e. if you
have Picture 2 and Picture 3 in a directory, your count is 2...
try naming your new picture Picture 3 and it overwrites the
existing Picture 3 ...)
I'm fairly good with RegEx so it would be easy to parse
filenames for numbers I guess but it seems a little overkill for
such a trivial task... (especially since my app wouldn't use
Regex for anything else so, I dont want to add a regex framework
just for that)...
Any ideas would be appreciated?
Jean-Nicolas Jolivet
email@hidden
http://www.silverscripting.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the
list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
Jean-Nicolas Jolivet
email@hidden
http://www.silverscripting.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the
list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
Jean-Nicolas Jolivet
email@hidden
http://www.silverscripting.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden