一、文件夹的创建

 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下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)的更多相关文章

  1. Linux基础------文件打包解包---tar命令,文件压缩解压---命令gzip,vim编辑器创建和编辑正文件,磁盘分区/格式化,软/硬链接

    作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group > /1.txt2) 将用户信息数据库文件和用户 ...

  2. linux批量复制或删除同命名规则的文件

    如图所示,有多个不同后缀的文件,但他们的前缀都是"QC_TZ.impute." 此时想批量复制图中的文件的话,可以考虑用命令行 cp QC_TZ.impute.* /your/de ...

  3. Java基础之写文件——创建通道并且写文件(TryChannel)

    控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中. import static java.nio.file.StandardOpenOption.*; import java.nio.c ...

  4. C# 对文件与文件夹的操作包括删除、移动与复制

    在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类.文件夹(Folder)是只在Wind ...

  5. 无法删除 NTFS 盘上的文件或文件夹(对Windows文件的各种情况有比较详细的描述)

    简介 本文介绍您可能无法删除 NTFS 文件系统卷上的文件或文件夹的原因,以及如何分析造成此问题的不同原因从而解决此问题. 更多信息 注意:在内部,NTFS 将文件夹作为特殊类型的文件进行处理.因此, ...

  6. DriverStore文件夹特别大,能删除吗?

    DriverStore文件夹特别大,能删除吗? DriverStore\FileRepository文件夹特别大,能删除吗? C:\Windows\System32\DriverStore\FileR ...

  7. [转载]DriverStore文件夹特别大,能删除吗?

    [转载]DriverStore文件夹特别大,能删除吗? 转自博客园https://www.cnblogs.com/lovebing/p/6951833.html 这篇文章,清理完C盘多了20G!不要太 ...

  8. MFC下MCI的使用播放音乐

    最近研究了一下MFC下的音乐的播放,主要使用了MCI 1.需要包含的库文件 在链接资源里(link)添加库文件VFW32.lib winmm.lib 2.包含的头文件 #include <mms ...

  9. C++ 文件的复制、删除、重命名

    一.文件的复制 #include <iostream>#include <fstream>using namespace std; int CopyFile(char *Sou ...

随机推荐

  1. Chapter 2 Open Book——34

    His gaze became appraising. "You put on a good show," he said slowly. 他的凝视变成了评价.“你上演了一场好戏” ...

  2. reids配置参数详解

    转自:http://www.jb51.net/article/60627.htm reids配置参数详解 #daemonize no  默认情况下, redis 不是在后台运行的,如果需要在后台运行, ...

  3. python入门必备知识总结

    人生苦短,我用python.看图说话 一.python简介与发展: python 是一种面向对象的解释性计算机程序设计语言. python由荷兰人Guido van Rossum 于1989年发明. ...

  4. Javascript 数字保留2位小数

    整理使用Javascript函数将数值保留两位小数: 1.num.toFixed(2) //进位 2.(Math.round(num * 100) / 100).toFixed(2) //进位 3.( ...

  5. Mssql 行转列

    ) set @sql='' --初始化变量@sql --变量多值赋值 ,,'')--去掉首个',' set @sql=' select * from( select objectid,name,jyj ...

  6. installshield学习笔记

    看预定义常量的值:SprintfBox(INFORMATION,"","%d",变量值);     自定义常量:#define MAXCOUNT 1000;#d ...

  7. java学习 (2)xml操作 SAX(增、删、改、查)

    sax是事件驱动的,sax是一种推模式 SAX常用事件: startDocument()----文档开始事件 startElement()-----元素开始事件 charElement()----文本 ...

  8. Android:Asmack能登录但是获取不到联系人的问题

    先说一下:换了N个jar包,换了N个OpenFire,就是获取不到联系人.但是能登录.特别纳闷,百度不到,google一下,有人隐约说了下权限的问题. 终于搞出来了,总结一下,浪费了一整天的时间.(码 ...

  9. emoji Unicode characters

    http://www.easyapns.com/iphone-emoji-alerts he complete list of iPhone emoji Unicode characters. Jus ...

  10. android 5.0 -- Ripple 效果

    Ripple 水波纹效果,也就是涟漪效果. 波纹效果有两种: 1,波纹有边界:波纹涟漪效果只是显示在控件内部 android:background="?android:attr/select ...