Get filename and extension from filepath programmatically cocoa? - cocoa

I know you can use NSBundle:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"foo" ofType:#"rtf"];
to get the filepath of a file, but how would I get the filename and extension (separately) from the filepath programmatically using NSBundle.
For example, I may have: /Users/theuser/Documents/yourdocument.txt
It is obvious that the file is 'yourdocument', and the extension 'txt'
But I need to get the filename and extension from the filepath to use as an NSString.
Your help is greatly appreciated!

There are methods on NSString that do this. Look at -[NSString pathExtension] and -[NSString lastPathComponent], both defined in NSPathUtilities.h.

to get the filename without extension, try out [NSString stringByDeletingPathExtension];

Try this, it works for me.
NSString *fileName = #"yourFileName.pdf";
NSString *ext = [fileName pathExtension];

i hope this will help you....
Str = [NSString stringWithFormat:#"%#",[openPanel URL]];
[txtBeowsFilePath setStringValue:Str];
Browesfilename=[Str lastPathComponent];

Related

What’s the correct way to create an NSURL from an NSString?

I’ve got an NSString that stores the path to a saved file:
NSString *filePath = [NSString stringWithFormat:
#"%#/someFolder/%#",
NSHomeDirectory(),
[NSString stringWithFormat:#"%#",[self.fileName stringByAppendingPathExtension:#"txt"]]];
And it’s OK — when I log it, I get:
/Users/username/someFolder/fileName.txt
So my next step is to make an NSURL object from this NSString. I did this:
NSURL *pathURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#", filePath]];
NSLog(#"URL = %#", pathURL);
but the response is:
URL = (null)
What’s wrong here? How can I do this correctly?
A path is not a valid URL by itself. You have to use this:
NSURL *pathURL = [NSURL fileURLWithPath:filePath];
And read the documentation. (And don’t overuse / abuse format strings.)

How to get the path/directory component from a NSURL?

I retrieved a NSURL from a NSSavePanel. I now have this NSURL which gives me the following:
file://localhost/Users/brett/Documents/asdf%20asdf.json
Now, it is easy for me to retrieve just the filename using something like the following:
[[[NSFileManager defaultManager] displayNameAtPath:pathAndFilename] stringByDeletingPathExtension]
This gives me just the localized filename, as expected: asdf%20asdf
So, how do I get the path, like so: file://localhost/Users/brett/Documents/
-[NSURL URLByDeletingLastPathComponent] is the simplest way to achieve this.
You could use NSString methods to work with file paths. For example,
NSString *directory = [[URL absoluteString] stringByDeletingLastPathComponent];
NSString *filename = [[URL absoluteString] lastPathComponent];
You could find other useful methods in Apple Docs: NSString Class Reference -> Working with Paths section
Directly from your NSSavePanel:
NSSavePanel *savePanel;
...
NSString *path = savePanel.directoryURL.path;

Problems with UIImagePNGRepresentation

I'm trying to use this line of code
[UIImagePNGRepresentation(image, 1.0) writeToFile:pngPath atomically:YES];
But obviously pngPath is undeclared. So I have to use stringByAppendingPathComponent.
I googled examples of this, and I found this
myPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: #"%#.plist", plistName] ];
[myPlistPath retain]
;
The problem is that I don't have a plist file, because I don't need one. How can I solve all these issues and writeToFile the UIImage image?
I don't fully understand your question, but to get the full path where your file should be saved, you could try this:
NSString *file = #"myfile.png";
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];
NSString *path = [directory stringByAppendingPathComponent: file];
and then:
[UIImagePNGRepresentation(image) writeToFile:path atomically:YES];

didReceiveDataOfLength: Problems in cocoa

Im trying to get the amont of data downloaded with this - (void)download:(NSURLDownload *)theDownload didReceiveDataOfLength:(NSUInteger)length code from apples website but I need to convert the length to a string. How might I do this? Thanks in advance
Just create a string if you need a string:
NSString *lengthString = [NSString stringWithFormat:#"%u", length];
Can't you do NSString* stringLength = [NSString stringWithFormat: #"%u", length];?

Use stringWithFormat: as a file path in cocoa

I'm having a problem with a cocoa application that takes the value of a text field, and writes it to a file. The file path is made using stringWithFormat: to combine 2 strings. For some reason it will not create the file and the console says nothing. Here is my code:
//Get the values of the text field
NSString *fileName = [fileNameTextField stringValue];
NSString *username = [usernameTextField stringValue];
//Use stringWithFormat: to create the file path
NSString *filePath = [NSString stringWithFormat:#"~/Library/Application Support/Test/%#.txt", fileName];
//Write the username to filePath
[username writeToFile:filePath atomically:YES];
Thanks for any help
The problem is that you have a tilde ~ in the path. ~ is expanded by the shell to the user's home directory, but this doesn't happen automatically in Cocoa. You want to use -[NSString stringByExpandingTildeInPath]. This should work:
NSString *fileName = [fileNameTextField stringValue];
NSString *username = [usernameTextField stringValue];
NSString *fileName = [fileName stringByAppendingPathExtension:#"txt"]; // Append ".txt" to filename
NSString *filePath = [[#"~/Library/Application Support/Test/" stringByExpandingTildeInPath] stringByAppendingPathComponent:fileName]; // Expand '~' to user's home directory, and then append filename
[username writeToFile:filePath atomically:YES];
Adding to mipadi's reply, it's better to use -[NSString stringByStandardizingPath] since it does more - and can clean up more problems - than resolving the tilde.

Resources