Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go from absolute path to relative path?

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
like image 470
user3084848 Avatar asked Feb 17 '14 06:02

user3084848


People also ask

How do I find the relative path of a folder?

var relativePath = Path. GetRelativePath( @"C:\Program Files\Dummy Folder\MyProgram", @"C:\Program Files\Dummy Folder\MyProgram\Data\datafile1. dat"); In the above example, the relativePath variable is equal to Data\datafile1.

How do you make a path relative?

Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.


1 Answers

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

should return the relative file path.

like image 142
Sebastian Lange Avatar answered Oct 11 '22 15:10

Sebastian Lange