给你一段代码参考一下:
// 删除15天之前的log日志文件
void DeleteLogFile( LPCTSTR strLogPath )
{
if ( !PathFileExists( strLogPath ) )
{
return;
} time_t tCurTime = time( NULL ); // 获取当前时间 tstring strFindFileName = strLogPath;
strFindFileName += _T("\\*.*");
WIN32_FIND_DATA wfd;
HANDLE hFindFile = FindFirstFile( strFindFileName.c_str(), &wfd );
if ( hFindFile == INVALID_HANDLE_VALUE )
{
return;
} while ( true )
{
if ( wfd.cFileName[0] != _T('.') )
{// 非本级或上级目录
if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) // 目录
{
if ( !FindNextFile( hFindFile, &wfd ) )
{
break;
}
continue;
}
else if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM ) // 系统文件,不处理
{
if ( !FindNextFile( hFindFile, &wfd ) )
{
break;
}
continue;
}
else // 用户日志文件
{
tstring strLogFile = strLogPath;
strLogFile += _T("\\");
strLogFile += wfd.cFileName; time_t tModifyTime = GetFileModifyTime( strLogFile.c_str() ); // 如果是15天之前的文件,则将之删除掉(拿当前时间和文件的最后修改时间作比较)
if ( tCurTime - tModifyTime > DELETE_INTERVAL_SECOND )
{
DeleteFile( strLogFile.c_str() );
}
}
} if ( !FindNextFile( hFindFile, &wfd ) )
{
break;
}
}; FindClose( hFindFile );
}
// 删除15天之前的log日志文件
void DeleteLogFile( LPCTSTR strLogPath )
{
if ( !PathFileExists( strLogPath ) )
{
return;
} time_t tCurTime = time( NULL ); // 获取当前时间 tstring strFindFileName = strLogPath;
strFindFileName += _T("\\*.*");
WIN32_FIND_DATA wfd;
HANDLE hFindFile = FindFirstFile( strFindFileName.c_str(), &wfd );
if ( hFindFile == INVALID_HANDLE_VALUE )
{
return;
} while ( true )
{
if ( wfd.cFileName[0] != _T('.') )
{// 非本级或上级目录
if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) // 目录
{
if ( !FindNextFile( hFindFile, &wfd ) )
{
break;
}
continue;
}
else if ( wfd.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM ) // 系统文件,不处理
{
if ( !FindNextFile( hFindFile, &wfd ) )
{
break;
}
continue;
}
else // 用户日志文件
{
tstring strLogFile = strLogPath;
strLogFile += _T("\\");
strLogFile += wfd.cFileName; time_t tModifyTime = GetFileModifyTime( strLogFile.c_str() ); // 如果是15天之前的文件,则将之删除掉(拿当前时间和文件的最后修改时间作比较)
if ( tCurTime - tModifyTime > DELETE_INTERVAL_SECOND )
{
DeleteFile( strLogFile.c_str() );
}
}
} if ( !FindNextFile( hFindFile, &wfd ) )
{
break;
}
}; FindClose( hFindFile );
}
CString strFile;
CFileFind find;
BOOL bWorking = find.FindFile("C:/windows/*.txt");//这是搜索C:/WIndows的txt
while(bWorking)
{
bWorking=find.FindNextFile();
strFile=find.GetFileName();
}
2楼已经给出代码了,可以参考
BOOL bFind = finder.FindFile(m_filepath);
if(bFind!=0)
{
CStdioFile LogFile;
char* old_locale=_strdup( setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE,"chs");
CString strRow;
LogFile.Open(m_filepath,CFile::modeRead);
while(LogFile.ReadString(strRow))
{ m_strModelContent=m_strModelContent+strRow+_T("\n");
}
setlocale( LC_CTYPE, old_locale ); //还原语言区域的设置
free( old_locale );//还原区域设定
}加头文件 #include <locale.h>访问中文字符
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
#include <tchar.h>int _tmain(int argc, TCHAR *argv[])
{
HANDLE hFind = INVALID_HANDLE_VALUE;
TCHAR szPath[] = TEXT("C:\\Windows");
TCHAR szFind[MAX_PATH] = { 0 };
WIN32_FIND_DATA wfd = { 0 };
PathCombine(szFind, szPath, TEXT("*.exe"));
hFind = FindFirstFile(szFind, &wfd);
if (hFind != INVALID_HANDLE_VALUE) {
TCHAR szFile[MAX_PATH] = { 0 };
do {
PathCombine(szFile, szPath, wfd.cFileName);
_tprintf(TEXT("%s\n"), szFile);
} while (FindNextFile(hFind, &wfd));
FindClose(hFind);
}
return 0;
}
//读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字
system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt");
//读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录
system("dir /b /ad c:\\*.* >d:\\alldirs.txt");
//读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字
请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。
void CTestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
CFileFind finder;
CString m_strModelContent="";
BOOL bFind = finder.FindFile("D:/abc/*.txt");
if(bFind!=0)
{
CStdioFile LogFile;
CString filepath=finder.GetFilePath();
char* old_locale=_strdup( setlocale(LC_CTYPE,NULL) );
setlocale( LC_CTYPE,"chs");
CString strRow;
LogFile.Open(filepath,CFile::modeRead);
while(LogFile.ReadString(strRow))
{ m_strModelContent=m_strModelContent+strRow+_T("\n");
}
setlocale( LC_CTYPE, old_locale ); //还原语言区域的设置
free( old_locale );//还原区域设定
}
GetDlgItem(IDC_EDIT1)->SetWindowText(m_strModelContent);
}
void CTestDlg::OnButton1()
{
CString strFile;
CFileFind find;
CString sFileName;
CStdioFile out;
HANDLE h;
WIN32_FIND_DATA p;
BOOL bWorking = find.FindFile("D:/abc/*.txt");
while (bWorking)
{
bWorking=FindNextFile(h,&p);
sFileName=find.GetFilePath();
out.Open(sFileName, CFile::modeRead);
CString sSql="",s; //读取文件
do{ out.ReadString(s); sSql=sSql+s+(char)10; }
while (out.GetPosition()!=out.GetLength());
out.Close();
GetDlgItem(IDC_EDIT1)->SetWindowText(sSql);
}}
{
CString strFile;
CFileFind find;
CString sFileName;
CStdioFile out;
HANDLE h;
WIN32_FIND_DATA p;
BOOL bWorking = find.FindFile("D:/abc/*.txt");
while (bWorking)
{
bWorking=FindNextFile(h,&p);
sFileName=find.GetFilePath();
out.Open(sFileName, CFile::modeRead);
CString sSql="",s; //读取文件
do{ out.ReadString(s); sSql=sSql+s+(char)10; }
while (out.GetPosition()!=out.GetLength());
out.Close();
GetDlgItem(IDC_EDIT1)->SetWindowText(sSql);
}}
test1Dlg.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall CTest1Dlg::RecurseFiles(char const *,char const *,struct HWND__ *)" (?RecurseFiles@CTest1Dlg@@MAEXPBD0PAUHWND__@@@Z)
不需要、