2012-02-16 20:38:23|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:C/C++, 文件操作|来源:唯设编程网
1.2、不引用MFC的C++环境中使用
/////////////////////////////////
//C++版 目录拷贝
/////////////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <io.h>
#include <windows.h>
void FileCopyTo(char *source, char *dis, char *searchChars, bool bcover /*= true*/)
{
struct _finddata_t ffblk;
char path[256], SourcePath[256], DisPath[256];
sprintf(path,"%sq_*",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);
}
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;
}
方案二:执行系统提供的批处理(CMD)命令
实例代码:
#include<stdlib.h>
#include<stdio.h>
int main()
{
system("md d:aazhao"); // 在下新建文件夹
system("del d:aazhao"); // 删除该文件夹下的所有文件
}
本方案使用简单,操作灵活,但是需要用户具备基本的批处理知识。