赞
踩
QT 文件复制,移动(剪切)操作_qt剪切文件到指定路径-CSDN博客
如何移动一个文件?
- QString old_name=QString("D:\\c++\\c++优秀源码学习.txt");
- QString new_name=QString("D:\\c++优秀源码学习.txt");
- bool x= QFile::rename(old_name,new_name); //A路径移动到B路径
- qDebug()<<x;
- //true
- QString old_name=QString("D:\\c++\\c++优秀源码学习.txt");
- QString new_name=QString("D:\\123456\\c++优秀源码学习.txt");
- bool x= QFile::rename(old_name,new_name); //A路径移动到B路径
- qDebug()<<x;
- //false:因为没有D:\\123456这个目录。需要先创建该目录。
如何移动文件夹(包含里面的全部内容):
- #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()<<"不存在,创建一个";
- 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();
- }
如何删除文件夹:
Qt拷贝文件、文件夹、创建文件夹、删除文件夹操作_qtmakedir文件夹-CSDN博客
- QString str="D:/Qt/a";
- QDir dir(str);
- if(!dir.exists()){
- qDebug()<<"not exists";
- }
- qDebug()<<dir.rmpath(dir.absolutePath());
当文件夹为空时,删除成功。
当文件夹不为空时,删除失败。
删除文件夹(递归)(文件夹中必须只有文件夹)
- 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();
- }
但经过测试:.vs文件夹是删不掉的。
因为.vs 是隐藏文件夹
需要添加:QDir::Hidden字段。
dir.setFilter(QDir::Hidden|QDir::Dirs | QDir::NoDotAndDotDot);
功能:把文件夹从A移动到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()<<"不存在,创建一个";
- 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();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。