Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

how can I have information about a dir



  • is there a function to know information about a dir like dimension and date of creation?


  • Moderators

    You can use QFileInfo (yes I know it has "file" in name, but it works for directories, too) to get creation date. I have no idea what you mean by "dimension" - directory size? If so, then you need to manually count all the files or use platform-specific APIs to get the size. QFileInfo will only return you the size of directory descriptor.



  • @vale88

    Have a look at this or this to get dimension (amount of subDirs). You can sort Dirs in a list by modified time with the filter QDir::Time... but dont know how to print out the value from specific QDir.

    Hope it helps :)



  • @sierdzio I wrote:

      QFileInfo fi(path_definitivo);
    
    
                double dimensione = fi.size();
                QString dim_bytes = QString::number(dimensione);
    

    "fi" is a dir, but dimensione is 0, but it'isn't possible because inside dir there is a file that has a size of 1kb



  • @vale88

    QFileInfo works on Dirs too, but not in general.

    Returns the file size in bytes. If the file does not exist or cannot be fetched, 0 is returned.

    It seems XY.size() does not work on directories

    If you need a reliable solution, you have to iterate through all sub-directories and sum all file sizes



  • No OS (well, Linux or Windows at least) offers a "size" attribute on directories which gives the size of all its contents, or for that matter the number of children, whether you use QFileInfo or not. It would be too slow/hard to maintain updated. If you want to know either of those you have to walk all the children (recursively if you want to know about sub-content) and count for yourself.


  • Moderators

    @vale88 said in how can I have information about a dir:

    "fi" is a dir, but dimensione is 0, but it'isn't possible because inside dir there is a file that has a size of 1kb

    As I already wrote:

    you need to manually count all the files or use platform-specific APIs to get the size. QFileInfo will only return you the size of directory descriptor.


Log in to reply