Qt——创建文件/文件夹时,判断该目录下是否存在同名文件夹或同名文件

1、参考网友  (注意 ,需要引入对应的头文件)

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
1. 判断文件夹是不是存在
参数说明:
QString fullPath;//文件夹全路径
/*方法1*/
bool isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
      return true;
    }
    return false;
}
/*方法2*/
bool isDirExist(QString fullPath)
{
    QFileInfo fileInfo(fullPath);
    if(fileInfo.isDir())
    {
      return true;
    }
    return false;
}
 
2. 判断文件是不是存在
参数说明:
QString fullFileName;//文件全路径(包含文件名)
/*方法1*/
bool isFileExist(QString fullFileName)
{
    QFileInfo fileInfo(fileFullName);
    if(fileInfo.isFile())
    {
        return true;
    }
    return false;
}
 
3、判断文件或文件夹是不是存在(即不确定字符串是文件还是文件夹路径)
参数说明:
QString fullFilePath;//路径名
/*方法1*/
bool isFileExist(QString fullFilePath)
{
    QFileInfo fileInfo(fullFilePath);
    if(fileInfo.exists())
    {
        return true;
    }
    return false;
}
/*方法2*/
bool isFileExist(QString fullFilePath)
{
    QFile file(fullFilePath);
    if(file.exists())
    {
        return true;
    }
    return false;
}
 
4、判断文件夹是否存在,不存在则创建
/*方法1*/
bool isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
      return true;
    }
    else
    {
       bool ok = dir.mkdir(fullPath);//只创建一级子目录,即必须保证上级目录存在
       return ok;
    }
}
/*方法1*/
bool isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
      return true;
    }
    else
    {
       bool ok = dir.mkpath(fullPath);//创建多级目录
       return ok;
    }
}

  

2、我的项目使用(代码片段)

复制代码
void NewObject::_okCreaet() //确定创建工程 槽函数
{
    qDebug() << QString::fromLocal8Bit("确定槽函数,触发!");
    _projectName = ui.lineEdit2_NamE->text(); //获取工程名称
    qDebug() << QString::fromLocal8Bit("项目名为 = ") << _projectName;
    
    if (_pathName_!=nullptr && _projectName !=nullptr)
    {
        qDebug() << QString::fromLocal8Bit("项目创建成功! ");
         QString _pahtANDfile = _pathName_ + "/" + _projectName+".ac"; //路径+名称
        //判断该目录下是否存在该同名工程文件
        bool fileExist = false;
            QFileInfo fileInfo(_pahtANDfile);
            if (fileExist = fileInfo.isFile()) //判断是否存在同名文件
            {
                qDebug() << QString::fromLocal8Bit("存在该工程文件! ");
                //return ;  //如果存在,则退出
            }
        // 判断存放stl和spro的文件夹是否存在    
            QString _DirResult = _pathName_ + "/" + "result"; //文件夹绝对路径
        QDir dir(_DirResult);
        bool dirExits = dir.exists();

        if (((fileExist && true) | (dirExits && true)))
        {
            qDebug() << QString::fromLocal8Bit(" fileExist && true = ") <<(fileExist && true);
            qDebug() << QString::fromLocal8Bit(" dirExits&& true = ") << (dirExits && true);
            qDebug() << QString::fromLocal8Bit(" fileExist && true  | dirExits&& true = ") << ((fileExist && true) | (dirExits && true));

            qDebug() << QString::fromLocal8Bit(" fileExist = ") << fileExist;
            qDebug() << QString::fromLocal8Bit(" dirExits = ") << dirExits;

            qDebug() << QString::fromLocal8Bit(" 存在同名文件和同名文件夹!") << endl;
            qDebug() << QString::fromLocal8Bit(" 退出!") << endl;
            QMessageBox::information(nullptr, QString::fromLocal8Bit("提示"),
                QString::fromLocal8Bit("项目文件或result文件夹已存在\n请从新选择路径!"));
            ui.lineEdit2_NamE->clear(); //获取工程名称,清空    否则下次还会显示
            ui.lineEdit2_Path->clear(); //显示路径, 清空  否则下次还会显示    
            _pahtANDfile.clear(); //清空文件绝对路径信息
            _DirResult.clear();  //清空文件夹绝对路径信息,不清空的话,即便删除路径下原来的工程文件和文件夹,也会认为是存在的,只能从新打开软件判断不存在。
            return;
    
        }
        else {
            qDebug() << QString::fromLocal8Bit(" fileExist && true = ") << (fileExist && true);
            qDebug() << QString::fromLocal8Bit(" dirExits&& true = ") << (dirExits && true);
            qDebug() << QString::fromLocal8Bit(" fileExist && true  | dirExits&& true = ") << ((fileExist && true)|(dirExits && true));


            qDebug() << QString::fromLocal8Bit(" fileExist = ") << fileExist;
            qDebug() << QString::fromLocal8Bit(" dirExits = ") << dirExits;

            qDebug() << QString::fromLocal8Bit(" 该目录下不存在同名的ac文件,以及同名的result文件夹") << endl;
            qDebug() << QString::fromLocal8Bit(" 创建该工程!") << endl;
        }

        //创建工程文件
        QFile _objectName(_pahtANDfile);  
        _objectName.open(QIODevice::WriteOnly);
        _objectName.write("projectFileName"); //工程文件写入信息(用来打开工程用)
        _objectName.close();

        //创建result文件夹
        if (!dirExits) {
            bool ismkdir = QDir().mkdir(_DirResult);
            if (!ismkdir)
                qDebug() << "Create path fail" << endl;
            else
                qDebug() << "Create  success" << endl;
        }
        else {
            qDebug() << QString::fromLocal8Bit(" result文件夹存在") << endl;
            //return; 文件夹如果存在,则退出
        }

        //将模板中的stl和spro文件复制到新创建的result文件夹中
            //默认文件选择路径为,程序所在的路径
        QString gettemplateDir = "templateSpro"; //获取模板路径
        QDir  fromDir(gettemplateDir);  //实例化这个路径对象
        QString fronDir = _pathName_ + "/" + "result"; //获取目标文件夹路径
        QDir  toDir(fronDir);//实例化目标文件夹路径对象
        bool bCoverIfFileExists = true;
        bool a =  _CopyStl(fromDir, toDir, bCoverIfFileExists); //拷贝模板下的stl文件和spro文件到新工程下
         qDebug() << "a = "<< a; //执行成功返回true 
        if (a == true)
        {
            emit _projectCreatOK_signal(a); //发送工程创建成功信号
            //_GuangHe_FangZhen->_ModelImport1_Item
            ui.lineEdit2_NamE->clear(); //获取工程名称,清空    否则下次还会显示
            ui.lineEdit2_Path->clear(); //显示路径, 清空  否则下次还会显示    
            this->close(); //关闭窗口
        }
        else
        {
            QMessageBox::information(nullptr, QString::fromLocal8Bit("提示"),
                QString::fromLocal8Bit("项目创建失败!"));
            return;
        }    
    }
复制代码

 

 

效果

 

posted @   雾枫  阅读(1533)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
点击右上角即可分享
微信分享提示