QDirIterator - Skip Folders and its subfolders - c++

How can you skip folders with the QDirIterator?
I've tried it with:
QString nameFilter = "*.h";
QDirIterator dirIterator(folder, nameFilter, QDir::Files, QDirIterator::Subdirectories);
QString str("folder");
QStringList filenames;
while (dirIterator.hasNext())
{
if(dirIterator.next() == str) continue;
filenames.append(dirIterator.next());
}
but it only ignores the specific folder but not its subdirectories.
Any idea?

Bellow instruction working 100 percent :
This method QString QDirIterator::next() Advances the iterator to the next entry, and returns the "file path" of this new entry. If hasNext() returns false, this function does nothing, and returns a null QString.
If QDirIterator found a file with specific filter (based on const QStringList &nameFilters parameter) then QDirIterator::next() returns "file-path" and you cannot compare the whole "file-path" with "skiped-folder" to iterate!
Because of this i had to write a function as a directory parser as bellow :
QStringList Widget::getCurrDirsOfFile(const QFileInfo &file_info, const QString &default_folder)
{
QString file_path = file_info.filePath();
QString file_name = file_info.fileName();
file_path.truncate(file_path.lastIndexOf("/" + file_name));
file_path = file_path.mid(file_path.lastIndexOf("/") + 1);
return file_path;
}
This function get "file-path" and return last "folder-name's" of it like bellow :
input : /home/msi/Desktop/123/untitled/123/widget.h
output(list) : 123, 123
We have QString skiped_dir("untitled"); as skiped folders!
In this state when you got something excluding 123 "folder-name's", you can append that file to QStringList filenames :
if(folder_list.indexOf(QRegExp(skiped_dir)) == -1)
filenames.append(it.filePath());
So try this (notice in comments) :
void Widget::btn_iterate_clicked()
{
// folder to iterate
QString folder("/home/msi/Desktop/");
// folder-name you want to skip
QString skiped_dir("untitled");
// file-names which are match with .h filter stored in filenames list
QStringList filenames;
// this list used in QStringList getCurrDirsOfFile() method
QStringList folder_list;
// iterator
QDirIterator it(folder, QStringList() << "*.h" , QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) // if next object exist for iterate
{
// we have the file with .h extension
it.next();
// get previous folders that exist in filepath address of it.fileInfo()
folder_list = getCurrDirsOfFile(it.fileInfo(), folder);
// if folder_list contained skiped folder then reject that or appent it
if(folder_list.indexOf(QRegExp(skiped_dir)) == -1)
filenames.append(it.filePath());
}
for(int i = 0;i < filenames.count();i++)
{
QMessageBox::information(this, "", filenames.at(i));
}
}
Suppose we have bellow tree (directories) :
This program just shows :
/home/msi/Desktop/build-untitled-Desktop_Qt_5_0_2_GCC_64bit-Debug/ui_widget.h
/home/msi/Desktop/1/1.h

I know this is a bit old, but your original code was close...there's two problems. First, the iterator covers every entry and what it returns is a full path and filename, not just the entry name within the current directory. Checking for an exact match is the problem rather than for containment. The second issue is that you have two calls to "next" without a call to "hasNext" between them, so it's possible you'll have a crash.
while (dirIterator.hasNext())
{
QString fname = dirIterator.next ();
if (fname.contains (str)) continue;
filenames.append (fname);
}
Note that you may need to be more specific with the value of "str". Any filename could contain the word "folder", so you'll probably want to check for str.endsWith ("/folder") as well as str.contains ("/folder/").
If you inspect the values returned from "next", you'll better see how to compare what you're excluding to the current entry, so just tweak the string comparison to get what you want.

Related

Execute Command with QProcess and Store result in QStringList

I have written the following function which executes a windows enumeration command for getting shared folders and store the result in a QString.
QProcess p;
p.setProgram("wmic");
p.setArguments({"share", "get", "name"});
p.start();
if (!p.waitForFinished()) {
return;
}
const QString output = p.readAllStandardOutput();
qDebug () << output;
const QString error = p.readAllStandardError();
if (!error.isEmpty()) {
qDebug () << error;
}
But the output has a lot of delimiters like "\n\r" ... so I wanted to strip all of those delimiters from my string output. In the next step, you consider we will have a result like the following one:
C$
D$
E$
IPC$
So I wanted to save these names in a QStringList, or something like a list which I can append those names in combo widget independently. How can I do that?
You could just use qstring split:
QStringList list = output.split("\n", QString::SkipEmptyParts);
If you need a more "intelligent" split that you can pass in a regex:
list = output.split(QRegExp("...some regex..."));
The skip empty parts just "removes"/ignores any values that would be empty - I don't think you need that in this case

How do I get the value of text inside of the file using Qt?

The data of my file.txt is as below:
Student_ID=0001
Student_Name=joseph
Student_GradeLevel=2
How do I get the value, let say I want to get the Student_ID using Qt.
Thanks.
Take a look at this function, it can be used to find any value you want in your input file, where all lines are in the format you've posted above (key=value). If the key is not found, it returns an empty QString() object.
QString findValueInFile(QString key, QString filename) {
QFile file(filename);
if(file.open(QIODevice::ReadOnly)) {
QTextStream txtStr(&file);
QStringList fileContent = txtStr.readAll().split('\n');
for(auto &&line : fileContent) {
if(line.contains(key)) return line.split(QChar('='))[1];
}
file.close();
}
return QString(); // not found
}
Now you call it somewhere, e.g.:
qDebug() << findValueInFile("Student_ID", "file.txt");
qDebug() << findValueInFile("Student_Name", "file.txt");
This function can be easily modified if you replace your = sign with other delimiter e.g. => or sth else. However for key=value format there is a special QSettings class (mentioned by sebastian) that can allow you to read those values even easier:
QSettings file("file.txt", QSettings::IniFormat);
qDebug() << file.value("Student_Name").toString(); // et voila!
You can probably also use QSettings, as they are able to read ini files.
There are some caveats though regarding backslashes which might be important to you (though they aren't for the example you posted): http://doc.qt.io/qt-4.8/qsettings.html#Format-enum
QSettings iniFile("myfile.txt", QSettings::IniFormat);
// now get the values by their key
auto studentId = iniFile.value("Student_ID").toString().toInt();
I'm more of a PyQt user, so: apologies if I got some C++ specifics wrong...

Getting full path from QListWidget

I have 2 listwidgets, lets call them listwidgetinput and listwidgetoutput. I have alot of files(only file name) on listwidgetinput. And i trim the file name before adding it to listwidgetinput like this it.fileName(). and i transfer the selected files to listdigetoutput like:
QList <QListWidgetItem*> items=ui->listWidgetinput->selectedItems();
for(int j=0;j<items.count();j++)
{
list= items.at(j)->text();
ui->listWidgetOutput->insertItem(j,list);
After i transfer the file can i get the path for all the files?. If Yes, how?
edit: code where whole path is available.
QString Dir, Type;
QStringList Files;
Qlistwidget wid
if (index==0)
{
Dir.append(C:\desktop....);
type.append(".txt")
wid = ui->listwidgetinput_txt;
}
if (index ==1)
{
Dir.append(C:\desktop....);
type.append(".doc")
wid = ui->listwidgetinput_doc
}
QDirIterator it(Dir, QStringList() << Type, QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext())
{
it.next();
Files.append(it.fileName());
}
wid->additems(Files);
Use QListWidgetItem::setData() to pass additional "invisible" properties like the full path when creating the item:
auto item = new QListWidgetItem;
item->setText(fileInfo.fileName());
item->setData(Qt::UserRole, fileInfo.absoluteFilePath());
...
Later you can retrieve it via QListWidgetItem::data():
const auto fullPath = item->data(Qt::UserRole).toString();

Removing extension of a file name in Qt

I'm using Qt to get a file name from the user:
QString fileName = QFileDialog::getOpenFileName(this,tr("Select an image file"),"d:\\",tr("Image files(*.tiff *.tif )"));
It works, but I need the file name without its extension, is it possible in Qt??
whenn I try :
QString f = QFileInfo(fileName).fileName();
f is like "filename.tif", but I want it to be "filename".
QFileInfo has two functions for this:
QString QFileInfo::completeBaseName () const
Returns file name with shortest extension removed (file.tar.gz -> file.tar)
QString QFileInfo::baseName () const
Returns file name with longest extension removed (file.tar.gz -> file)
To cope with filenames containing multiple dots, look for the last one and take the substring until that one.
int lastPoint = fileName.lastIndexOf(".");
QString fileNameNoExt = fileName.left(lastPoint);
Of course this can (and should) be written as a helper function for reuse:
inline QString withoutExtension(const QString & fileName) {
return fileName.left(fileName.lastIndexOf("."));
}
You can split fileName with "." as separator like this:
QString croped_fileName=fileName.split(".",QString::SkipEmptyParts).at(0);
or use section function of QString to take the first part before "." like this:
QString croped_fileName=fileName.section(".",0,0);
You can use QString::split and use the . as the place where to split it.
QStringList list1 = str.split(".");
That will return a QStringList with {"filename", "extenstion"}. Now you can get your filename without the extension.
To get absolute path without extension for QFileInfo fileInfo("/a/path/to/foo.tar.gz") you can use:
QDir(file_info.absolutePath()).filePath(file_info.baseName());
to get "/a/path/to/foo" or
QDir(file_info.absolutePath()).filePath(file_info.completeBaseName());
to get "/a/path/to/foo.tar"

Delete all files in a directory

I need to delete all files in a directory using Qt.
All of the files in the directory will have the extension ".txt".
I don't want to delete the directory itself.
Does anyone know how I can do this? I've looked at QDir but am having no luck.
Bjorns Answer tweeked to not loop forever
QString path = "whatever";
QDir dir(path);
dir.setNameFilters(QStringList() << "*.*");
dir.setFilter(QDir::Files);
foreach(QString dirFile, dir.entryList())
{
dir.remove(dirFile);
}
Ignoring the txt extension filtering... Here's a way to delete everything in the folder, including non-empty sub directories:
In QT5, you can use removeRecursively() on dirs. Unfortunately, that removes the whole directory - rather than just emptying it. Here is basic a function to just clear a directory's contents.
void clearDir( const QString path )
{
QDir dir( path );
dir.setFilter( QDir::NoDotAndDotDot | QDir::Files );
foreach( QString dirItem, dir.entryList() )
dir.remove( dirItem );
dir.setFilter( QDir::NoDotAndDotDot | QDir::Dirs );
foreach( QString dirItem, dir.entryList() )
{
QDir subDir( dir.absoluteFilePath( dirItem ) );
subDir.removeRecursively();
}
}
Alternatively, you could use removeRecursively() on the directory you want to clear (which would remove it altogether). Then, recreate it with the same name after that... The effect would be the same, but with fewer lines of code. This more verbose function, however, provides more potential for detailed exception handling to be added if desired, e.g. detecting access violations on specific files / folders...
Call QDir::entryList(QDir::Files) to get a list of all the files in the directory, and then for each fileName that ends in ".txt" call QDir::remove(fileName) to delete the file.
You started in a good way, look at entryList and of course pass the namefilter you want.
To improve on #user3191791's answer (which removes all files and directories), this answer:
Modernises the code with a range-based for loop
Provides optional error checking
The code:
struct FileOperationResult
{
bool success;
QString errorMessage;
};
FileOperationResult removeDirContents(const QString &dirPath)
{
QDir dir(dirPath);
dir.setFilter(QDir::NoDotAndDotDot | QDir::Files);
const QStringList files = dir.entryList();
for (const QString &fileName : files) {
if (!dir.remove(fileName)) {
const QString failureMessage = QString::fromUtf8(
"Failed to remove file %1 from %2").arg(fileName, dirPath);
return { false, failureMessage };
}
}
dir.setFilter(QDir::NoDotAndDotDot | QDir::Dirs);
const QStringList dirs = dir.entryList();
for (const QString &dirName : dirs) {
QDir subDir(dir.absoluteFilePath(dirName));
if (!subDir.removeRecursively()) {
const QString failureMessage = QString::fromUtf8(
"Failed to recursively remove directory %1 from %2").arg(dirName, dirPath);
return { false, failureMessage };
}
}
return { true, QString() };
}
Usage:
const FileOperationResult removeResult = removeDirContents(path);
if (!removeResult.success)
qWarning() << removeResult.errorMessage;
This is how I would do it:
QString path = "name-of-directory";
QDir dir(path);
dir.setNameFilters(QStringList() << "*.txt");
dir.setFilters(QDir::Files);
while(dir.entryList().size() > 0){
dir.remove(dir.entryList().first());
}
Other variant of rreeves's code:
QDir dir("/path/to/file");
dir.setNameFilters(QStringList() << "*.*");
dir.setFilter(QDir::Files);
for(const QString &dirFile: dir.entryList()) {
dir.remove(dirFile);
}
You can achieve this without using Qt: to do so, opendir, readdir, unlink, and even rmdir will be your friends. It's easy to use, just browse the man pages ;).