Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newformac

macrumors regular
Original poster
May 24, 2011
107
0
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.
 
Last edited by a moderator:

newformac

macrumors regular
Original poster
May 24, 2011
107
0
Since you already know you should be using NSFileManager I am surprised you have not already looked at the documentation for that class, specifically the 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.
 
Last edited by a moderator:

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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...
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

macrumors regular
Original poster
May 24, 2011
107
0
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.
 
Last edited by a moderator:

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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...
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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

macrumors member
Dec 27, 2006
60
0
Missouri
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.