最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Qt把文件夾從A移動(dòng)到B的實(shí)現(xiàn)示例

 更新時(shí)間:2025年03月16日 09:36:06   作者:lpl還在學(xué)習(xí)的路上  
本文主要介紹了Qt把文件夾從A移動(dòng)到B的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

QT 文件復(fù)制,移動(dòng)(剪切)操作

文件復(fù)制

 bool x= QFile::copy(old_name,new_name);
 qDebug()<<x;

文件移動(dòng)(剪切)

    QString old_name="路徑A";
    QString new_name="新路徑A";
    bool x= QFile::rename(old_name,new_name); //A路徑移動(dòng)到B路徑
    qDebug()<<x;

重命名之前,文件已經(jīng)在我程序的其他模塊中被使用了,使用中的文件是不能重命名的(win平臺(tái)下).
補(bǔ)充:遇到無法移動(dòng)的bug,嘗試手動(dòng)移動(dòng)文件后在執(zhí)行程序成功

如何移動(dòng)一個(gè)文件? 

    QString old_name=QString("D:\\c++\\c++優(yōu)秀源碼學(xué)習(xí).txt");
    QString new_name=QString("D:\\c++優(yōu)秀源碼學(xué)習(xí).txt");
    bool x= QFile::rename(old_name,new_name); //A路徑移動(dòng)到B路徑
    qDebug()<<x;
    //true
    QString old_name=QString("D:\\c++\\c++優(yōu)秀源碼學(xué)習(xí).txt");
    QString new_name=QString("D:\\123456\\c++優(yōu)秀源碼學(xué)習(xí).txt");
    bool x= QFile::rename(old_name,new_name); //A路徑移動(dòng)到B路徑
    qDebug()<<x;
    //false

因?yàn)闆]有D:\\123456這個(gè)目錄。需要先創(chuàng)建該目錄。 

    QString old_name=QString("D:\\ccc\\1.txt");//存在
    QString new_name=QString("D:\\ccc\\ddd\\1.txt");//本身就存在
    bool x= QFile::rename(old_name,new_name); //A路徑移動(dòng)到B路徑
    qDebug()<<x;
    //false

因?yàn)锽路徑本身已經(jīng)存在同名文件,所以移動(dòng)失敗。 

這樣寫就可以了:

    QString old_name=QString("D:\\ccc\\1.txt");
    QString new_name=QString("D:\\ccc\\ddd\\1.txt");
    bool is_exists=QFile::exists(new_name);
    if(is_exists){
        QFile::remove(new_name);
    }
    bool x= QFile::rename(old_name,new_name); //A路徑移動(dòng)到B路徑
    qDebug()<<x;

如何移動(dòng)文件夾(包含里面的全部內(nèi)容):

#include "widget.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
#include <QDir>
#include <unistd.h>
QString old_path;
QString new_path;
void create_Multilevel_folder(char* path)
{
    int len = strlen(path);
    int _len = 0;
    for (int i = 0; i < len; ++i)
    {
        if (path[i] == '/') {
            _len++;
        }
    }
    int* a = new int[_len];
    memset(a, 0, sizeof(a));
    for (int i = 0, b = 0; i < len; ++i)
    {
        if (path[i] == '/') {
            a[b] = i;
            b++;
        }
    }
    for (int i = 0; i < _len; i++)
    {
        char p[4096];
        strcpy(p, path);
        p[a[i]] = '\0';
        if (access(p, 0) == -1) {
            qDebug()<<"不存在,創(chuàng)建一個(gè)";
            mkdir(p);
        }
    }
}

void print_Files(QString path)
{
    QDir dir(path);
    dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
    QStringList list = dir.entryList();

    for (int i = 0; i < list.count(); ++i)
    {
        QString path1 = path + "/" + list[i];
        qDebug() <<"Old_Files:"<< path1;
        //替換
        QString path2=path1.right(path1.length()-old_path.length());
        QString path3=new_path+path2;
        qDebug()<<path3;
        qDebug()<<"New_Files:"<<QFile::rename(path1,path3);
    }
}
void print_files_and_dirs(QString path)
{
    QDir dir(path);
    dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
    QStringList list = dir.entryList();

    for (int i = 0; i < list.count(); ++i)
    {
        QString path1 = path + "/" + list[i];
        qDebug() <<"old_Dir:"<< path1;
        QString path2=path1.right(path1.length()-old_path.length());
        QString path3=new_path+path2+"/";
        qDebug()<<"new_Dir:"<<path3;
        create_Multilevel_folder(path3.toLocal8Bit().data());
        print_files_and_dirs(path1);//前面要加上前綴
    }
    print_Files(path);
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    old_path="D:/Qt/zip";
    new_path="D:/Qt/aaa";
    QString str=new_path+"/";
    create_Multilevel_folder(str.toLocal8Bit().data());
    print_files_and_dirs(old_path);
    return a.exec();
}

如何刪除文件夾:

    QString str="D:/Qt/a";
    QDir dir(str);
    if(!dir.exists()){
        qDebug()<<"not exists";
    }
    qDebug()<<dir.rmpath(dir.absolutePath());

當(dāng)文件夾為空時(shí),刪除成功。

當(dāng)文件夾不為空時(shí),刪除失敗。

刪除文件夾(遞歸)(文件夾中必須只有文件夾)

bool del_folder(QString str)
{
    QDir dir(str);
    if(!dir.exists()){
        qDebug()<<"not exists";
    }
    return dir.rmpath(dir.absolutePath());
}

void del_folders(QString path)
{
    QDir dir(path);
    dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
    QStringList list = dir.entryList();
    //qDebug()<<"count:"<<list.count();
    //qDebug()<<list;
    for (int i = 0; i < list.count(); ++i)
    {
        QString path1 = path + "/" + list[i];
        qDebug() <<"old_Dir:"<< path1;
        del_folders(path1);
    }
    if(list.count()==0){
        del_folder(path);
    }
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    old_path="D:/app/f";
    del_folders(old_path);
    //好像找不到.vs文件
    return a.exec();
}

但經(jīng)過測試:.vs文件夾是刪不掉的。

因?yàn)?strong>.vs 是隱藏文件夾

需要添加:QDir::Hidden字段。

dir.setFilter(QDir::Hidden|QDir::Dirs | QDir::NoDotAndDotDot);

功能:把文件夾從A移動(dòng)到B,原位置刪除。

#include "widget.h"
#include <QApplication>
#include <QFile>
#include <QDebug>
#include <QDir>
#include <unistd.h>
QString old_path;
QString new_path;
void create_Multilevel_folder(char* path)
{
    int len = strlen(path);
    int _len = 0;
    for (int i = 0; i < len; ++i)
    {
        if (path[i] == '/') {
            _len++;
        }
    }
    int* a = new int[_len];
    memset(a, 0, sizeof(a));
    for (int i = 0, b = 0; i < len; ++i)
    {
        if (path[i] == '/') {
            a[b] = i;
            b++;
        }
    }
    for (int i = 0; i < _len; i++)
    {
        char p[4096];
        strcpy(p, path);
        p[a[i]] = '\0';
        if (access(p, 0) == -1) {
            qDebug()<<"不存在,創(chuàng)建一個(gè)";
            mkdir(p);
        }
    }
}

void print_Files(QString path)
{
    QDir dir(path);
    dir.setFilter(QDir::Hidden|QDir::Files | QDir::NoDotAndDotDot);
    QStringList list = dir.entryList();

    for (int i = 0; i < list.count(); ++i)
    {
        QString path1 = path + "/" + list[i];
        qDebug() <<"Old_Files:"<< path1;
        //替換
        QString path2=path1.right(path1.length()-old_path.length());
        QString path3=new_path+path2;
        qDebug()<<path3;
        qDebug()<<"New_Files:"<<QFile::rename(path1,path3);
    }
}
void print_files_and_dirs(QString path)
{
    QDir dir(path);
    dir.setFilter(QDir::Hidden|QDir::Dirs | QDir::NoDotAndDotDot);
    QStringList list = dir.entryList();

    for (int i = 0; i < list.count(); ++i)
    {
        QString path1 = path + "/" + list[i];
        qDebug() <<"old_Dir:"<< path1;
        QString path2=path1.right(path1.length()-old_path.length());
        QString path3=new_path+path2+"/";
        qDebug()<<"new_Dir:"<<path3;
        create_Multilevel_folder(path3.toLocal8Bit().data());
        print_files_and_dirs(path1);//前面要加上前綴
    }
    print_Files(path);
}
bool del_folder(QString str)
{
    QDir dir(str);
    if(!dir.exists()){
        qDebug()<<"not exists";
    }
    return dir.rmpath(dir.absolutePath());
}

void del_folders(QString path)
{
    QDir dir(path);
    dir.setFilter(QDir::Hidden|QDir::Dirs | QDir::NoDotAndDotDot);
    QStringList list = dir.entryList();
    //qDebug()<<"count:"<<list.count();
    //qDebug()<<list;
    for (int i = 0; i < list.count(); ++i)
    {
        QString path1 = path + "/" + list[i];
        qDebug() <<"old_Dir:"<< path1;
        del_folders(path1);
    }
    if(list.count()==0){
        del_folder(path);
    }
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    old_path="D:/VS/ConsoleApplication1";
    new_path="D:/VS/ConsoleApplication2";
    QString str=new_path+"/";
    create_Multilevel_folder(str.toLocal8Bit().data());
    print_files_and_dirs(old_path);

    del_folders(old_path);
    return a.exec();
}

到此這篇關(guān)于Qt把文件夾從A移動(dòng)到B的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Qt把文件夾從A移動(dòng)到B內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • 美化你的代碼 vb(VBS)代碼格式化的實(shí)現(xiàn)代碼

    美化你的代碼 vb(VBS)代碼格式化的實(shí)現(xiàn)代碼

    雖然VB.NET出現(xiàn)很久了,但還有好多人仍然在使用VB6。我在實(shí)現(xiàn)一些小功能的時(shí)候也喜歡用VB6,畢竟誰都不想每天的美好心情被VS那烏龜般的啟動(dòng)速度影響
    2012-05-05
  • 使用C語言編寫一個(gè)關(guān)機(jī)惡搞小程序

    使用C語言編寫一個(gè)關(guān)機(jī)惡搞小程序

    system函數(shù)的參數(shù)是"shutdown"時(shí),它將會(huì)執(zhí)行系統(tǒng)的關(guān)機(jī)命令,所以本文將利用這一特點(diǎn)制作一個(gè)關(guān)機(jī)惡搞小程序,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • 如何讓Dev-C++支持auto關(guān)鍵字呢

    如何讓Dev-C++支持auto關(guān)鍵字呢

    這篇文章主要介紹了如何讓Dev-C++支持auto關(guān)鍵字問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • Qt學(xué)習(xí)教程之表格控件螞蟻線詳解

    Qt學(xué)習(xí)教程之表格控件螞蟻線詳解

    如果有用過PS的選區(qū)工具應(yīng)該就會(huì)知道螞蟻線是什么東西了,就是用來表示選區(qū)的一種虛線,關(guān)鍵還是要?jiǎng)討B(tài)的!下面這篇文章主要給大家介紹了關(guān)于Qt學(xué)習(xí)教程之表格控件螞蟻線的相關(guān)資料,需要的朋友可以參考下
    2018-07-07
  • C++非繼承時(shí)函數(shù)成員訪問屬性和類繼承過程中的訪問控制

    C++非繼承時(shí)函數(shù)成員訪問屬性和類繼承過程中的訪問控制

    這篇文章主要介紹了C++非繼承時(shí)函數(shù)成員訪問屬性和類繼承過程中的訪問控制,非繼承時(shí),protected成員和private成員沒有任何區(qū)別,都是類內(nèi)部可以直接訪問它們、類外部的類對(duì)象不可訪問它們、類內(nèi)部的類對(duì)象可以訪問它們,更多詳細(xì)內(nèi)容請(qǐng)參考下面相關(guān)資料
    2022-03-03
  • C++帶有指針成員的類處理方式詳解

    C++帶有指針成員的類處理方式詳解

    這篇文章主要為大家詳細(xì)介紹了C++帶有指針成員的類處理方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • C++通過COM接口操作PPT

    C++通過COM接口操作PPT

    這篇文章主要為大家詳細(xì)介紹了C++通過COM接口操作PPT的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • C語言double和float 實(shí)例分析

    C語言double和float 實(shí)例分析

    本文主要介紹了C語言中的浮點(diǎn)數(shù)(float,double),并通過實(shí)例代碼進(jìn)行分析比較,希望能幫助學(xué)習(xí)相關(guān)知識(shí)的同學(xué)
    2016-07-07
  • VC++開發(fā)中完美解決頭文件相互包含問題的方法解析

    VC++開發(fā)中完美解決頭文件相互包含問題的方法解析

    本文中,為了敘述方便,把class AClass;語句成為類AClass的聲明,把class AClass開始的對(duì)AClass的類成員變量、成員函數(shù)原型等的說明稱為類的定義,而把在CPP中的部分稱為類的定義
    2013-09-09
  • C++類中隱藏的幾個(gè)默認(rèn)函數(shù)你知道嗎

    C++類中隱藏的幾個(gè)默認(rèn)函數(shù)你知道嗎

    這篇文章主要為大家詳細(xì)介紹了C++類中隱藏的幾個(gè)默認(rèn)函數(shù),使用數(shù)據(jù)庫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03

最新評(píng)論

青神县| 乐山市| 田林县| 苍梧县| 嘉黎县| 冷水江市| 眉山市| 太康县| 丹巴县| 永川市| 建阳市| 叶城县| 大庆市| 霍州市| 留坝县| 固原市| 扎囊县| 德庆县| 宁都县| 栾城县| 小金县| 洪泽县| 集安市| 塔河县| 通许县| 章丘市| 巫山县| 浏阳市| 太保市| 上林县| 无棣县| 都匀市| 平泉县| 涞源县| 犍为县| 上犹县| 南华县| 邵阳市| 高平市| 嵩明县| 常宁市|