BOOL CZip::RestoreFileName(CString sDirectory)
{
CFileFind ff;
BOOL bFound = ff.FindFile(sDirectory + _T("\\*.*"));
TCHAR newName[260];
CString sNoteDirectory = sDirectory;
sNoteDirectory += _T("\\note.ini");
CString oldName, oldPath;
int i = 0;
while (bFound)
{
bFound = ff.FindNextFile();
if (ff.IsDots())
{
continue;
}
if (ff.IsDirectory())
{
oldName = ff.GetFilePath();
RestoreFileName(oldName);
}
else
{
oldName = ff.GetFileName();
if (oldName == _T("note.ini"))
{
continue;
}
oldPath = ff.GetFilePath();
GetPrivateProfileString(_T("note"),oldName,_T(""),newName,256,sNoteDirectory);
if (!_tcscmp(newName, _T("")))
{
return TRUE;
}
CFile::Rename(oldPath,sDirectory+_T("\\")+newName);
}
}
return TRUE;
}
BOOL CZip::ModifyFileName(CString sDirectory)
{
CFileFind ff;
BOOL bFound = ff.FindFile(sDirectory + _T("\\*.*"));
int i = 0;
CString sNoteDirectory = sDirectory;
sNoteDirectory += _T("\\note.ini");
CString oldPath, oldName, sExtension, newPath,newName;
int pos, iCount;
while (bFound)
{
bFound = ff.FindNextFile();
if (ff.IsDots())
{
continue;
}
if (ff.IsDirectory())
{
oldPath = ff.GetFilePath();
ModifyFileName(oldPath);
}
else
{
oldName = ff.GetFileName();
oldPath = ff.GetFilePath();
pos = oldName.Find('.');
sExtension = oldName.Mid(pos);
srand(i);
i++;
iCount = 100 + rand()%(1000-100+1);
if (oldName == _T("note.ini"))
{
continue;
}
newName.Format(_T("%d%s"),iCount,sExtension);
newPath = sDirectory +_T("\\")+ newName;
CFile::Rename(oldPath,newPath);
WritePrivateProfileString(_T("note"), newName,oldName, sNoteDirectory);
}
}
return TRUE;
}
写了两个方法,为了修改文件名和还原文件名。不过我遇到了一个问题,就是在还原文件名的时候当文件超过35个的时候,FindNextFile就找不到文件末尾了,导致我note.ini文件里面没有记录这个键值,得到了空值,然后在修改文件名的时候出现异常。
if (!_tcscmp(newName, _T("")))
{
return TRUE;
}我这样是为了不出现异常情况,但是我想知道,为什么文件超过那个数量的是FindNextFile会出现找不到文件结尾的情况。好愁
最后一个返回0