PDA

View Full Version : qDir illegal characters in path name



spike6479
18th June 2021, 16:49
I am using QT V6.1.1 on windows 10. I noticed that QDir will not find file with a single quote in a filter, even though windows allows this. Is there a list of illegal characters qt doesn't allow in path names?

Thanks

ChrisW67
20th June 2021, 04:39
Qt just uses the OS directory iteration APIs. Are you saying that something like this:


#include <QCoreApplication>
#include <QDir>
#include <iostream>

int main(int argc, char** argv) {
QCoreApplication app(argc, argv);

QDir dir("/tmp/tt/a");
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
dir.setSorting(QDir::Size | QDir::Reversed);

QFileInfoList list = dir.entryInfoList();
std::cout << " Bytes Filename" << std::endl;
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10)
.arg(fileInfo.fileName()));
std::cout << std::endl;
}

return 0;

}

does not list a file containing a single quote in its file name. Certainly does here on Linux:


13:38:09: Starting /tmp/tt/build-a-Desktop_Qt_6_1_1_GCC_64bit-Debug/a ...
Bytes Filename
0 file_with_'quote
36 a.pro
671 main.cpp
34117 a.pro.user
13:38:09: /tmp/tt/build-a-Desktop_Qt_6_1_1_GCC_64bit-Debug/a exited with code 0

spike6479
20th June 2021, 12:16
Thanks for the reply. I'm using windows 10, not LINUX. Here is the exact code I'm using:



QDir dir(info.absolutePath());
dir.setFilter(QDir::Files);
QStringList filters;
filters << info.completeBaseName()+QStringLiteral("*.*");
dir.setNameFilters(filters);
QList <QString> files = dir.entryList();


When this code is run with a file name that contains a ', the files list is empty. It works as expected when the ' is removed from the file name.
The completebasename contains a ;, so what I searching for is: abs'xyz*.*

ChristianEhrlicher
20th June 2021, 13:42
This works perfectly fine for me:



QCoreApplication a(argc, argv);
QDir dir("D:\\tmp");
dir.setFilter(QDir::Files);
dir.setNameFilters({ QStringLiteral("*'*.*") });
QList <QString> files = dir.entryList();
for (const QString& file : files)
qDebug() << file;


-> output:
"bl'b.txt"

spike6479
20th June 2021, 14:48
I guess I need to dig deeper. I created a file in a folder with a single quote and a file name with a single quote and it worked fine. When it failed before I removed the single quotes and the problem went away, so I concluded that it was the single quote that caused the problem. I'm very sorry to have wasted your time.

ChrisW67
21st June 2021, 03:47
No harm, no foul. It is precisely these little oddities that often escape testing. There was always the possibility that the character you thought was a simple apostrophe was in fact a "smart" quote or that there were non-printables embedded in the file name causing something to break the Windows file globbing.