Delete all files in a directory - c++

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 ;).

Related

Zip directory recursion

I am working on creating zip archive using old Qt - ZipWriter class. The problem is when I want to add the directory. The default Qt code for addDirectory method - d->addEntry(ZipWriterPrivate::Directory, archDirName, QByteArray());. It does not add any content, only the empty directory. So, I have improved it to add the directories and content as well.
My code:
QList<QString> dirs;
int recursion = 0;
void ZipWriter::addDirectory(const QString &dirPath)
{
QDir archDir(dirPath);
archDir.setFilter(QDir::AllEntries | QDir::NoDotAndDotDot);
dirs << addDirSeparator(archDir.dirName());
if (archDir.exists()) {
QString archDirName = "";
if (recursion > 0) {
for (int i = recursion; i < dirs.count(); i++) {
archDirName = dirs.first().append(dirs.at(i));
}
} else {
archDirName = dirs.at(recursion);
}
if (!archDir.isEmpty()) {
const QStringList archFileList = archDir.entryList();
if (archFileList.count() > 0) {
for (QString archFile : archFileList) {
QFileInfo archFileInfo(QDir::toNativeSeparators(QString("%1/%2").arg(archDir.absolutePath(), archFile)));
if (archFileInfo.isDir()) {
recursion++;
addDirectory(archFileInfo.absoluteFilePath());
} else {
QFile zipFile(archFileInfo.absoluteFilePath());
zipFile.open(QIODevice::ReadOnly);
addFile(QString("%1%2").arg(archDirName, archFile), zipFile.readAll());
zipFile.close();
}
}
}
} else {
d->addEntry(ZipWriterPrivate::Directory, archDirName, QByteArray());
}
}
}
Now, it adds the directory and content recursively but it has issue when directory is on the same level, it appends it to the end. I think, I must use the STL container to keep track of the directory for example QMap but the question is how to get the current directory level? Any ideas? Thank you.
Updated: 01.05.2022
I have change my code to this:
void ZipWriter::addDirectory(const QString &dirPath)
{
QDirIterator dirIt(dirPath, QDir::AllEntries | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
while (dirIt.hasNext()) {
QString archDirPath = dirIt.next();
QFile zipFile(archDirPath);
zipFile.open(QIODevice::ReadOnly);
if (!dirIt.fileInfo().isDir()) {
addFile(archDirPath, zipFile.readAll());
}
zipFile.close();
}
}
It adds everything recursively and in correct order but I have another issue. Now, it adds the full path to the archive. For example, I want to add this folder and it's content to the archive: 22610.1_amd64_en-us_professional_00fb7ba0_convert.
In the archive I get: C:\Users\userProfile\Downloads\22610.1_amd64_en-us_professional_00fb7ba0_convert. Any ideas how to make this relative path or trim it? Thank you.
You can use QDirIterator to recursively iterate over directory and subdirectories, and I believe you dont need to add nonempty directories at all, just files will be fine.
And why would you use stl container in qt, please use qt containers.

QDirIterator - Skip Folders and its subfolders

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.

C++ Transfer Files From Qt to External USB Drive

I am new in Qt and I need help in transferring all files from a specific path of the local machine to an external USB Drive.
Copying a single file
You can use QFile::copy.
QFile::copy(srcPath, dstPath);
Note: this function doesn't overwrite files, so you must delete previous files if they exist:
if (QFile::exist(dstPath)) QFile::remove(dstPath);
If you need to show an user interface to get the source and destination paths, you can use QFileDialog's methods to do that. Example:
bool copyFiles() {
const QString srcPath = QFileDialog::getOpenFileName(this, "Source file", "",
"All files (*.*)");
if (srcPath.isNull()) return false; // QFileDialog dialogs return null if user canceled
const QString dstPath = QFileDialog::getSaveFileName(this, "Destination file", "",
"All files (*.*)"); // it asks the user for overwriting existing files
if (dstPath.isNull()) return false;
if (QFile::exist(dstPath))
if (!QFile::remove(dstPath)) return false; // couldn't delete file
// probably write-protected or insufficient privileges
return QFile::copy(srcPath, dstPath);
}
Copying the whole content of a directory
I'm extending the answer to the case srcPath is a directory. It must be done manually and recursively. Here is the code to do it, without error checking for simplicity. You must be in charge of choosing the right method (take a look at QFileInfo::isFile for some ideas.
void recursiveCopy(const QString& srcPath, const QString& dstPath) {
QDir().mkpath(dstPath); // be sure path exists
const QDir srcDir(srcPath);
Q_FOREACH (const auto& dirName, srcDir.entryList(QStringList(), QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)) {
recursiveCopy(srcPath + "/" + dirName, dstPath + "/" + dirName);
}
Q_FOREACH (const auto& fileName, srcDir.entryList(QStringList(), QDir::Files, QDir::Name)) {
QFile::copy(srcPath + "/" + fileName, dstPath + "/" + fileName);
}
}
If you need to ask for the directory, you can use QFileDialog::getExistingDirectory.
Final remarks
Both methods assume srcPath exists. If you used the QFileDialog methods it is highly probable that it exists (highly probable because it is not an atomic operation and the directory or file may be deleted or renamed between the dialog and the copy operation, but this is a different issue).
I have solved the problem with the QStorageInfo::mountedVolumes() which return the list of the devices that are connected to the Machine. But all of them won't have a name except the Pendrive or HDD. So (!(storage.name()).isEmpty())) it will return the path to only those devices.
QString location;
QString path1= "/Report/1.txt";
QString locationoffolder="/Report";
foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) {
if (storage.isValid() && storage.isReady() && (!(storage.name()).isEmpty())) {
if (!storage.isReadOnly()) {
qDebug() << "path:" << storage.rootPath();
//WILL CREATE A FILE IN A BUILD FOLDER
location = storage.rootPath();
QString srcPath = "writable.txt";
//PATH OF THE FOLDER IN PENDRIVE
QString destPath = location+path1;
QString folderdir = location+locationoffolder;
//IF FOLDER IS NOT IN PENDRIVE THEN IT WILL CREATE A FOLDER NAME REPORT
QDir dir(folderdir);
if(!dir.exists()){
dir.mkpath(".");
}
qDebug() << "Usbpath:" <<destPath;
if (QFile::exists(destPath)) QFile::remove(destPath);
QFile::copy(srcPath,destPath);
qDebug("copied");
}
}
}
I had to create a folder as well as in USB because of my requirements and I have given a static name for the files. Then I just copied the data from file of the local machine to the file which I have created in USB with the help of QFile::copy(srcPath, dstPath). I hope it will help someone.

How to iterate through only certain type of files using QDirIterator

I want to iterate through only .xml files (all files under selected folder and its sub-directories), is that possible with QDirIterator?
QDirIterator iter( rootDir, QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
QString fileName;
while(iter.hasNext() )
{
qDebug() << iter.next();
fileName = iter.fileName();
// now I have to check if fileName is indeed a .xml extension
}
As can be seen in code above, if my iterator can jump to .xml files only than I don't have to check for file extension in the loop..is it possible?
One of the constructors of QDirIterator allows a nameFilters argument:
QDirIterator::QDirIterator ( const QString & path, const QStringList & nameFilters, QDir::Filters filters = QDir::NoFilter, IteratorFlags flags = NoIteratorFlags )
Constructs a QDirIterator that can iterate over path, using
nameFilters and filters.
The nameFilters argument is not properly documented, but there is a good chance it works like in QDir::setNameFilters.
QDirIterator it(rootdir, QStringList() << "*.xml", QDir::Files, QDirIterator::Subdirectories);
while (it.hasNext()) {
qDebug() << it.next();
}

Copying Directory and sub directory

Hi im try to copy a directory and all its contents. I thought a good start is to list all the sub directories so i can get an idea and copy the sub structure to my destination folder.
but i need to get the path from the QDir onwards not the path from the root of the machine,
how do i do this so i get sweetassurfwear/folder/folder instaed of /usr/local/websites/sweetassurfwear/folder/folder
here is my code so far
QDir targetDir1("/home/brett/sweetback");
targetDir1.mkdir("sweetassurfwear");
QDir targetDir("/usr/local/websites/sweetassurfwear");
targetDir.setFilter(QDir::NoDotAndDotDot| QDir::Dirs | QDir::Files);
QDirIterator it(targetDir, QDirIterator::Subdirectories);
while (it.hasNext()) {
QFileInfo Info(it.next().relativePath());
QString testName = QString(Info.fileName());
QString testPath = QString(Info.absoluteFilePath());
if(Info.isDir()){
// QString cpd = "/home/brett/sweetback/sweetassurfwear/";
// targetDir.mkdir(cpd+testName);
QString dirname = Info.filePath();
ui.textEdit->append(QString("Copy Files " + dirname));
}
}
The problem is that as I wrote in the comment, you are constructing the file info with the full path as opposed to relative path as you wish to have.
There are two approaches, both presented below, to solve the issue:
1) The work around would be to remove the "prefix" manually by string manipulation.
2) The nicer solution would be to get the relative path out of the absolute and root paths by using the corresponding QDir convenience method.
main.cpp
#include <QDir>
#include <QDirIterator>
#include <QString>
#include <QFileInfo>
#include <QDebug>
int main()
{
QDir targetDir("/usr/local");
targetDir.setFilter(QDir::NoDotAndDotDot| QDir::Dirs | QDir::Files);
QDirIterator it(targetDir, QDirIterator::Subdirectories);
while (it.hasNext()) {
it.next();
QFileInfo Info = it.fileInfo();
QString testName = Info.fileName();
// Work around
// QString testPath = Info.absoluteFilePath().mid(it.path().size()+1);
// Real solution
QString testPath = QString(targetDir.relativeFilePath(Info.absoluteFilePath()));
qDebug() << "TEST:" << testPath;
if(Info.isDir()) {
// QString cpd = "/home/brett/sweetback/sweetassurfwear/";
// targetDir.mkdir(cpd+testName);
QString dirname = Info.filePath();
}
}
return 0;
}
main.pro
TEMPLATE = app
TARGET = main
QT = core
SOURCES += main.cpp
Build and run
qmake && make && ./main
Note that, you do not really need to build a file info based on the path since the QDirIterator instance can return that directly.
Why not just use relativePath method of this class ? Thanks to this method you can get relative path from your path -> link