NSFileManager unique file names
Asked Answered
M

9

45

I need a quick and easy way to store files with unique file names on iOS. I need to prefix the file with a string, and then append the generated unique identifier to the end. I was hoping NSFileManager had some convenient method to do this, but I can't seem to find it.

I was looking at createFileAtPath:contents:attributes:, but am unsure if the attributes will give me that unique file name.

Mckinney answered 13/10, 2011 at 19:11 Comment(1)
This Stack Overflow[1] might be is what you need. [1]: #216320Rae
M
79

Create your own file name:

CFUUIDRef uuid = CFUUIDCreate(NULL);
CFStringRef uuidString = CFUUIDCreateString(NULL, uuid);
CFRelease(uuid);
NSString *uniqueFileName = [NSString stringWithFormat:@"%@%@", prefixString, (NSString *)uuidString];
CFRelease(uuidString);

A simpler alternative proposed by @darrinm in the comments:

NSString *prefixString = @"MyFilename";

NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString] ;
NSString *uniqueFileName = [NSString stringWithFormat:@"%@_%@", prefixString, guid];

NSLog(@"uniqueFileName: '%@'", uniqueFileName);

NSLog output:
uniqueFileName: 'MyFilename_680E77F2-20B8-444E-875B-11453B06606E-688-00000145B460AF51'

Note: iOS6 introduced the NSUUID class which can be used in place of CFUUID.

NSString *guid = [[NSUUID new] UUIDString];
Mannuela answered 13/10, 2011 at 19:31 Comment(5)
A nice shortcut to get a GUID string is: NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString]Ib
That's great, but what is the probability of collisions?Adolpho
@user578205 From the docs: A UUID is made unique over both space and time by combining a value unique to the computer on which it was generated and a value representing the number of 100-nanosecond intervals. There is a reason they are called "unique".Mannuela
And if UUIDs will be generated in the cycle, the function srand () to generate a UUID will help solve the problem?Adolpho
The only potential problem is if you generate the UUIDs faster than every 100 nanoseconds which on todays processors is probably not possible. Also srand() and it's family are not really good at random numbers, arc4random() is the way to go for random numbers.Mannuela
S
10

Super-easy Swift 4 1-liner:

fileName = "MyFileName_" + UUID().uuidString

or

fileName = "MyFileName_" + ProcessInfo().globallyUniqueString
Stepsister answered 1/1, 2018 at 21:4 Comment(0)
A
9

I use current date to generate random file name with a given extension. This is one of the methods in my NSFileManager category:

+ (NSString*)generateFileNameWithExtension:(NSString *)extensionString
{
    // Extenstion string is like @".png"

    NSDate *time = [NSDate date];
    NSDateFormatter* df = [NSDateFormatter new];
    [df setDateFormat:@"dd-MM-yyyy-hh-mm-ss"];
    NSString *timeString = [df stringFromDate:time];
    NSString *fileName = [NSString stringWithFormat:@"File-%@%@", timeString, extensionString];

    return fileName;
}
Adjourn answered 25/2, 2013 at 10:18 Comment(3)
WARNING: Using the date can cause collisions during daylight savings time -1. (In the fall when the same hour is repeated.)Torus
To prevent collisions when daylight savings time changes we can append "Z" specifier to the date format string (it represents current time zone offset). Maybe this will help someone.Lifework
Unless you want the date to be part of the fileName, then this should be avoided because of collisions as Bob mentions, that initializing a Date object is an expensive and that Apple already provides alternative ways of creating a unique identifierPlatypus
Q
7

You can also use the venerable mktemp() (see man 3 mktemp). Like this:

- (NSString*)createTempFileNameInDirectory:(NSString*)dir
{
  NSString* templateStr = [NSString stringWithFormat:@"%@/filename-XXXXX", dir];
  char template[templateStr.length + 1];
  strcpy(template, [templateStr cStringUsingEncoding:NSASCIIStringEncoding]);
  char* filename = mktemp(template);

  if (filename == NULL) {
    NSLog(@"Could not create file in directory %@", dir);
    return nil;
  }
  return [NSString stringWithCString:filename encoding:NSASCIIStringEncoding];
}

The XXXXX will be replaced with a unique letter/number combination. They can only appear at the end of the template, so you cannot have an extension appended in the template (though you can append it after the unique file name is obtained). Add as many X as you want in the template.

The file is not created, you need to create it yourself. If you have multiple threads creating unique files in the same directory, you run the possibility of having race conditions. If this is the case, use mkstemp() which creates the file and returns a file descriptor.

Quay answered 14/6, 2012 at 18:39 Comment(1)
You can actually have an extension.. path = @".../XXXXXX.pdf"and then do mkstemps(path, suffixLength). suffixLength = 4 in this case.Cornie
R
5

In iOS 6 the simplest method is to use:

NSString *uuidString = [[NSUUID UUID] UUIDString];
Rondel answered 15/7, 2014 at 0:30 Comment(0)
I
5

Here is what I ended up using in Swift 3.0

public func generateUniqueFilename (myFileName: String) -> String {

    let guid = ProcessInfo.processInfo.globallyUniqueString
    let uniqueFileName = ("\(myFileName)_\(guid)")

    print("uniqueFileName: \(uniqueFileName)")

    return uniqueFileName
}
Indebted answered 29/1, 2016 at 18:36 Comment(2)
For Swift 3: let guid = ProcessInfo.processInfo.globallyUniqueStringUnstoppable
Thanks! Updated my answer.Indebted
P
3

Swift 4.1 and 5. Just pass you file extension name and function will return unique file name.

func uniqueFileNameWithExtention(fileExtension: String) -> String {
        let uniqueString: String = ProcessInfo.processInfo.globallyUniqueString
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyyMMddhhmmsss"
        let dateString: String = formatter.string(from: Date())
        let uniqueName: String = "\(uniqueString)_\(dateString)"
        if fileExtension.length > 0 {
            let fileName: String = "\(uniqueName).\(fileExtension)"
            return fileName
        }
        
        return uniqueName
    }
Phyte answered 20/6, 2018 at 15:37 Comment(0)
S
2

This should probably work for you:

http://vgable.com/blog/2008/02/24/creating-a-uuid-guid-in-cocoa/

The author of the post suggests implementing a 'stringWithUUID' method as a category of NSString. Just append a GUID generated with this method to the end of the file name that you're creating.

Schreck answered 13/10, 2011 at 19:29 Comment(0)
C
2

Swift 4.2, I use two options, one mostly unique but readable, and the other just unique.

// Create a unique filename, added to a starting string or not
public func uniqueFilename(filename: String = "") -> String {
    let uniqueString = ProcessInfo.processInfo.globallyUniqueString
    return filename + "-" + uniqueString
}

// Mostly Unique but Readable ID based on date and time that is URL compatible ("unique" to nearest second)
public func uniqueReadableID(name: String = "") -> String {

    let timenow = DateFormatter.localizedString(from: Date(), dateStyle: .medium, timeStyle: .medium)
    let firstName = name + "-" + timenow
    do {
        // Make ID compatible with URL usage
        let regex = try NSRegularExpression(pattern: "[^a-zA-Z0-9_]+", options: [])
        let newName = regex.stringByReplacingMatches(in: firstName, options: [], range: NSMakeRange(0, firstName.count), withTemplate: "-")
        return newName
    }
    catch {
        print("🧨 Unique ID Error: \(error.localizedDescription)")
        return uniqueFilename(filename: name)
    }
}
Checker answered 21/12, 2018 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.