获得文件路径:GetFilePath()
获得文件名:GetFileName()
获得文件标题:GetFileTitile()
Path is : "C:\WINDOWS\SYSTEM.INI"
Name is : "SYSTEM.INI"
Title is: "System"
获得文件名:GetFileName()
获得文件标题:GetFileTitile()
Path is : "C:\WINDOWS\SYSTEM.INI"
Name is : "SYSTEM.INI"
Title is: "System"
1.创建文件夹
创建路径上最后的一个文件夹CreateDirectory(%%1,NULL)
创建路径上所有的文件夹
SHCreateDirectoryEx(NULL, strTempPath, NULL);
2.创建文件
CFile file;
file.Open(%%1,CFile::modeCreate|CFile::modeWrite);
3.删除文件
DeleteFile(%%1);
4.删除文件夹
RemoveDirectory(%%1);
5.删除一个文件夹下所有的文件夹
CFileFind finder;
BOOL bWorking = finder.FindFile(%%1+"\\*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
if(finder.IsDirectory()){
dir.Delete(finder.GetFilePath());
}
}
6.清空文件夹
RemoveDirectory(%%1);
CreateDirectory(%%1,NULL)
7.读取文件
char sRead[1024];
CFile mFile(_T(%%1),CFile::modeRead);
while(sRead!=null)
{
mFile.Read(sRead,1024);
CString %%2=CString(sRead);
%%3
}
8.写入文件
mFile.Close();
CFile mFile(_T(%%1), CFile::modeWrite|CFile::modeCreate);
mFile.Write(%%2);
mFile.Flush();
mFile.Close();
9.写入随机文件
char szTempPath[_MAX_PATH],szTempfile[_MAX_PATH];
GetTempPath(_MAX_PATH, szTempPath);
GetTempFileName(szTempPath,_T ("my_"),0,szTempfile);
CFile m_tempFile(szTempfile,CFile:: modeCreate|CFile:: modeWrite);
char m_char='a';
m_tempFile.Write(&m_char,2);
m_tempFile.Close();
//循环写入多个值
strTempA;
int i;
int nCount=6;
file://共有6个文件名需要保存
for(i=0;i {strTemp.Format("%d",i);
strTempA=文件名;
file://文件名可以从数组,列表框等处取得.
::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,
c:\\usefile\\usefile.ini);
}
strTemp.Format("%d",nCount);
::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");
file://将文件总数写入,以便读出.
//读出
nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");
for(i=0;i {strTemp.Format("%d",i);
strTemp="FileName"+strTemp;
::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\\usefile\\usefile.ini");
file://使用strTempA中的内容.
}
10.读取文件属性
dwAttrs = GetFileAttributes(%%1);
if (dwAttrs & FILE_ATTRIBUTE_READONLY)
{
}
if (NORMAL & FILE_ATTRIBUTE_READONLY)
{
}
11.写入属性
SetFileAttributes(szNewPath,dwAttrs | FILE_ATTRIBUTE_READONLY);
12.枚举一个文件夹中的所有文件夹
CFileFind finder;
BOOL bWorking = finder.FindFile(%%1+"\\*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
if(finder.IsDirectory()){
CString %%1=finder.GetFilePath();
%%2
}
}
13.复制文件夹
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = %%2;
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
if (!CreateDirectory(szDirPath, NULL))
{
//不能创建新的目录
return;
}
hSearch = FindFirstFile(%%1+"\\*.*", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
return;
}
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
//不能复制文件
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
//遍历文件夹完成
fFinished = TRUE;
}
else
{
//找不到下一个文件
return;
}
}
}
FindClose(hSearch);
14.复制一个文件夹下所有的文件夹到另一个文件夹下
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = %%2;
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
if (!CreateDirectory(szDirPath,NULL))
{
//不能创建新的目录
return;
}
BOOL bWorking = finder.FindFile(%%1+"\\*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
if(finder.IsDirectory()){
hSearch = FindFirstFile(finder.GetFilePath()+"\\*.*", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
return;
}
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
//不能复制文件
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
//遍历文件夹完成
fFinished = TRUE;
}
else
{
//找不到下一个文件
return;
}
}
}
FindClose(hSearch);
}
}
15.移动文件夹
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = %%2;
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
if (!CreateDirectory(szDirPath, NULL))
{
//不能创建新的目录
return;
}
hSearch = FindFirstFile(%%1+"\\*.*", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
return;
}
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
//不能复制文件
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
//遍历文件夹完成
fFinished = TRUE;
}
else
{
//找不到下一个文件
return;
}
}
}
FindClose(hSearch);
RemoveDirectory(%%1);
16.移动一个文件夹下所有的文件夹到另一个目录下
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = %%2;
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
if (!CreateDirectory(szDirPath,NULL))
{
//不能创建新的目录
return;
}
BOOL bWorking = finder.FindFile(%%1+"\\*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
if(finder.IsDirectory()){
hSearch = FindFirstFile(finder.GetFilePath()+"\\*.*", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
return;
}
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
//不能复制文件
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
//遍历文件夹完成
fFinished = TRUE;
}
else
{
//找不到下一个文件
return;
}
}
}
FindClose(hSearch);
RemoveDirectory(finder.GetFilePath().GetBuffer(0));
}
}
17.以一个文件夹的框架在另一个目录创建文件夹和空文件
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = %%2;
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
if (!CreateDirectory(szDirPath, NULL))
{
//不能创建新的目录
return;
}
hSearch = FindFirstFile(%%1+"\\*.*", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
return;
}
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
HANDLE hFile=CreateFileHandle hFile=CreateFile(szNewPath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);
if(!hFile)
{
//不能创建文件
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
//遍历文件夹完成
fFinished = TRUE;
}
else
{
//找不到下一个文件
return;
}
}
}
FindClose(hSearch);
18.复制文件
CopyFile(%%1,%%2,true)
19.复制一个文件夹下所有的文件到另一个目录
//#include <string>
using std::string;
char sep='/';
#ifdef _WIN32
sep='\\';
#endif
CFileFind finder;
BOOL bWorking = finder.FindFile(%%1+"\\*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
if(!finder.IsDirectory() || finder.IsDots()){
string s(finder.GetFileName());
CString sourcefile(%%1);
if(s.rfind(sep,s.length())!=string::npos)
{
sourcefile=sourcefile+"\\"+s.substr(i+1,s.length()-i);
CString targetfile(s.substr(i+1,s.length()-i));
targetfile=%%2+"\\"+targetfile;
CopyFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true);
}
}
}
20.提取扩展名
//#include <string>
using std::string;
string s(%%1);
size_t i=s.rfind('.',s.length());
if(i!=string::npos)
CString %%2(s.substr(i+1,s.length()-i));
else
CString %%2="";
21.提取文件名
//#include <string>
using std::string;
string s(%%1);
char sep='/';
#ifdef _WIN32
sep='\\';
#endif
size_t i=s.rfind(sep,s.length());
if(i!=string::npos)
CString %%2(s.substr(i+1,s.length()-i));
else
CString %%2="";
22.提取文件路径
//#include <string>
using std::string;
string s(%%1);
char sep='/';
#ifdef _WIN32
sep='\\';
#endif
size_t i=s.rfind(sep,s.length());
if(i!=string::npos)
CString %%2(s.substr(0,i));
else
CString %%2="";
23.替换扩展名
//#include <string>
using std::string;
string s(%%1);
string newExt(%%2);
string::size_type i=s.rfind('.',s.length());
if(i!=string::npos)
s.replace(i+1,newExt.length(),newExt);
CString %%3(s);
24.追加路径
//#include <string>
//#include <cstdlib>
//#include <boost/filesystem/operations.hpp>
//#include <boost/filesystem/fstream.hpp>
using namespace std;
using namespace boost::filesystem;
try {
path p1=complete(path(%%2,native),
path(%%1,native));
path p2=system_complete(path(%%2,native));
CString %%3(p3);
}
catch(exception& e){
//e.what();
}
25.移动文件
MoveFile(%%1,%%2);
26.移动一个文件夹下所有文件到另一个目录
//#include <string>
using std::string;
char sep='/';
#ifdef _WIN32
sep='\\';
#endif
CFileFind finder;
BOOL bWorking = finder.FindFile(%%1+"\\*.*");
while (bWorking)
{
bWorking = finder.FindNextFile();
if(!finder.IsDirectory() || finder.IsDots()){
string s(finder.GetFileName());
CString sourcefile(%%1);
if(s.rfind(sep,s.length())!=string::npos)
{
sourcefile=sourcefile+"\\"+s.substr(i+1,s.length()-i);
CString targetfile(s.substr(i+1,s.length()-i));
targetfile=%%2+"\\"+targetfile;
MoveFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true);
}
}
}
27.指定目录下搜索文件
CString strFileTitle;
CFileFind finder;
BOOL bWorking = finder.FindFile ("C:\\windows\\sysbkup\\*.cab");
while(bWorking)
{
bWorking=finder.FindNextFile();
strFileTitle=finder.GetFileTitle();
}