1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

how can calculate total file size of all .rtf files in folder?

Discussion in 'Mac Programming' started by newformac, Jun 3, 2011.

  1. newformac, Jun 3, 2011
    Last edited by a moderator: Jun 3, 2011

    macrumors regular

    #1
    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.
     
  2. Moderator

    robbieduncan

    Staff Member

    #2
    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.
     
  3. newformac, Jun 3, 2011
    Last edited by a moderator: Jun 3, 2011

    macrumors regular

    #3

    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.
     
  4. Moderator

    robbieduncan

    Staff Member

    #4
    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...
     
  5. macrumors regular

    #5
    have u any example for this?
     
  6. Moderator

    robbieduncan

    Staff Member

    #6
    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.
     
  7. newformac, Jun 3, 2011
    Last edited by a moderator: Jun 3, 2011

    macrumors regular

    #7
    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.
     
  8. Moderator

    robbieduncan

    Staff Member

    #8
    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...
     
  9. newformac, Jun 3, 2011
    Last edited by a moderator: Jun 3, 2011

    macrumors regular

    #9
    thanks now i done it.
     
  10. macrumors G5

    gnasher729

    #10
    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.
     
  11. macrumors member

    #11
    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.
     
  12. macrumors regular

    #12
    thanks its done now.
     

Share This Page