Questions tagged [nsfilemanager]

The NSFileManager class enables you to perform various generic file-system operations and insulates an application from the underlying file system.

An apple class reference.

A simple file handling

//Gives you document folder detail

    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager setDelegate:self];
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSArray *newfilelist=[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error ];

//gives your file is file or directory

NSString *path = @"/Users";
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSLog(@"fileManager=%@", fileManager);
    BOOL bDirectory;
    BOOL bExists = [fileManager fileExistsAtPath:path isDirectory:&bDirectory];
    NSLog(@"%@: bExists=%d bDirectory=%d", path, bExists, bDirectory);
1400 questions
218
votes
7 answers

How to check if a file exists in Documents folder?

I have an application with In-App Purchase, that when the user buy something, download one html file into the Documents folder of my app. Now I must check if this HTML file exists, so if true, load this HTML file, else load my default html page. How…
Obliviux
  • 2,639
  • 4
  • 19
  • 11
179
votes
12 answers

Is there any way to see the file system on the iOS simulator?

Is there any way to browse the file system of a currently running or just killed iOS simulator? I'd settle for being able to see a specific app's files if there's a way to do that. Note that I don't want to do this programatically. I want to…
Chris Williams
  • 9,399
  • 14
  • 53
  • 88
151
votes
9 answers

Create a folder inside documents folder in iOS apps

I just want to create new folders in the documents folder of my iPhone app. Does anybody know how to do that? Appreciate your help!
Mina Mikhael
  • 2,799
  • 6
  • 25
  • 31
149
votes
18 answers

How to detect total available/free disk space on the iPhone/iPad device?

I'm looking for a better way to detect available/free disk space on the iPhone/iPad device programmatically. Currently I'm using the NSFileManager to detect the disk space. Following is the snippet of the code which does the job for…
Code.Decode
  • 3,494
  • 4
  • 22
  • 30
94
votes
1 answer

What's a quick way to test to see a file exists?

I want to quickly check to see if a file exists in my iPhone app's Documents directory (or any path for that matter). I can enumerate through the directory's files, or I can try to open a specific file. What's the fastest way? I just need to know…
mahboudz
  • 38,588
  • 16
  • 95
  • 123
77
votes
14 answers

Iterate through files in a folder and its subfolders using Swift's FileManager

I'm quite new to programming a Swift and I'm trying to iterate through the files in a folder. I took a look at the answer here and tried to translate it to Swift syntax, but didn't succeed. let fileManager = NSFileManager.defaultManager() let…
Iacopo Boccalari
  • 1,115
  • 2
  • 11
  • 17
73
votes
6 answers

Testing file existence using NSURL

Snow Leopard introduced many new methods to use NSURL objects to refer to files, not pathnames or Core Services' FSRefs. However, there's one task I can't find a URL-based method for: Testing whether a file exists. I'm looking for a URL-based…
Peter Hosey
  • 93,914
  • 14
  • 203
  • 366
72
votes
15 answers

How can I calculate the size of a folder?

I'm creating a folder to cache images inside Documents with my iPhone App. I want to be able to keep the size of this folder down to 1MB, so I need to to check the size in bytes of my folder. I have code to calculate the size of file, but I need…
joseph_carney
  • 1,475
  • 2
  • 14
  • 17
68
votes
3 answers

Check if file exists at URL instead of path

How can I check if a file exists at a URL (instead of a path), in order to set a pre-populated default store into the iPhone Simulator: NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Food.sqlite"]; /* Set up…
Pedro Sousa
  • 797
  • 1
  • 7
  • 12
64
votes
5 answers

Finding file's size

In my iPhone app I am using the following code to find a file's size. Even though the file exists, I am seeing zero for the size. Can anyone help me? Thanks in advance. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,…
Kiran
  • 1,271
  • 3
  • 15
  • 25
58
votes
8 answers

How to create directory using Swift code (NSFileManager)

I'm having some trouble with converting Objective-C code to create a directory for Swift. Objective-C: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths…
Gian
  • 1,041
  • 2
  • 8
  • 20
54
votes
9 answers

How to overwrite a file with NSFileManager when copying?

I'm using this method to copy a file: [fileManager copyItemAtPath:sourcePath toPath:targetPath error:&error]; I want to overwrite a file when it exists already. The default behavior of this method is to throw an exception/error "File Exists." when…
Proud Member
  • 38,700
  • 43
  • 143
  • 225
52
votes
8 answers

Delete files from directory inside Document directory?

I have created a Temp directory to store some files: //MARK: -create save delete from directory func createTempDirectoryToStoreFile(){ var error: NSError? let paths =…
user4790024
49
votes
2 answers

NSFileManager.defaultManager().fileExistsAtPath returns false instead of true

How is it possible? let exists = NSFileManager.defaultManager().fileExistsAtPath(path.absoluteString) print("exists: \(exists)") //false This is…
Bartłomiej Semańczyk
  • 52,820
  • 43
  • 206
  • 318
45
votes
6 answers

Is there a safer way to create a directory if it does not exist?

I've found this way of creating a directory if it does not exist. But it looks a bit wonky and I am afraid that this can go wrong in 1 of 1000 attempts. if(![[NSFileManager defaultManager] fileExistsAtPath:bundlePath]) { [[NSFileManager…
1
2 3
93 94