PDA

View Full Version : QFile::exists() and QFileInfo::exists() and case sensitive file names



JPNaude
21st February 2011, 11:30
Hi

I need to check if a file exists in a folder, and that the file name actually matches the case of the search string. However on Windows QFile::exists() and/or QFileInfo::exists() does not take the case of the file name into account. I have not tested it but I suspect on Unix exists() will take the case of the file name into account.

Is there any way to use the exists() function as on windows so that they take the case into account?

I can probably do a manual check by looking at the file names in the directory using QDir, but I'm hoping there is an easier way.

Thanks,
Jaco

mcosta
21st February 2011, 14:28
Windows filenames are not case sensitive.

I think you cannot do this check because in Windows filename.txt and FileName.TXT are the same file

JPNaude
21st February 2011, 14:52
Yip I'm aware of that. I guess exists() uses the file system's case sensitivity. I need to check it on Windows since I generate scripts that's run on a Linux machine. The easiest way I could come up with its:



bool FirmwareCore::FileModel::caseSensitiveExists() const {
// Only do it if it says it exists;
// exists() is a normal exists on QFileInfo
if (exists()) {
// Check the directory where the file is located:
// fileInfo() below is a normal QFileInfo object.
QDir parent_dir = fileInfo().dir();
return parent_dir.entryList().contains(fileName(),Qt::Cas eSensitive);
} else
return false;
}