学逆向论坛

找回密码
立即注册

只需一步,快速开始

发新帖

2万

积分

41

好友

1166

主题
发表于 2023-7-3 15:53:45 | 查看: 717| 回复: 1

做客户端开发,避免不了与文件以及文件夹打交道。

今天就与大家分享一下,windows平台上的一些关于文件以及文件夹的操作:

1 重命名
不再赘述,看看之前的博客吧:
《C++中修改文件夹名以及文件名》

2 判断是文件还是文件夹是否存在

// 判断文件是否存在
BOOL IsFileExist(const CString& csFile)
{
    DWORD dwAttrib = GetFileAttributes(csFile);
    return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
// 判断文件夹是否存在
BOOL IsDirExist(const CString & csDir)
{
    DWORD dwAttrib = GetFileAttributes(csDir);
    return INVALID_FILE_ATTRIBUTES != dwAttrib && 0 == (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
}
// 判断文件或文件夹是否存在
BOOL IsPathExist(const CString & csPath)
{
    DWORD dwAttrib = GetFileAttributes(csPath);
    return INVALID_FILE_ATTRIBUTES != dwAttrib;
}
// 变体
BOOL IsPathExist(const CString & csPath)
{
    WIN32_FILE_ATTRIBUTE_DATA attrs = { 0 };
    return 0 != GetFileAttributesEx(csPath, GetFileExInfoStandard, &attrs);
}

3删除文件

if(!_access(source,0))
{
    SetFileAttributes(source,0);
    if(DeleteFile(source))//删除成功
    {
       cout<<source<<" 已成功删除."<<endl;
    }
    else//无法删除:文件只读或无权限执行删除
    {
       cout<<source<<" 无法删除:文件为只读属性或无删除权限."<<endl;
    }
    }
    else//文件不存在
    {
    cout<<source<<" 不存在,无法删除."<<endl;
}

4删除文件夹下所有的文件

BOOL CDlgData::DeleteDirectory(char *sDirName)
{
    CFileFind tempFind; 
    char sTempFileFind[200] ;
    sprintf(sTempFileFind,"%s\\*.*",sDirName); 
    BOOL IsFinded = tempFind.FindFile(sTempFileFind); 
    while (IsFinded) 
    { 
        IsFinded = tempFind.FindNextFile(); 
        if (!tempFind.IsDots()) 
        { 
            char sFoundFileName[200]; 
            strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200)); 
            if (tempFind.IsDirectory()) 
            { 
                char sTempDir[200]; 
                sprintf(sTempDir,"%s\\%s",sDirName,sFoundFileName); 
                DeleteDirectory(sTempDir); 
            } 
            else 
            { 
                char sTempFileName[200]; 
                sprintf(sTempFileName,"%s\\%s",sDirName,sFoundFileName); 
                DeleteFile(sTempFileName); 
            } 
        } 
    } 
    tempFind.Close(); 
    if(!RemoveDirectory(sDirName)) 
    { 
        return FALSE; 
    } 
    return TRUE; 
}

5 拷贝文件

bool CopyFile(const std::wstring &from_path, const std::wstring &to_path)
{
    if (from_path.size() >= MAX_PATH ||
        to_path.size() >= MAX_PATH) {
            return false;
    }
    return (::CopyFileW(from_path.c_str(), to_path.c_str(),
        false) != 0);
}

6 获取文件大小

int64_t GetFileSize(const PathString &filepath)
{
    WIN32_FIND_DATAW file_data;
    HANDLE file = FindFirstFileW(filepath.c_str(), &file_data);
    if (file == INVALID_HANDLE_VALUE)
        return -1;
    LARGE_INTEGER li = { file_data.nFileSizeLow, file_data.nFileSizeHigh };
    FindClose(file);
    return li.QuadPart;
}

7 获取文件夹下所有文件

void GetFilesInDirectory(std::vector<string> &out, const string &directory)
{
    HANDLE dir;
    WIN32_FIND_DATA file_data;
    if ((dir = FindFirstFile((directory + "/*").c_str(), &file_data)) == INVALID_HANDLE_VALUE)
        return; /* No files found */
    do {
        const string file_name = file_data.cFileName;
        const string full_file_name = directory + "/" + file_name;
        const bool is_directory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
        if (file_name[0] == '.')
            continue;
        if (is_directory)
            continue;
        out.push_back(full_file_name);
    } while (FindNextFile(dir, &file_data));
    FindClose(dir);
}


温馨提示:
1.如果您喜欢这篇帖子,请给作者点赞评分,点赞会增加帖子的热度,评分会给作者加学币。(评分不会扣掉您的积分,系统每天都会重置您的评分额度)。
2.回复帖子不仅是对作者的认可,还可以获得学币奖励,请尊重他人的劳动成果,拒绝做伸手党!
3.发广告、灌水回复等违规行为一经发现直接禁言,如果本帖内容涉嫌违规,请点击论坛底部的举报反馈按钮,也可以在【投诉建议】板块发帖举报。
论坛交流群:672619046

    发表于 2024-3-25 09:21:25
    务端、客户端、GM模式、视频教程

    小黑屋|手机版|站务邮箱|学逆向论坛 ( 粤ICP备2021023307号 )|网站地图

    GMT+8, 2024-7-27 19:06 , Processed in 0.130139 second(s), 40 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表