1. 程式人生 > >QT實現批量修改檔名的程式

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();
}