1)文件/文件夹是否存在
添加头文件:
#include <shlwapi.h>
#pragma comment(lib,"Shlwapi.lib")
PathFileExists(CString strFileName)
2)文件夹
创建文件夹:CreateDirectory()
删除文件夹:ReMoveDirectory()
3)文件路径的存储问题
例如:test\\test.cpp文件
如果需要保存在一个CString类型或者
一个char数组,需要添加多一个\
如下:
char* pdbName = "test\\\test.cpp"
否则运行的查看显示如下:
test\test.cpp
注意:实际上可以通过/,来避免上述问题的产生
修改文件名称
CString strOldName= _T("D:\\old.txt");
CString strNewName= _T("D:\\new.txt");
CFile::Rename(strOldName,strNewName);
需求说明:文件夹中的文件一律替换成小写
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
void
TransferName(CString strDirPath)
{
CFileFind finder;
CString path;
path.Format(_T(
"%s/*.*"
),strDirPath);
BOOL bWorking = finder.FindFile(path);
while
(bWorking)
{
bWorking = finder.FindNextFile();
if
(finder.IsDirectory() && !finder.IsDots())
{
//处理文件夹
TransferName(finder.GetFilePath());
//递归文件夹
}
else
{
//转换文件名称大小写
if
(!finder.IsDots())
{
CString strOldName = finder.GetFilePath();
CString strNewName = finder.GetFilePath();
strNewName.MakeLower();
CFile::Rename(strOldName,strNewName);
}
}
}
}
调用过程:
CString strDir = _T(
"d:\\log"
);
TransferName(strDir);
|
其中的关键点:
1.
1
|
if
(!finder.IsDots())
|
表示当前的工作目录
本文转自fengyuzaitu 51CTO博客,原文链接:http://blog.51cto.com/fengyuzaitu/1955672,如需转载请自行联系原作者