C++文件操作

1.获取文件夹创建时间

SYSTEMTIME GetFolderCreateTime(CString sFolder)
{
    SYSTEMTIME screatetime;
    HANDLE hDir;
    hDir = CreateFile(sFolder,GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_DELETE,NULL,
        OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);//打开现存目录 只读方式打开即可

    FILETIME lpCreateTime,lpAccessTime,lpWriteTime;
    if(GetFileTime(hDir,&lpCreateTime,&lpAccessTime,&lpWriteTime))
    {
        FILETIME fcreatetime;
        FileTimeToLocalFileTime(&lpCreateTime,&fcreatetime);//转换为本地时间
        FileTimeToSystemTime(&fcreatetime,&screatetime);//转换为系统时间
    }

    CloseHandle(hDir);//关闭文件句柄

    return screatetime;
}

2.获取文件夹内文件名

void GetFolderFile(CString sFolder, CStringArray &fileList)
{
    fileList.RemoveAll();
    CString strMatch;
    if(sFolder.Right(1) == "\\")
        strMatch = sFolder + "*.*";
    else
        strMatch = sFolder + "\\*.*";

    CString strFullName;  
    CFileFind finder;  
    BOOL bWorking = finder.FindFile(strMatch);  
    while (bWorking)  
    {  
        bWorking = finder.FindNextFile();  
        if(finder.IsDots())  
            continue;  

        if(finder.IsDirectory()) 
        {
            continue; //跳过子文件夹
        }else  
        { 
            strFullName = finder.GetFilePath(); 
            fileList.Add(strFullName);
        }  
    }  

    finder.Close();
}

3.获取文件最后修改日期

bool GetFileModifyDate(CString filePathName, SYSTEMTIME &modDate)
{
    HANDLE   hFile;  
    WIN32_FIND_DATA   wfd;  
    SYSTEMTIME   systime;  
    FILETIME   localtime;  
    
    memset(&wfd, 0, sizeof(wfd));  
    if((hFile=FindFirstFile(filePathName, &wfd))==INVALID_HANDLE_VALUE)  
        return false; 
    //转换时间  
    FileTimeToLocalFileTime(&wfd.ftLastWriteTime,&localtime);  
    FileTimeToSystemTime(&localtime,&systime);
    
    modDate = systime; 
    return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值