nsfilemanager

What characters are allowed in a iOS file name?

不打扰是莪最后的温柔 提交于 2019-11-30 11:02:52
I'm looking for a way to make sure a string can be used as a file name under iOS. I'm currently in the section of the code that deletes incompatible characters. I'm wondering if I'm doing it right. NSString *filename = @"A file name"; fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet controlCharacterSet]]; fileName = [fileName stringByTrimmingCharactersInSet: [NSCharacterSet newlineCharacterSet]]; I'm also wondering if there's already a method that validates a string as a file name. Thank you for your advice! Erik B First of all, you're using the wrong method. Trimming the

How to rename a file using NSFileManager

独自空忆成欢 提交于 2019-11-30 10:55:41
问题 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? 回答1: You can use moveItemAtPath. NSError * err = NULL; NSFileManager * fm = [[NSFileManager alloc] init]; BOOL result = [fm moveItemAtPath:@"/tmp/test.tt" toPath:@"/tmp/dstpath.tt" error:&err]; if(!result) NSLog(@"Error: %@", err); [fm release]; 回答2: To keep this

Rename file in Cocoa?

北慕城南 提交于 2019-11-30 10:55:12
How would I rename a file, keeping the file in the same directory? I have a string containing a full path to a file, and a string containing a the new filename (and no path), for example: NSString *old_filepath = @"/Volumes/blah/myfilewithrubbishname.avi"; NSString *new_filename = @"My Correctly Named File.avi"; I know about NSFileManager's movePath:toPath:handler: method, but I cannot workout how to construct the new file's path.. Basically I'm looking for the equivalent to the following Python code: >>> import os >>> old_filepath = "/Volumes/blah/myfilewithrubbishname.avi" >>> new_filename =

NSFileManager watch directory

主宰稳场 提交于 2019-11-30 10:23:46
How would you monitor a directory with NSFileManager ? I would like to able to detect when a file is deleted/added in my documents directory while my app is running. Look Kernel Queues: An Alternative to File System Events in Apple documentation. There is an example for iOS in AVPlayerDemo (look DirectoryWatcher class). Also, check Directory Monitor blog post. Here's my version of DirectoryWatcher written in Swift using GCD instead of Mach and using a closure instead of a delegate import Foundation @objc public class DirectoryWatcher : NSObject { override public init() { super.init() } deinit

Ignore .DS_Store and Icon files in a folder with Cocoa NSFIleManager

女生的网名这么多〃 提交于 2019-11-30 08:35:28
I'm trying to remove specific files from a directory using NSFileManager. I would like to ignore the hidden .DS_Store and Icon files (the folder that I'm checking has to have a custom icon) that are in the directory, however I keep accidentally deleting them as well. Right now, I'm doing the following: NSFileManager *manager = [NSFileManager defaultManager]; NSArray *dirContents = [manager contentsOfDirectoryAtPath:[selectedFolder stringValue] error:nil]; for (int i = 0; i < [dirContents count]; i++) { NSString *theFile = [dirContents objectAtIndex:i]; if([theFile isEqualToString:@".DS_Store"]

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

别来无恙 提交于 2019-11-30 07:04:18
问题 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. 回答1: URL includes the protocol being used (http:// etc). Path doesn't or doesn't need at least. 回答2: path is location of a resource (file/directory) in a file system . Just like iOS File System, other environments file system can be Windows file system, Unix etc. Path can have spaces like /docs

Testing file existence using NSURL

僤鯓⒐⒋嵵緔 提交于 2019-11-30 05:55:18
问题 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 version of -[NSFileManager fileExistsAtPath:]. Like that method, it should return YES if the URL describes anything, whether it's a regular file, a directory, or anything else. I could attempt to look up various resource values, but none of them are

iOS - Flag entire Document directory as do not backup

半世苍凉 提交于 2019-11-30 04:57:57
My application got rejected by Apple due to : 2.23: Apps must follow the iOS Data Storage Guidelines or they will be rejected so is it possible that flag entire Document directory as do no backup ? my application runs in iOS 6.1 and higher . I am familiar with this code , but I don't know hot to mark my Document directory with it - (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL { assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]); NSError *error = nil; BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error:

Get the count of files in a directory

江枫思渺然 提交于 2019-11-30 04:23:26
问题 How can I count the files in a directory? I couldn't find anything relevant in the class reference of NSFileManager . 回答1: contentsOfDirectoryAtPath:error: returns an NSArray . Just send count to the array. 回答2: contentsOfDirectoryAtPath:error: returns an NSArray of everything in a directory (Files + Folders). To get a count of files only, you can filter as follows: NSMutableArray *files = [[NSMutableArray alloc] init]; NSArray *itemsInFolder = [[NSFileManager defaultManager]

Copy Folder (w/contents) from bundle to Documents directory - iOS

谁都会走 提交于 2019-11-30 03:49:54
EDIT: SOLVED Thanks Brooks. Your question led me to keep digging into if the file even existed in my bundle - and it did not! So by using this code (also below): iPhone/iPad: Unable to copy folder from NSBundle to NSDocumentDirectory and the instructions for adding a directory properly to Xcode (from here and below)I was able to get it to work. Copy folder into Xcode: Create a Directory on your Mac. Select Add Existing Files to your project Select the Directory you want to import In the pop-up window make sure you select "Copy items into destination group's folder" and "Create Folder