16,472
社区成员
发帖
与我相关
我的任务
分享
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);
}
#include <stdio.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;
}
CFileFind finder;
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>访问中文字符void RecurseFiles(LPCTSTR lpszDir, LPCTSTR lpszExtension, HWND hEdit)
{
ASSERT(NULL != lpszDir);
ASSERT(NULL != lpszDir);
ASSERT(NULL != hEdit);
CFileFind finder;
// build a string with wildcards
CString strWildcard(lpszDir);
strWildcard += _T("\\*.*");
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
while (bWorking)
{
bWorking = finder.FindNextFile();
if (finder.IsDots())
{
continue;
}
CString strPath(finder.GetFilePath());
if(finder.IsDirectory())
{
RecurseFiles(strPath, lpszExtension, hEdit);
}
else
{
if(0 == strPath.Right(_tcslen(lpszExtension)).CompareNoCase(lpszExtension))
{
strPath += _T("\r\n");
SendMessage(hEdit, EM_SETSEL, -1, -1);
SendMessage(hEdit, EM_REPLACESEL, 0, (LPARAM)(LPCTSTR)strPath);
}
}
}
finder.Close();
}
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);
}
}
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);
}
}
// 删除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 );
}