Results 1 to 6 of 6

Thread: qDir illegal characters in path name

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2019
    Posts
    23
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default qDir illegal characters in path name

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qDir illegal characters in path name

    Qt just uses the OS directory iteration APIs. Are you saying that something like this:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDir>
    3. #include <iostream>
    4.  
    5. int main(int argc, char** argv) {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QDir dir("/tmp/tt/a");
    9. dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
    10. dir.setSorting(QDir::Size | QDir::Reversed);
    11.  
    12. QFileInfoList list = dir.entryInfoList();
    13. std::cout << " Bytes Filename" << std::endl;
    14. for (int i = 0; i < list.size(); ++i) {
    15. QFileInfo fileInfo = list.at(i);
    16. std::cout << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10)
    17. .arg(fileInfo.fileName()));
    18. std::cout << std::endl;
    19. }
    20.  
    21. return 0;
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 
    does not list a file containing a single quote in its file name. Certainly does here on Linux:
    Qt Code:
    1. 13:38:09: Starting /tmp/tt/build-a-Desktop_Qt_6_1_1_GCC_64bit-Debug/a ...
    2. Bytes Filename
    3. 0 file_with_'quote
    4. 36 a.pro
    5. 671 main.cpp
    6. 34117 a.pro.user
    7. 13:38:09: /tmp/tt/build-a-Desktop_Qt_6_1_1_GCC_64bit-Debug/a exited with code 0
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    spike6479 (20th June 2021)

  4. #3
    Join Date
    Dec 2019
    Posts
    23
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qDir illegal characters in path name

    Thanks for the reply. I'm using windows 10, not LINUX. Here is the exact code I'm using:

    Qt Code:
    1. QDir dir(info.absolutePath());
    2. dir.setFilter(QDir::Files);
    3. QStringList filters;
    4. filters << info.completeBaseName()+QStringLiteral("*.*");
    5. dir.setNameFilters(filters);
    6. QList <QString> files = dir.entryList();
    To copy to clipboard, switch view to plain text mode 

    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*.*
    Last edited by spike6479; 20th June 2021 at 13:03.

  5. #4
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    552
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qDir illegal characters in path name

    This works perfectly fine for me:

    Qt Code:
    1. QCoreApplication a(argc, argv);
    2. QDir dir("D:\\tmp");
    3. dir.setFilter(QDir::Files);
    4. dir.setNameFilters({ QStringLiteral("*'*.*") });
    5. QList <QString> files = dir.entryList();
    6. for (const QString& file : files)
    7. qDebug() << file;
    To copy to clipboard, switch view to plain text mode 

    -> output:
    "bl'b.txt"

  6. The following user says thank you to ChristianEhrlicher for this useful post:

    spike6479 (20th June 2021)

  7. #5
    Join Date
    Dec 2019
    Posts
    23
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qDir illegal characters in path name

    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.

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qDir illegal characters in path name

    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.

  9. The following user says thank you to ChrisW67 for this useful post:

    spike6479 (21st June 2021)

Similar Threads

  1. Replies: 4
    Last Post: 20th January 2013, 11:01
  2. Replies: 3
    Last Post: 13th October 2011, 01:07
  3. QDir::entryList() get absolute path
    By Aji Enrico in forum Qt Programming
    Replies: 3
    Last Post: 23rd April 2011, 04:26
  4. Replies: 6
    Last Post: 22nd April 2010, 15:43
  5. QDir Path problem
    By ToddAtWSU in forum Qt Programming
    Replies: 3
    Last Post: 21st August 2008, 12:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.