5

I am making a small program to help with file reading.

i have a for loop to take in commands from the command line:

 for (i = 1; argc > i; i++)
            {
                QString path = QDir::currentPath()+ "/" + QString(argv[i]);
                QDir dir(path);
                fileSearch(dir);
            }

from there I call another method where I look into each file/foler and get the size and whether its a file or folder.

void fileSearch(QDir dir)
{
        foreach(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::AllDirs ))
            {
                if (info.isFile())
                {
                    qDebug() << info.path() << "is a file! its size is:" << info.size() << endl;
                }
                if (info.isDir())
                {
                    qDebug() << info.path() << "is a directory! its size is:" << info.size() << endl;
                    fileSearch(info.filePath());
                }
            }

instead of reading the whole entire path, I want it to read just the relative path. So instead of it reading:

home/john/desktop/project/currentproject/checkdirectory is a directory! its size is: 4096
home/john/desktop/project/currentproject/checkdirectory/test.txt is a file! its size is: 4

I want it to read:

checkdirectory/ is a directory! its size is: 4096
checkdirectory/test.txt is a file! its size is: 4
user3084848
  • 65
  • 1
  • 9

1 Answers1

4
QString QDir::relativeFilePath(const QString & fileName);

should return the relative file path.

Sebastian Lange
  • 3,879
  • 1
  • 19
  • 38
  • So in my case it would read `qDebug() << QString QDir::relativeFilePath(const QString & info);` right? – user3084848 Feb 17 '14 at 06:52
  • So how would that look in my case? I saw that exact line online but really couldnt make sense of it. It wont compile like the way I suggested above that so I guess its wrong :/ – user3084848 Feb 17 '14 at 06:58
  • 2
    ``QDir infoDir(QDir::currentPath()).relativeFilePath(info.path());`` Something like this. QDir::currentPath() returns your working directory from where you called your binary. So from there the relative Path to info.path() is what you want i guess. – Sebastian Lange Feb 17 '14 at 06:58