VC/MFC(C++)实现文件拷贝和文件删除等
发布日期:2021-05-10 16:05:57 浏览次数:18 分类:精选文章

本文共 4406 字,大约阅读时间需要 14 分钟。

������������������������������Windows������������������������������������������������������������������������������������������

1. ������������

������������������MFC���������

���MFC������������������������������������������������CFileFind���������������������������������������������

void FileCopyTo(CString source, CString destination, CString searchStr, BOOL cover /*= true*/)
{
CFileFind filefinder;
CString strSearchPath = source + "//" + searchStr;
BOOL bfind = filefinder.FindFile(strSearchPath);
while (bfind)
{
bfind = filefinder.FindNextFile();
CString filename = filefinder.GetFileName();
CString sourcePath = source + "//" + filename;
CString dispPath = destination + "//" + filename;
CopyFile((LPCSTR)sourcePath, (LPCSTR)dispPath, cover);
}
filefinder.Close();
}

���������������

  • CFileFind������������������������������FindFile���FindNextFile���������������������������������
  • ���������������������������������������������������������������������CopyFile���������������������������
  • cover���������������������������������������������

������������������C++���������

������C++������������������findfirst���findnext���������������������������������������������

#include 
#include
#include
#include
#include
void FileCopyTo(char *source, char *dis, char *searchChars, bool bcover /*= true*/)
{
struct _finddata_t ffblk;
char path[256], SourcePath[256], DisPath[256];
sprintf(path, "%s//q_*", source);
long done = _findfirst(path, &ffblk);
int find = 0;
while (find == 0)
{
if(strcmp(ffblk.name, "q_"))
{
sprintf(SourcePath, "%s//%s", source, ffblk.name);
sprintf(DisPath, "%s//%s", dis, ffblk.name);
CopyFile(SourcePath, DisPath, bcover);
}
find = _findnext(done, &ffblk);
}
_findclose(done);
}

���������������

  • ������_finddata_t���������������������������������
  • ������sprintf���������������������CopyFile���������������������������
  • find������������������������������������������������������������

2. ������������

���������������������������������

���������������������������������������������������������������������������������������

bool 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;
}

���������������

  • ������CFileFind���������������������������������������������
  • ���������������������������������DeleteDirectory���������������
  • ���������������������������������DeleteFile���������������������

������������������������������

���������������������������������������������������������������������������������������������������������������

int main()
{
system("md d://aa//zhao "); // ������������������
system("del d://aa//zhao "); // ������������������������������������
}

���������������

  • ���oose���������system������������������������������������
  • "md d://aa//zhao "������������������������������������������
  • "del d://aa//zhao "������������������������������������������

������������

  • ������������: ���FileCopyTo������������cover���������������TRUE������������������������������������������
  • ������������: ������������������������������<���������������������������������������
  • ������������: ���������������������������������������������������������������������������������
  • ������������������������������Windows������������������������������������������������������������������������������������������������������������������

    上一篇:Qt学习之路(1):前言(转载)
    下一篇:使用C语言获取文件夹地址的方法收藏

    发表评论

    最新留言

    网站不错 人气很旺了 加油
    [***.192.178.218]2025年04月20日 23时53分17秒