MFC下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)
一、文件夹的创建
void CFileOperationDlg::OnButtonMakeFolder()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileFind m_sFileFind; if (!m_sFileFind.FindFile(m_FolderName))
{
CreateDirectory(m_FolderName,NULL);
}
}
二、文件的创建
void CFileOperationDlg::OnButtonMakeFile()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFile m_sFile;
m_sFile.Open(m_FolderName + TEXT("\\") + m_FileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::shareDenyWrite);
m_sFile.SeekToEnd();
m_sFile.Close();
}
三、文件夹的复制(包括文件的复制)
void CFileOperationDlg::OnButtonCopy()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString m_strDestPath;
m_strDestPath = "D:\\TestMyself\\FileOperation\\Debug";
CString m_strSrcPath = "D:\\TestMyself\\FileOperation\\VISTA";
CopyDirectory(m_strSrcPath,m_strDestPath);
} BOOL CFileOperationDlg::CopyDirectory(CString strSrcPath,CString strDestPath)
{
CFileFind m_sFileFind;
if (strSrcPath.IsEmpty())
{
OutputDebugString("源文件名为空,无法进行拷贝!");
return FALSE;
}
if (!m_sFileFind.FindFile(strDestPath))
{
CreateDirectory(strDestPath,NULL);//创建目标文件夹
}
CFileFind finder;
CString path;
path.Format("%s/*.*",strSrcPath);
//AfxMessageBox(path);
BOOL bWorking = finder.FindFile(path);
while (bWorking)
{
bWorking = finder.FindNextFile();
//AfxMessageBox(finder.GetFileName());
if (finder.IsDirectory() && !finder.IsDots())//是文件夹 而且 名称不含 . 或 ..
{
CopyDirectory(finder.GetFilePath(),strDestPath+"/"+finder.GetFileName());//递归创建文件夹+"/"+finder.GetFileName()
}
else
{//是文件,则直接复制
//AfxMessageBox("复制文件"+finder.GetFilePath());//+finder.GetFileName()
CopyFile(finder.GetFilePath(),strDestPath+"/"+finder.GetFileName(),FALSE);
}
} return TRUE;
} CString CFileOperationDlg::GetFilePath()
{
CString m_FilePath; GetModuleFileName(NULL,m_FilePath.GetBufferSetLength(MAX_PATH+),MAX_PATH);
m_FilePath.ReleaseBuffer(); int m_iPosIndex;
m_iPosIndex = m_FilePath.ReverseFind('\\');
m_FilePath = m_FilePath.Left(m_iPosIndex+); return m_FilePath;
} CString CFileOperationDlg::GetFileName()
{
CString sFileName; sFileName = CTime::GetCurrentTime().Format("%Y-%m-%d") + TEXT(".log"); return sFileName;
}
四、文件夹的删除
BOOL CFileOperationDlg::DeleteFolder(LPCTSTR lpszPath)
{
int nLength = strlen(lpszPath);
char *NewPath = new char[nLength + ];
strcpy(NewPath,lpszPath);
NewPath[nLength] = '\0';
NewPath[nLength + ] = '\0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPath;
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
return SHFileOperation(&FileOp) == ;
}
五、文件夹的移动(剪切)
BOOL CFileOperationDlg::MoveFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
{
int nLengthFrm = strlen(lpszFromPath);
char *NewPathFrm = new char[nLengthFrm + ];
strcpy(NewPathFrm,lpszFromPath);
NewPathFrm[nLengthFrm] = '\0';
NewPathFrm[nLengthFrm + ] = '\0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION ;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPathFrm;
FileOp.pTo = lpszToPath;
FileOp.wFunc = FO_MOVE; return SHFileOperation(&FileOp) == ;
}
六、文件写操作
void CFileOperationDlg::OnButtonOk()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
try
{
CFile m_sFile;
CFileFind m_FileFind;
CString m_sErrorMessage;
//CString m_sFileName = GetFileName();
//CString m_sFilePath = GetFilePath();
CString m_sCurrentTime = (CTime::GetCurrentTime()).Format("%Y-%m-%d %X"); if (!m_FileFind.FindFile(m_FolderName))
{
CreateDirectory(m_FolderName,NULL);
}
m_sFile.Open(m_FolderName + TEXT("\\") +m_FileName,CFile::modeCreate |CFile::modeNoTruncate| CFile::modeReadWrite |CFile::shareDenyWrite); m_sFile.SeekToEnd(); if (sizeof(TCHAR) == sizeof(WCHAR))
{
WORD wSignature = 0xFFFF;
m_sFile.Write(&wSignature,);
} m_sErrorMessage = TEXT("*******************") + m_sCurrentTime + TEXT("*******************")+TEXT("\r\n") ;
m_sFile.Write(m_sErrorMessage,m_sErrorMessage.GetLength()*sizeof(TCHAR)); m_RichText += TEXT("\r\n");\
m_sFile.Write(m_RichText,m_RichText.GetLength()*sizeof(TCHAR));
m_sFile.Close();
}
catch(CFileException fileException)
{
return ;
} }
以上代码测试通过!!
MFC下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)的更多相关文章
- Linux基础------文件打包解包---tar命令,文件压缩解压---命令gzip,vim编辑器创建和编辑正文件,磁盘分区/格式化,软/硬链接
作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group > /1.txt2) 将用户信息数据库文件和用户 ...
- linux批量复制或删除同命名规则的文件
如图所示,有多个不同后缀的文件,但他们的前缀都是"QC_TZ.impute." 此时想批量复制图中的文件的话,可以考虑用命令行 cp QC_TZ.impute.* /your/de ...
- Java基础之写文件——创建通道并且写文件(TryChannel)
控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中. import static java.nio.file.StandardOpenOption.*; import java.nio.c ...
- C# 对文件与文件夹的操作包括删除、移动与复制
在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类.文件夹(Folder)是只在Wind ...
- 无法删除 NTFS 盘上的文件或文件夹(对Windows文件的各种情况有比较详细的描述)
简介 本文介绍您可能无法删除 NTFS 文件系统卷上的文件或文件夹的原因,以及如何分析造成此问题的不同原因从而解决此问题. 更多信息 注意:在内部,NTFS 将文件夹作为特殊类型的文件进行处理.因此, ...
- DriverStore文件夹特别大,能删除吗?
DriverStore文件夹特别大,能删除吗? DriverStore\FileRepository文件夹特别大,能删除吗? C:\Windows\System32\DriverStore\FileR ...
- [转载]DriverStore文件夹特别大,能删除吗?
[转载]DriverStore文件夹特别大,能删除吗? 转自博客园https://www.cnblogs.com/lovebing/p/6951833.html 这篇文章,清理完C盘多了20G!不要太 ...
- MFC下MCI的使用播放音乐
最近研究了一下MFC下的音乐的播放,主要使用了MCI 1.需要包含的库文件 在链接资源里(link)添加库文件VFW32.lib winmm.lib 2.包含的头文件 #include <mms ...
- C++ 文件的复制、删除、重命名
一.文件的复制 #include <iostream>#include <fstream>using namespace std; int CopyFile(char *Sou ...
随机推荐
- dplyr 数据操作 常用函数(2)
继上一节常用函数,继续了解其他函数 1.desc() 这个函数和SQL中的排序用法是一样的,表示对数据进行倒序排序. 接下来我们看些例子. a=sample(20,50,rep=T)a desc(a) ...
- CNN- 数据预处理
对于CNN输入的数据,常见的有三种处理方式: 1.Mean subtraction. 将数据的每一维特征都减去平均值.在numpy 中 X -= np.mean(X, axis = 0) 2.Nor ...
- spring+springmvc+maven+mybatis整合
jar包依赖:网址search.maven.org 1.spring :spring-core 2. myhabits:myhabits 3.整合spring和myhabits: ...
- 《Intel汇编第5版》 汇编拷贝字符串
一.字符串定义 二.dup指令 三.调用Writestring过程 四.代码以及效果 TITLE String Copy INCLUDE Irvine32.inc includelib Irvine3 ...
- Regmon7.04绿色版(注册表变动实时监视工具)
Regmon V7.04 简繁中文绿色版 软件大小: 155KB软件语言: 简体中文运行环境:Win9x/NT/2000/XP/2003/软件类别:国外软件 / 免费版 / 系统其它Regmon Re ...
- 注册 Gmail,验证手机号码的时候提示“此号码不能用于验证”,怎么解决?
地址:https://www.zhihu.com/question/34834773 刚刚注册成功.折腾了好久,最后换了Chrome浏览器就成功了.手机号在注册的第一个界面时就填+86 xxxxx ...
- dll间接应用问题
在项目prj引用一个dll,a.dll,时,此dll应用b.dll 此时不将b.dll引用添加到prj,会有问题
- python join()阻塞的用法
join()阻塞的用法,用来检测线程有没有完全执行完毕 #!/usr/bin/env python#-*- coding:utf-8 -*-import threadingimport time de ...
- @Autowired注解(转)
5.6.4 @Autowired注解 自Spring诞生以来,
- mysql 分组按条件统计
百度经验 COUNT(CASE WHEN (S.rank = 1) THEN S.loanContractId END ) AS 'MZ', //根据loanContractId 分组,并统计ran ...