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
44
votes
3 answers

Storing images locally on an iOS device

Some iOS photo-related apps store images created with the app somewhere other than the photo library. For example Fat Booth shows a scrolling list of photos created with the app at app start-up. Note these photos are persisted w/o the user…
SundayMonday
  • 17,607
  • 27
  • 94
  • 151
42
votes
7 answers

Swift 3.0 FileManager.fileExists(atPath:) always return false

When I use method .fileExists(atPath:)to judge whether the file is exist in file system, the method always return false to me. I checked the file system and the file do exist. Here is my code: let filePath = url?.path var isDir : ObjCBool =…
Dean Lee
  • 681
  • 1
  • 7
  • 13
39
votes
4 answers

How to rename a file using NSFileManager

I have a single file named a.caf in the documents directory. I would like to rename it when user types into a UITextField and presses change (the text entered in the UITextField should be the new filename). How can I do this?
Dipakkumar
  • 717
  • 1
  • 6
  • 14
38
votes
9 answers

iOS: How do you find the creation date of a file?

I'm trying to find the creation date (NOT modification date) of a file. Creation date doesn't appear to be in the attributes of a file, though modified date is. I'm using this code.. NSFileManager* fm = [NSFileManager defaultManager]; NSString*…
Ben Clayton
  • 75,781
  • 25
  • 117
  • 124
36
votes
5 answers

iphone - how to check if the file is a directory , audio , video or image?

Following my program shows contents of Documents directory and is displayed in a tableView . But the Documents directory contains some directories , some audio , some video , and some images etc . NSArray *dirPaths =…
Subbu
  • 2,013
  • 4
  • 24
  • 39
35
votes
5 answers

How to use iCloud to store and sync app files

I already have an iPhone App that stores data in a file in the local documents folder. Now I learnt about iCloud technologies and my first question was: is there a way to use iCloud as a directory when sometimes I check for new versions? I mean: can…
albianto
  • 3,942
  • 2
  • 38
  • 52
35
votes
9 answers

NSFileManager delete contents of directory

How do you delete all the contents of a directory without deleting the directory itself? I want to basically empty a folder yet leave it (and the permissions) intact.
Vervious
  • 5,471
  • 3
  • 35
  • 53
34
votes
5 answers

Get directory contents in date modified order

Is there an method to get the contents of a folder in a particular order? I'd like an array of file attribute dictionaries (or just file names) ordered by date modified. Right now, I'm doing it this way: get an array with the file names get the…
nevan king
  • 108,735
  • 42
  • 196
  • 237
33
votes
4 answers

Save file in document directory in swift 3?

I am saving files in a document directory in swift 3 with this code: fileManager = FileManager.default // let documentDirectory = fileManager?.urls(for: .documentDirectory, in: .userDomainMask).first as String var path =…
TechChain
  • 7,104
  • 20
  • 81
  • 193
33
votes
4 answers

Document folder iOS Simulator

How is possible to find quickly the document folder (in the mac) of an App when I'm using the simulator? When I need to explore the document folder during the simulation of the App now I use a variable of the App to find the document folder path and…
Lorenzo
  • 2,987
  • 2
  • 23
  • 48
31
votes
3 answers

Find parent directory of a path

Is there any way to find the parent directory of a path using NSFileManager or something? e.g. Take this: /path/to/something And turn it into /path/to/
indragie
  • 17,634
  • 15
  • 89
  • 161
31
votes
4 answers

Getting bundle file references / paths at app launch

Suppose I have an arbitrary set of files included in the Main App Bundle. I would like to fetch the file URLs for those at launch and store them somewhere. Is this possible using NSFileManager? The documentation is unclear in that regard. Note: I…
Andrew Lauer Barinov
  • 5,474
  • 9
  • 56
  • 80
30
votes
3 answers

iOS9 Swift File Creating NSFileManager.createDirectoryAtPath with NSURL

Before iOS9, we had created a directory like so let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! String let logsPath = documentsPath.stringByAppendingPathComponent("logs") let errorPointer =…
Moemars
  • 4,103
  • 3
  • 24
  • 27
28
votes
2 answers

What's the difference between path and URL in iOS?

In a class such as NSFileManager there are 2 versions of practically every method. One for paths and one for URLs. What's the difference? And what's the best practice for converting a URL to a path.
Andrew
  • 7,605
  • 6
  • 39
  • 68
27
votes
3 answers

How to check if a directory exists in Objective-C

I guess this is a beginner's problem, but I was trying to check if a directory exists in my Documents folder on the iPhone. I read the documentation and came up with this code which unfortunately crashed with EXC_BAD_ACCESS in the BOOL fileExists…
n.evermind
  • 11,764
  • 17
  • 74
  • 119
1
2
3
93 94