How to open a directory and check this content?



  • Hi,

    I would like to open a directory and scanning that empty or not.

    @QString directory = QFileDialog::getExistingDirectory(this, tr("Browse folder"), QDir::homePath (), QFileDialog::DontResolveSymlinks);

    if (!QFileInfo(directory).size ())
    {
        ui->sfTextEdit->setText ("The selected folder is empty!");
    }
    else
    {
        ui->sfTextEdit->setText ("The selected folder is not empty!");
    }@
    

    I wrote this script, but didn't work.



  • QFileInfo(...).size() works only for files, try QDir(...).count().



  • I dunno how to :(

    I would like to select a directory and write out the content in a list.
    Example:
    I selected D:/Music/Adele
    And the app will write that:

    Adele - Someone like you -> 5mb
    Adele - somename -> 6mb
    .....



  • Look at this function:

    @void QThAnalyze::SideList(QString Path) {
    QVector<FileItem> *QVSideList= new QVector<FileItem>();
    QDir QDPath= QDir(Path);
    QDPath.setSorting(QDir::Name);
    QStringList QSLFiles= QDPath.entryList(QDir::AllEntries | QDir::NoDotAndDotDot | QDir::Hidden);
    if (QSLFiles.size()== 0) emit SendEmptyDirs(Side, Path);
    else {
    for (int count= 0; count< QSLFiles.size(); count++) {
    FileItem fileitem;
    fileitem.Path= Path+ QDir::separator();
    if (fileitem.Path.startsWith(InitialPath)) fileitem.Path= fileitem.Path.right(fileitem.Path.length()- InitialPath.length());
    fileitem.File= QSLFiles[count];
    QFileInfo QFIPath(Path+ QDir::separator()+ QSLFiles[count]);
    struct stat FileAttributes;
    stat(QString(Path+ QDir::separator()+ QSLFiles[count]).toStdString().c_str(), &FileAttributes);
    //qDebug() << QString(Path+ QDir::separator()+ QSLFiles[count]).toStdString().c_str(); <<<---------
    if (QFIPath.isDir()) {
    fileitem.IsDirectory= true;
    Directories++;
    QVSideList->append(fileitem);
    if (DoStart) emit SendFiles(Side, Files, Directories, QVSideList);
    QVSideList->clear();
    if (DoStart && QFIPath.isDir() &! QFIPath.isSymLink()) SideList(Path+ QDir::separator()+ QSLFiles[count]);
    } else {
    fileitem.IsDirectory= false;
    fileitem.Size= QFIPath.size();
    Files++;
    fileitem.LastAccess= FileAttributes.st_atime;
    fileitem.LastModified= FileAttributes.st_mtime;
    QVSideList->append(fileitem);
    }
    }
    if (DoStart) emit SendFiles(Side, Files, Directories, QVSideList);
    }
    }@

    How you can see the "QDir QDPath= QDir(Path);" contains the content of directory. "QFileInfo" and "stat" function contain the attributes.
    Look at full source here: "Your text to link here...":http://kde-apps.org/content/show.php/?content=149622
    file qthanalyze.cpp


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.