查找文件删除

1)
_finddata_t fileinfo;
    if ((hFile=_findfirst(filespec,&fileinfo)) != -1)
    {
        do
        {
            //检查是不是目录
            //如果不是,则进行处理
            if (!(fileinfo.attrib & _A_SUBDIR))
            {
            }
        } while (_findnext(hFile,&fileinfo) == 0);
        _findclose(hFile);
    }


2)

void Application::DeleteDiagnoseFiles()
{
	TCHAR tcFileDir[MAX_PATH] = {0};
	FileSystemHandler::getAbsolutePath(tcFileDir);

	TCHAR tcFilePath[MAX_PATH] = {0};
	_stprintf(tcFilePath, TEXT("%s\\diagnose*.*"), tcFileDir);
	
	WIN32_FIND_DATA wfd = {0};
	HANDLE hFind = FindFirstFile(tcFilePath, &wfd);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (wfd.cFileName[0] == TEXT('.'))
			{

			}
			else
			{
				CHAR cFilePath[MAX_PATH]={0};
				_stprintf(tcFilePath, TEXT("%s\\%s"), tcFileDir, wfd.cFileName);
				//ListView::TC2C(tname,cname,sizeof(cname)/sizeof(cname[0]));
				EncodingHandler::WCharToMByte(tcFilePath,cFilePath,sizeof(cFilePath)/sizeof(cFilePath[0]));
				remove(cFilePath);
			}
		}
		while (FindNextFile(hFind, &wfd));
	}
	FindClose(hFind);

}

(返回的文件名中会包含"."和".."。“.'代表本目录,".."代表上一层目录。一般情况下需要把这两个名称过滤掉。比如要进行文件删除操作)

3)
void LanguageManager::FindI18nFile(LPCTSTR ptcFileDir)
{
	TCHAR tcFilePath[MAX_PATH] = {0};
	_stprintf(tcFilePath, TEXT("%s\\*.*"), ptcFileDir);
	WIN32_FIND_DATA wfd = {0};
	HANDLE hFind = FindFirstFile(tcFilePath, &wfd);
	if (hFind != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (wfd.cFileName[0] == TEXT('.'))
			{
				
			}
			else if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
			{
				TCHAR tcFileDir[MAX_PATH] = {0};
				_stprintf(tcFileDir, TEXT("%s\\%s"), ptcFileDir, wfd.cFileName);
				this->FindI18nFile(tcFileDir);
			}
			else
			{
				LPTSTR ptcLastPoint = _tcsrchr(wfd.cFileName, TEXT('.'));
				if ((ptcLastPoint != NULL) && (0 == _tcsicmp(ptcLastPoint, TEXT(".xml"))))
				{
					TCHAR tcFilePath[MAX_PATH];
					_stprintf(tcFilePath, TEXT("%s\\%s"), ptcFileDir, wfd.cFileName);
				}
			}
		}
		while (FindNextFile(hFind, &wfd));
	}
	FindClose(hFind);
}

你可能感兴趣的:(文件)