NSFileManager copyItemAtPath toPath - objective-c

I wanna watch a folder, then copy different extensioned & same filenamed files to another directory/folder. But I check the files with
if([myManager fileExistsAtPath:[(NSURL*)pathSource path]]==YES
it detects ,return YES, there is file in directory.But never copied any of Video-Audio-txt files with
[myManager copyItemAtPath:#"/Users/.../CONTENTS/002BJ" toPath:#"/Users/.../CONTENTS/AUDIO/"; error:nil]
codes. So what's my wrong or Is there any another way to copy/move files to directories?

When a file is being copied, the destination path must end in a
filename—there is no implicit adoption of the source filename.
NSString *path = #"/Users/.../CONTENTS/002BJ";
[myManager copyItemAtPath:path toPath:[#"/Users/.../CONTENTS/AUDIO/" stringByAppendingPathComponent:[path lastPathComponent] error:nil];

Related

Get pdf files from Bundle

I created the Settings Bundle
I done it. Because i need serf inside the bundle as the folder. For Example FileManager.
Below you can see.
And I opened meeting.Bundle in Finder using show contents and put into the Folders with pdf files. I try get these pdf files it on the device (iPhone 5) with code
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:#"meeting" ofType:#"bundle"];
NSString *filePathPdf = [[NSBundle bundleWithPath:bundlePath] pathForResource:theFileName ofType:#"pdf"];
But *filePathPdf is nil. Xcode cannot find them.
What i do wrong?
PS: I found the way how to load pdf files but i think this is wrong method.
I put into meeting.bundle the folder with pdf files and put the same folder into main bundle.
Load with path:
NSString *fileNamePdf = [self.contents objectAtIndex:indexPath.row];
NSString* theFileName = [[fileNamePdf lastPathComponent] stringByDeletingPathExtension];
NSString *filePathPdf = [[NSBundle mainBundle] pathForResource:theFileName ofType:#"pdf"];
Pdf Files loading from main bundle. But references of pdf files are taken from meeting.bundle
Maybe somebody knows how to make the right to work?

Downloading a file from Dropbox for documents directory deletes all my files

My app shows dropbox files, and the documents directory. when viewing dropbox files i can press a button that shows a list of all my document folders, and subdirectories in those folders so they can chose the folder they want to put that file in the documents. when doing this i used dropbox's objective c API they say to use in the documentation to download a file into a path on the system.
NSString *DocumentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
for (NSIndexPath *indexPath in [dropboxFilesTable indexPathsForSelectedRows]) {
DBMetadata *fileMetadata = [[dropboxFolderMetadata contents] objectAtIndex:indexPath.row];
[[self restClient] loadFile:fileMetadata.path intoPath:DocumentsPath];
}
the delegate for the dropbox says it is successfully put inside, but when i went back in, every folder and downloaded file has been deleted and that file is still not in the folder. i thought it would need the final path like "path to documents"/"filename", but then dropbox gives a error for that. Anyone know why its deleting all my files and not putting it in my documents?
The intoPath parameter needs to be a full filename path, not a directory. You need to update the last line to something like:
NSString *filename = [fileMetadata.path lastPathComponent];
NSString *destPath = [DocumentsPath stringByAppendingPathComponent:filename];
[[self restClient] loadFile:fileMetadata.path intoPath:destPath];
Also make sure that you are only doing this will files and not folders.

Reading Text File from Xcode Bundle

Why can't I read the content of foo.rtf? I've already put it in Xcode bundle. fileRoot still contains null.
NSString* filePath = #"foo";
NSString* fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"rtf"];
NSLog(#"File root contains %#", fileRoot);
#TheAmateurProgrammer, Thank you for the diagram. I just added foo.rtf into the bundle resources. The result for fileRoot is still null. What step am I still missing?
Target's "Copy Bundle Resources" in Build Phases now contains foo.rtf. (I can't insert picture as I'm still a newbie).
(I will add the content reading after I can get fileRoot to point correctly).
You added the file, but is it really copied to your application bundle?
Make sure your rtf file is copied into your resource.
Secondly, you're only getting the path of the rtf, and not the contents of the rtf.
NSString *fileRoot = [[NSBundle mainBundle] pathForResource:filePath ofType:#"rtf"];
NSString *contents = [[[NSAttributedString alloc] initWithRTF:[NSData dataWithContentsOfFile:fileRoot] documentAttributes:NULL] string];
That will get the contents of the rtf.
First check that your file is present inside the bundle or not and if it present it must have same name as "foo.rtf".
You are getting nil because your file is not present directly inside in the bundle.
once you get the right file path you can get the content of your text file as a string by calling initWithContentsOfFile method on NSString class.

NSFileManager finds files inside a folder only when it's running under a debugger

When I run the following code under the Xcode debugger it successfully finds the package with .app extension, but when I run it standalone "file" object is nil. In fact when I did NSLogs folderEnum was also nil. Note that folderPath points to a folder that is in the same directory as the the program executable.
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSDirectoryEnumerator *folderEnum = [localFileManager enumeratorAtPath:folderPath];
NSString *file;
while (file = [folderEnum nextObject]) {
if ([[file pathExtension] isEqualToString: #"app"]) {
break;
}
}
Any ideas? Something to do with the Mac system file permissions?
Edit
I should have probably mentioned that folderPath was actually a relative path and not an absolute one. So I changed folderPath to be relative to [[NSBundle mainBundle] bundlePath] path and it works now. But if anyone can shed some light why relative path doesn't work that be great.
Does changing the first line to:
NSFileManager *localFileManager = [NSFileManager defaultManager];
make any difference? Are you just trying to get the path for your application? (There are easier ways)

How to browse folders in Objective C?

I have a folder and inside that, few files. How can i get that files without using any UI classes?
The small eg given below will make question more clearer:
Suppose in my home folder, there is a folder Printers, inside that there are three folders Printer1, Printer2, Printer3. Now inside each subfolder there is xml file like printer1.xml inside printer1 folder, printer2.xml inside printer2 folder, printer3.xml inside printer3 folder. I have to browse through the folders and store the path to printer3.xml in a variable (of type NSString).
Please suggest me the ways to do that..
You might want to use NSFileManager and related classes..This tutorial might be useful too..
You can use NsFileManger to read directories and files,
here a sample of using FileManager to read documents dir file
//Object file manager
NSFileManager *FileManager = [NSFileManager defaultManager];
//Read Program folder and create an Array with all folder
NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//Element in 0 position contain Documents directory
//Faccio la dir dei file della directory
Files = [FileManager contentsOfDirectoryAtPath:[Paths objectAtIndex:0] error:nil];
if(Files) {
//E uno per uno li cancello
for (NSString *File in Files) {
//Here YOU CAN CREATE YOUR STRING, File CONTAINS file Name
}
}