Results 1 to 3 of 3

Thread: Reading files from a perticular folder

  1. #1
    Join Date
    Nov 2008
    Location
    Bangalore, India
    Posts
    21
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows Symbian S60

    Default Reading files from a perticular folder

    Hi all.

    I have an application which takes a file name as the input.
    Currently I am hard coding the file name in the constructor.

    But, I want to read all the files from a perticular folder one by one.
    How to do that ??
    Using QFileDialog class i can only select a perticular file (or files) from a list of file
    Thank you.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Reading files from a perticular folder

    so, what is the problem? store files names in a list and then implement a loop in which you can read data from each file.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    May 2007
    Location
    Germany
    Posts
    89
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Reading files from a perticular folder

    Hello my friend,

    look at this little snippet from me perhaps it will help you.

    Qt Code:
    1. QString somepath = "C:/yourpathtofolder/"
    2. QDir somefolder = QDir(somepath);
    3. somefolder.setFilter(QDir::Files | QDir::Dirs | QDir::NoSymLinks);
    4. somefolder.setSorting(QDir::Size | QDir::Reversed);
    5.  
    6.  
    7. QStringList filenames = somefolder.entryList(QDir::NoDotAndDotDot | QDir::AllDirs, QDir::Name);
    8. QStringList fileListing;
    9.  
    10. for (int i(0); i < filenames.count(); i++)
    11. {
    12. QString str = filenames.at(i);
    13. //you can also ignore the if statement for filtering your Filenames depends on your filtertext
    14. QDir dir(somepath+filenames.at(i));
    15. foreach ( QString file, dir.entryList( QDir::Files))
    16. {
    17. if(file.contains("YourFilterText", Qt::CaseInsensitive))
    18. fileListing << QFileInfo( dir, file).absoluteFilePath() << "\n";
    19. }
    20. }
    21. //after you fill up your list and do want you want to do my friend ;o))
    22. foreach (QString str, fileListing)
    23. {
    24. QMessageBox::information(this, tr("My Files in Folder"),
    25. tr("File: %1 "
    26. " ")
    27. .arg(str));
    28. }
    29. fileListing.clear();
    To copy to clipboard, switch view to plain text mode 

    I hope I don´t make any mistake, please proof it before using ;o))

    bye

Similar Threads

  1. Access violation when reading big text files
    By robertson1 in forum General Programming
    Replies: 0
    Last Post: 18th September 2008, 06:59
  2. Avoiding unnecessary files and folder in Impelementation
    By salmanmanekia in forum Qt Programming
    Replies: 2
    Last Post: 30th May 2008, 14:54
  3. Reading files in C++
    By maverick_pol in forum General Programming
    Replies: 2
    Last Post: 15th May 2008, 08:53
  4. problem with reading text files
    By Axsis in forum Newbie
    Replies: 1
    Last Post: 25th April 2008, 12:29
  5. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53

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.