QT实现批量修改文件名的程序

一目录下有多个文件名,如下:

bd_aaaaaaa_1.avi bd_aaaaaaa_2.avi bd_aaaaaaa_3.avi bd_aaaaaaa_4.avi ….

修改成: 
bd_1.avi bd_2.avi bd_3.avi bd_4.avi …

首先 需要接收用户的配置信息: 目录的路径, 需要修改的字符串(名字的子串), 修改成什么样的名字 
需要获取指定目录下的文件名. 
修改文件名, 并加入进度条显示进度。

所需的技术: 
1 可用QLineEdit接收用户的输入信息. 
QString QFileDialog::getExistingDirectory 
用于获取用户指定的目录路径

2 可用QDir获取目录里的文件名 
3 每个文件名都是QString,文件名的修改就是修改字符串里的子串

    //把字符串中的 llo 修改成 abc
    QString str = "hello world";
    QString str2 = "abc";
    int n = str.indexOf("llo"); //查找"llo"在字符串中出现的位置
    str = str.replace(n, 4, "abc"); //进行替换
    qDebug() << str;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  1. 文件改名可用QFile的函数成员rename来完成.

完整代码

mywin.h
#ifndef MYWIN_H
#define MYWIN_H
#include <QWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QProgressBar>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>


class MyWin : public QWidget
{
    Q_OBJECT
private:
    QLabel *lbl_dir, *lbl_str_before, *lbl_str_after;
    QLineEdit *lnd_dir; //目录路径的输入框
    QLineEdit *lnd_str_before; //修改前的字符串输入框
    QLineEdit *lnd_str_after;  //修改后的字符串输入框
    QPushButton *btn_dir, *btn_change;
    QProgressBar *bar;
    QHBoxLayout *hlayout1, *hlayout2;
    QVBoxLayout *vlayout;

public:
    explicit MyWin(QWidget *parent = 0);
    ~MyWin();

signals:

public slots:
    void slot_btn_dir();
    void slot_btn_change();
};
#endif // MYWIN_H
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
mywin.cpp
#include "mywin.h"
#include <QFile>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <QThread>
MyWin::MyWin(QWidget *parent) : QWidget(parent)
{
    lbl_dir = new QLabel("path:");
    lnd_dir = new QLineEdit;
    btn_dir = new QPushButton;
    btn_dir->setText("...");
    hlayout1 = new QHBoxLayout;
    hlayout1->addWidget(lbl_dir, 1);
    hlayout1->addWidget(lnd_dir, 8);
    hlayout1->addWidget(btn_dir, 1);
    lbl_str_before = new QLabel("before:");
    lnd_str_before = new QLineEdit;
    lbl_str_after = new QLabel("after:");
    lnd_str_after = new QLineEdit;
    btn_change = new QPushButton("change");
    hlayout2 = new QHBoxLayout;
    hlayout2->addWidget(lbl_str_before, 2);
    hlayout2->addWidget(lnd_str_before, 3);
    hlayout2->addWidget(lbl_str_after, 2);
    hlayout2->addWidget(lnd_str_after, 3);
    hlayout2->addWidget(btn_change, 2);
    bar = new QProgressBar;
    vlayout = new QVBoxLayout(this);
    vlayout->addLayout(hlayout1);
    vlayout->addLayout(hlayout2);
    vlayout->addWidget(bar);
    resize(320, 140);
    // 信号与槽的参数需要匹配,但如果信号的参数有默认值,可以连接没参数的槽
    connect(btn_dir, SIGNAL(clicked(bool)), this, SLOT(slot_btn_dir()));
    connect(btn_change, SIGNAL(clicked(bool)), this, SLOT(slot_btn_change()));
}
MyWin::~MyWin()
{
    delete lbl_dir;
    delete lbl_str_after;
    delete lbl_str_before;
    delete lnd_dir;
    delete lnd_str_after;
    delete lnd_str_before;
    delete btn_change;
    delete btn_dir;
    delete hlayout1;
    delete hlayout2;
    delete vlayout;
}
void MyWin::slot_btn_change()
{
    QString str_dir = lnd_dir->text();
    if (str_dir.isEmpty())
    {
        QMessageBox::critical(this, "error", "no dir selected");
        lnd_dir->setFocus();
        return;
    }
    QDir dir(str_dir);
    if (!dir.exists()) //目录不存在
    {
        QMessageBox::critical(this, "error", "invlaid dir");
        btn_dir->setFocus();
        return;
    }
    QString str_before = lnd_str_before->text();
    QString str_after = lnd_str_after->text();
    if (str_before.isEmpty())
    {
        QMessageBox::critical(this, "error", "no string");
        lnd_str_before->setFocus();
        return;
    }
    // 获取目录里的文件,并过滤".", ".."
    QStringList files = dir.entryList(QDir::AllEntries|QDir::NoDotAndDotDot);
    QFile f;
    QString str;
    int n;
    bar->setMaximum(files.size()); // 设置滚动条的最大值为文件的个数,每处理一个文件,滚动条的值+1
    for (int i = 0; i < files.size(); i++)
    {
        str = files.at(i);
        if (str.contains(str_before)) //如果包括有要修改的字符串
        {
            qDebug() << "before" << str;
            f.setFileName(str_dir + "/" + str);
            n = str.indexOf(str_before);
            str = str.replace(n, str_before.size(), str_after);
            qDebug() << "after: " << str;
            f.rename(str_dir + "/" + str);
        }
        bar->setValue(i+1);
        QThread::msleep(2000/files.size()); //纯属装X
    }
}
void MyWin::slot_btn_dir()
{
    //从根目录打开, 以当前窗口对象为中心, 标题栏为"open"
    QString str = QFileDialog::getExistingDirectory(this, "open", "/");
    if (str.isEmpty()) //用户取消选择目录
        return;
    lnd_dir->setText(str);
    lnd_str_before->setFocus(); //光标移动到修改前的字符串输入框
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
main.cpp

#include <QApplication>
#include "mywin.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWin win;
    win.show();
    return a.exec();
}

猜你喜欢

转载自blog.csdn.net/u014746838/article/details/79328687
今日推荐