PDA

View Full Version : how can calculate total file size of all .rtf files in folder?




newformac
Jun 3, 2011, 04:53 AM
Hi all,

i have an folder with some subfolders each folder contains different type of files like .rtf,.html,.doc etc.

i want to calculate total file size of all .rtf files in folder with its subfolder?


please provide me help and guidance for this.

thanks in advance.



robbieduncan
Jun 3, 2011, 05:00 AM
Since you already know you should be using NSFileManager I am surprised you have not already looked at the documentation (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html) for that class, specifically the contentsOfDirectoryAtPath:error: (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/contentsOfDirectoryAtPath:error:) method.

That will give you all files in the directory, simply loop over them, check if they match the file type you want and add up the sizes.

newformac
Jun 3, 2011, 05:40 AM
Since you already know you should be using NSFileManager I am surprised you have not already looked at the documentation (http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html) for that class, specifically the contentsOfDirectoryAtPath:error: (http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/contentsOfDirectoryAtPath:error:) method.

That will give you all files in the directory, simply loop over them, check if they match the file type you want and add up the sizes.


here in the located folder two sub folder and each subfolder have 4 files.

but here only 2 objects are getting by this, here alert message display name of sub folder only.

robbieduncan
Jun 3, 2011, 05:51 AM
but here only 2 objects are getting by this, here alert message display name of sub folder only.

Yes. That is the expected result. If you want to traverse subdirectories that is up to you: you can check each file in the directory listing to see if it is a directory and if it is list that and so on. I would suggest you are looking to write a recursive method...

newformac
Jun 3, 2011, 05:55 AM
have u any example for this?

robbieduncan
Jun 3, 2011, 05:57 AM
have u any example for this?

For what? The NSFileManager documentation is very clear and it's obvious how to check if something is a directory. And recursive functions/methods are basic programming. If you don't know how to write one stop coding and go and learn the basics. Oh and it's you, not u.

newformac
Jun 3, 2011, 06:37 AM
now i have done ,to find all files from dir and sub dir.

how can i identify the file extension for .rtf file to calculate file size.

robbieduncan
Jun 3, 2011, 06:49 AM
Somewhat. It's not clear how you expect this to get the results back to you. I'd expect the function to either return the size of the items it's found or an array of items.

As for "finding the extension" what have you tried? Which documents have you looked in? The documentation for which class? You need to learn to look these things up for yourself...

newformac
Jun 3, 2011, 07:25 AM
thanks now i done it.

gnasher729
Jun 3, 2011, 08:57 AM
have u any example for this?

You don't need an example. Everything in a directory is either another directory, or an rtf file, or something else. So what would you do in each case?

I have the impression you don't want an example, you want someone to write code for you. My rate is 100 per hour. I bet robbieduncan will charge about the same.

mrmma
Jun 3, 2011, 11:43 AM
Not knowing exactly what you want to do and this requires a bit of refinement , you could do a variant of
find . -name "*.rtf" -print > .rtffiles
du -chs`cat .rtffiles` > sizes.rtffiles
grep total sizes.rtffiles

Doing a bit of testing, this requires a bit of tweaking if you have spaces in your file name. You can get fancy and push the three commands together, but it's more transparent this way.

newformac
Jun 9, 2011, 11:37 PM
thanks its done now.