Get name of subdirectories within Reference Folder ios - ios

I need to ask is there is any way to fetch the name or the path of the folders/subdirectories with in the Reference folder.
I have figured out the way to get the path of the files(.png) in Array within a reference folder but I am curious that is there any way to find the folder also ???

You can try like this where you can pass your directory name as documentsURL
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
NSURL *documentsURL = [paths lastObject];
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:documentsURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:^BOOL(NSURL *url, NSError *error)
{
if (error) {
NSLog(#"[Error] %# (%#)", error, url);
return NO;
}
return YES;
}];
for (NSString* file in enumerator) {
NSLog(#"File %#",file);
}

Related

How to rename files/folders from document folder programmatically in iOS

I am working on functionality to save video files in document folder of application in iOS.
How to rename some files programmatically?
try this code :
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);
other wise use this method to rename file
- (void)renameFileWithName:(NSString *)srcName toName:(NSString *)dstName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathSrc = [documentsDirectory stringByAppendingPathComponent:srcName];
NSString *filePathDst = [documentsDirectory stringByAppendingPathComponent:dstName];
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePathSrc]) {
NSError *error = nil;
[manager moveItemAtPath:filePathSrc toPath:filePathDst error:&error];
if (error) {
NSLog(#"There is an Error: %#", error);
}
} else {
NSLog(#"File %# doesn't exists", srcName);
}
}
There is no Direct API to rename the file . Though when you move the file from one place to another place, if that file in destination path not exists then iOS will create the file in the given name. For file you can just give the New name of your file, how it should be displayed/referenced.
you could check this answer. Good luck!

Error checking for subdirectory of Documents directory in IOS

My app needs to create a subdirectory of the Documents directory. The code below returns the directory URL. First time through it creates the directory, but fails next time because fileExistsAtPath: claims the directory still doesn't exist. I know it exists though because I have set the bundle to make files visible in iTunes and I can see it in iTunes. Any idea what I'm doing wrong?
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (NSURL *)applicationSubDirectory
{
NSFileManager *manager = [NSFileManager defaultManager];
NSURL *docDir = [self applicationDocumentsDirectory];
docDir = [docDir URLByAppendingPathComponent:#"Sub" isDirectory:YES];
NSString *docDirPath = [docDir absoluteString];
BOOL isFolder = YES;
if(!([manager fileExistsAtPath:docDirPath isDirectory:&isFolder] && isFolder)) {
NSError *error = nil;
BOOL ret = [manager createDirectoryAtURL:docDir withIntermediateDirectories:NO attributes:nil error:&error];
if(!ret) {
NSLog(#"ERROR app support: %#", error);
abort();
}
}
return docDir;
}
You want to use path instead of absoluteString The former returns the full url absolute path, something like "file:///..." while the latter only returns the file path.
NSString *docDirPath = [docDir path];
So, since you're looking for a file with the path "file:///...." which will never exist, the check always fails. When you go to create the directory, you create it with the URL, so it works.

how to find subfolder in an existing folder in iOS

My folder contains only one sub-folder, and I don't know sub-folder's name. This sub-folder contains a html file, and once again I don't know the html file's name.
My question is how I can get full path of this file by using
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:filename];
//- access sub-folder?
//- access html file?
EDITED:
I wrote a method to return the only one sub-folder as follow:
+ (NSString*) get1stSubFolder:(NSString*)folder
{
NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:folder];
//- no recursive
[directoryEnumerator skipDescendents];
NSString* file;
while (file = [directoryEnumerator nextObject])
{
BOOL isDirectory = NO;
BOOL subFileExists = [[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDirectory];
if (subFileExists && !isDirectory) {
return file;
}
}
return nil;
}
I always get nil as result. Do you know where was I wrong at?
Use NSDirectoryEnumerator. It should help you.
Use NSDirectoryEnumerator like this.
NSURL *documentsDirectoryURL = [NSURL URLWithString:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] ];
///If the folder you want to browse for subfolders is NSDocumentDirectory.
NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];
NSDirectoryEnumerator *enumerator = [[[NSFileManager alloc] init]
enumeratorAtURL:documentsDirectoryURL
includingPropertiesForKeys:keys
options:0
errorHandler:^(NSURL *url, NSError *error) {
return YES;
}];
for (NSURL *url in enumerator) {
NSError *error;
NSNumber *isDirectory = nil;
if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) {
NSLog(#"Error %#",error);
}
else if ([isDirectory boolValue]) {
NSLog(#"Folder URL: %#",url);
}else{
NSLog(#"File URL: %#",url);
}
}

Renaming all files and directories in NSLibraryDirectory

How can I rename all files and directories to lower case in NSLibraryDirectory\myFolder?
I found that I can to use:
[[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:[oldPath lowercaseString] error:&error];
to renaming directories.
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:path error:nil];
To getting array with directory contents but how I can check if directoryContent[0] is directory or file.
You could use this to check if a NSUrl is a directory or file:
for (NSURL *item in directoryContent) {
NSString *path = [item path];
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) {
if (isDir) {
NSLog(#"%# is a directory", path);
} else {
NSLog(#"%# is a file", path);
}
} else {
NSLog(#"%# does not exist", path);
}
}
UPDATE:
To rename all contents of your directory you could use this:
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:path error:nil];
BOOL isDir;
for (NSURL *item in directoryContent) {
if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) {
if (isDir) {
// create a new directory with lowercase name,
// move contents of old directory to the new one
// then delete the old directory
} else {
[[NSFileManager defaultManager] moveItemAtPath:path toPath:[path lowercaseString] error:&error];
}
}
}
//Call [self purgeDirectory:__NSLibraryDirectoryPath];
+(void)purgeDirectory:(NSString *)directoryPath __deprecated_msg("For debug purposes only") {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *directoryContent = [fileManager contentsOfDirectoryAtPath:directoryPath error:&error];
for (NSString *itemPath in directoryContent) {
BOOL isDir;
if ([[NSFileManager defaultManager] fileExistsAtPath:itemPath isDirectory:&isDir]) {
if (isDir) {
[self purgeDirectory:itemPath];//subdirectory
} else {
[[NSFileManager defaultManager] removeItemAtPath:itemPath error:&error];
}
}
}
}

How to delete a file from iTunes file sharing folder iOS

I have no problem with getting the path of the files that are located in the iTunes file sharing folder.. What I want is to delete them completely when the delete button is hit..
One more question.. is it possible to distinguish the file extension when delete button is hit? for example if it's an avi file, then alert user that he is about to delete a movie?
Thanks...
Thanks to Yannik L. Its now working.. I was just wondering one more thing.. How can I delete files with non-english charachers..?
The iTunesFileSharing folder is simply the document folder. You can retrieve the path by executing this code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *folderPath = [paths objectAtIndex:0];
Then you can run through the files available into the document folder and test their extension to check if they are AVI files:
- (BOOL)existsMovieFilesAtPath:(NSString *)folderPath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *contents = [fileManager contentsOfDirectoryAtPath:folderPath error:&error];
if (error == nil)
{
for (NSString *contentPath in contents)
{
NSString *fileExt = [contentPath pathExtension];
// If the current file is an AVI file
if ([fileExt isEqualToString:#"avi"])
{
return YES;
}
}
}
return NO;
}
And to delete the files you can make the same things:
- (BOOL)deleteFilesAtPath:(NSString *)folderPath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *contents = [fileManager contentsOfDirectoryAtPath:folderPath error:&error];
if (error == nil)
{
for (NSString *contentPath in contents)
{
[fileManager removeItemAtPath:contentPath error:NULL];
}
}
return NO;
}

Resources