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 ...
随机推荐
- [UWP小白日记-6]页面跳转过度动画
前言 在学习中发现页面导航默认是没有过度动画的,直接就导航过去太粗暴了( ̄へ ̄),于是打算上动画结果不言而喻自己进了坑完全不懂动画,然后就是各种疯狂(´・_・`)的搜索资料看了后终于有点头绪. 再后来 ...
- 前端 TDD 开发
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px ".PingFang SC"; color: #454545 } p.p2 ...
- UserDefault数据读取
//GameScene.h #include "cocos2d.h" USING_NS_CC; class GameScene : public cocos2d::Layer{pu ...
- Gentoo解决Windows系统txt文本中文乱码问题
Linux与Windows系统语言编码区别 在Linux操作系统下,我们有时打开在windows下的txt文件,发现在windows下能正常显示的txt文件出现了中文乱码. 出现这种情况的原因为两种操 ...
- ArcGIS 10.5 named user介绍
1 Named user概述 1.1 Named user简介 Named user是ArcGIS产品自10.3版本正式推出的一种以用户为中心的授权机制,也称"授权 ...
- List集合对象中的排序,随机显示
List<User> students = new ArrayList<User>(); User user1 = new User(); user1.setAge(112); ...
- Little Puzzlers–List All Anagrams in a Word
The Solution A major hint is in the fact we are given a dictionary. Because we are given this dicti ...
- Hadoop datanode 磁盘自动化处理
新集群的 datanode 磁盘自动化处理工具开工在即.
- New Year Tree 【DFS序+先段数区间查询修改+二进制保存状态】
题目链接[http://codeforces.com/problemset/problem/620/E] 题意:给出n个数,每个数有一个初始的颜色.由这n个数组成一颗树.有两种操作1.将以节点u为根的 ...
- C1FlexGrid小结(转自http://www.cnblogs.com/C1SupportTeam/archive/2012/12/11/2812316.html)
C1FlexGrid控件来对一个表格格式中的数据进行显示,编辑,组和总结.该表格可以绑定到一个数据源,它可以对自己的数据进行管理. C1FlexGrid控件有一个包含以下元素的丰富的对象模型: 以下的 ...