TKMFileManager Class Reference

Inherits from NSObject
Declared in TKMFileManager.h
TKMFileManager.m

Overview

@class TKFileManager

This class was developed to make file management in iOS easier, and with less lines of code.

@superclass SuperClass: NSObject

  documentsDirectory

This readonly property holds the string path of the app’s Documents Directory.

@property (readonly, nonatomic) NSString *documentsDirectory

Declared In

TKMFileManager.h

  mainBundleDirectory

This readonly property holds the string path of the directory of the app’s Main Bundle.

@property (readonly, nonatomic) NSString *mainBundleDirectory

Declared In

TKMFileManager.h

– init

Initializes the TKMFileManager object.

- (id)init

Return Value

id - After initializing the paths, it returns itself.

Discussion

Assigns the correct paths to documentsDirectory and mainBundleDirectory. Then it returns itself as the TKMFileManager object.

TKMFileManager *manager = [[TKMFileManager alloc] init];

Declared In

TKMFileManager.h

– createDirectory:

Creates a new directory at newDirectoryPath.

- (void)createDirectory:(NSString *)newDirectoryPath

Parameters

newDirectoryPath

The full path for the directory you wish to create.

Return Value

void - there isn’t anything to return.

Discussion

Creates a new directory at newDirectoryPath, if it doesn’t already exist.

NSString *newDirectory = [manager.documentsDirectory stringByAppendingPathComponent:@"NewDirectory"]];
[manager createDirectory:newDirectory];

Warning: The path must be a valid path that is accessible in the app’s sandbox.

Declared In

TKMFileManager.h

– createSubdirectory:inDirectory:

Creates a subdirectory named subdirectoryName in directoryPath.

- (void)createSubdirectory:(NSString *)subdirectoryName inDirectory:(NSString *)directoryPath

Parameters

subdirectoryName

The name of the subdirectory you wish to create.

directoryPath

The path of the directory in which you wish to make a subdirectory.

Return Value

void - there isn’t anything to return.

Discussion

Creates a new directory at subdirectoryName, if it doesn’t already exist.

NSString *subdirectory = @"/SubDirectory";
[manager createSubdirectory:subdirectory inDirectory:manager.documentsDirectory];

Warning: If subdirectoryName does not contain “/” at the beginning, it will automatically be added.

Declared In

TKMFileManager.h

– copyDirectory:toDirectory:

Copies the contents of one directory into another.

- (void)copyDirectory:(NSString *)directoryPath toDirectory:(NSString *)destinationDirectoryPath

Parameters

directoryPath

The path of the directory who’s contents you’d like to copy.

destinationDirectoryPath

The path of the directory into which you’d like the contents of directoryPath to be copied.

Return Value

void - there isn’t anything to return.

Discussion

Copies the contents of directoryPath into destinationDirectoryPath.

[manager copyDirectory:manager.mainBundleDirectory toDirectory:manager.documentsDirectory];

Warning: It does not create a new directory for the files to be copied into.

Declared In

TKMFileManager.h

– moveDirectory:toDirectory:

Moves the contents of one directory into another.

- (void)moveDirectory:(NSString *)directoryPath toDirectory:(NSString *)destinationDirectoryPath

Parameters

directoryPath

The path of the directory who’s contents you’d like to move.

destinationDirectoryPath

The path of the directory into which you’d like the contents of directoryPath to be moved.

Return Value

void - there isn’t anything to return.

Discussion

Moves the contents of directoryPath into destinationDirectoryPath. Note: directoryPath is deleted as a result of this operation

[manager moveDirectory:manager.mainBundleDirectory toDirectory:mainBundleInDocuments];

Warning: It does not create a new directory for the files to be moved into. Also, it is not recommended to move the contents of the Main Bundle into the Documents Directory.

Declared In

TKMFileManager.h

– renameDirectory:toName:

Renames the directory located at directoryPath to newName.

- (void)renameDirectory:(NSString *)directoryPath toName:(NSString *)newName

Parameters

directoryPath

The path of the directory you’d like to rename.

newName

The new name you’d like to give the directory.

Return Value

void - there isn’t anything to return.

Discussion

Under the hood, what this really does is creates a new directory at the same level as directoryPath with the name of newName, then moves the contents of directoryPath to newName’s directory.

NSString *directoryToRename = [manager.documentsDirectory stringByAppendingPathComponent:@"/SubDirectory"];
[manager renameDirectory:directoryToRename toDirectory:@"RenamedDirectory"];

Warning: Do not attempt to use this method to rename files

Declared In

TKMFileManager.h

– deleteDirectory:

Deletes a directory and its contents.

- (void)deleteDirectory:(NSString *)directoryPath

Parameters

directoryPath

The path of the directory you’d like to delete.

Return Value

void - there isn’t anything to return.

Discussion

Deletes a directory and its contents.

[manager deleteDirectory:manager.documentsDirectory];

Warning: Deleting the Main Bundle directory or the Documents Directory is not recommended.

Declared In

TKMFileManager.h

– getFile:inDirectory:

Returns the filepath of a file located in the desired directory.

- (NSString *)getFile:(NSString *)filename inDirectory:(NSString *)directoryPath

Parameters

filename

The name of the file who’s full path you’d like to retrieve.

directoryPath

The path of the directory which contains the file filename.

Return Value

NSString - An NSString containing the path to the desired file.

Discussion

Returns the filepath of a file named filename located in the directory directoryPath.

NSString *exampleFilePath = [manager getFile:@"example.png" inDirectory:manager.documentsDirectory];

Declared In

TKMFileManager.h

– findAndGetFile:

Returns the filepath of a file located in an unknown directory.

- (NSString *)findAndGetFile:(NSString *)filename

Parameters

filename

The name of the file you’d like to retrieve the path of, but don’t know the directory of.

Return Value

NSString - An NSString containing the path to the desired file.

Discussion

Recursively searches all available directories in the app’s sandbox and returns the path to the first instance of a file named filename.

NSString *exampleFilePath = [manager findAndGetFile:@"example.png"];

Warning: For use when the directory the desired file is located in is not known.

Declared In

TKMFileManager.h

– copyFile:toDirectory:

Copies a file to a specified directory.

- (void)copyFile:(NSString *)filePath toDirectory:(NSString *)destinationDirectoryPath

Parameters

filePath

The path of the file you’d like to copy.

destinationDirectoryPath

The path of the directory into which you’d like the file to be copied.

Return Value

void - there isn’t anything to return.

Discussion

Copies a file with the path filePath to a directory with the path directoryPath. If directoryPath doesn’t exist, it is created.

NSString *exampleFilePath = [manager.mainBundleDirectory stringByAppendingPathComponent:@"example.txt"];
[manager copyFile:exampleFilePath toDirectory:manager.documentsDirectory];

Declared In

TKMFileManager.h

– findAndCopyFile:toDirectory:

Finds, then copies a file to a specified directory.

- (void)findAndCopyFile:(NSString *)filename toDirectory:(NSString *)destinationDirectoryPath

Parameters

filename

The name of the file you’d like to find and copy.

destinationDirectoryPath

The path of the directory into which you’d like the file to be copied, once found.

Return Value

void - there isn’t anything to return.

Discussion

Recursively searches all available directories for a file named filename, then copies the first found instance to a directory with the path directoryPath. If directoryPath doesn’t exist, it is created.

[manager findAndCopyFile:@"example.txt" toDirectory:manager.documentsDirectory];

Declared In

TKMFileManager.h

– moveFile:toDirectory:

Moves a file to a specified directory.

- (void)moveFile:(NSString *)filePath toDirectory:(NSString *)destinationDirectoryPath

Parameters

filePath

The path of the file you’d like to move.

destinationDirectoryPath

The path of the directory into which you’d like the file to be moved.

Return Value

void - there isn’t anything to return.

Discussion

Moves a file with the path filePath to a directory with the path directoryPath. If directoryPath doesn’t exist, it is created.

NSString *exampleFilePath = [manager.mainBundleDirectory stringByAppendingPathComponent:@"example.txt"];
[manager moveFile:exampleFilePath toDirectory:manager.documentsDirectory];

Declared In

TKMFileManager.h

– findAndMoveFile:toDirectory:

Finds, then moves a file to a specified directory.

- (void)findAndMoveFile:(NSString *)filename toDirectory:(NSString *)destinationDirectoryPath

Parameters

filename

The name of the file you’d like to find and move.

destinationDirectoryPath

The path of the directory into which you’d like the file to be moved, once found.

Return Value

void - there isn’t anything to return.

Discussion

Recursively searches all available directories for a file named filename, then moves the first found instance to a directory with the path directoryPath. If directoryPath doesn’t exist, it is created.

[manager findAndMoveFile:@"example.txt" toDirectory:manager.documentsDirectory];

Declared In

TKMFileManager.h

– deleteFile:

Deletes the file located at filePath.

- (void)deleteFile:(NSString *)filePath

Parameters

filePath

The path of the file you’d like to delete.

Return Value

void - there isn’t anything to return.

Discussion

Deletes the file located at filePath.

NSString *exampleFilePath = [manager.mainBundleDirectory stringByAppendingPathComponent:@"example.txt"];
[manager deleteFile:exampleFilePath];

Declared In

TKMFileManager.h

– findAndDeleteFile:

Finds, then deletes a file named filename.

- (void)findAndDeleteFile:(NSString *)filename

Parameters

filename

The name of the file you’d like to delete, once found.

Return Value

void - there isn’t anything to return.

Discussion

Recursively searches all available directories for a file named filename, then deletes the first found instance.

[manager findAndDeleteFile:@"example.txt"];

Declared In

TKMFileManager.h

– setDebugMode:

Sets the TKMFileManager object into Debug Mode.

- (void)setDebugMode:(BOOL *)debug

Parameters

debug

A Boolean that if YES sets Debug Mode on, if NO sets Debug Mode off

Return Value

void - there isn’t anything to return.

Discussion

Sets the TKMFileManager object into Debug Mode. This will print all NSLog() statements, instead of just the ones containing errors. By default, Debug Mode is off.

[manager setDebugMode:YES];

Declared In

TKMFileManager.h