【发布时间】:2011-10-15 14:50:12
【问题描述】:
我正在尝试编写一个程序,该程序将使用 Windows API 根据日期删除一组与特定命名模式(通配符)匹配的文件/文件夹
...
SHFILEOPSTRUCT shFileOpStruct = {
.hwnd = NULL,
.wFunc = processByDate->op,
.pTo = NULL,
.fFlags = FOF_NOCONFIRMATION | FOF_SILENT
};
buildReferenceDate( &refTime, processByDate->nDays );
hFind = FindFirstFile( processByDate->srcFileName, &findFileData );
errorCode = GetLastError();
while ( errorCode == ERROR_SUCCESS ) {
LONG res = CompareFileTime( &refTime, &findFileData.ftCreationTime );
if ( (processByDate->nDays ^ res) > 0 ) {
sprintf( strrchr(processByDate->srcFileName, '\\') + 1, "%s%c",
findFileData.cFileName, '\0');
shFileOpStruct.pFrom = processByDate->srcFileName;
fprintf( stdout, "\n%s\n", shFileOpStruct.pFrom);
fprintf( stdout, "\n0x%x\n", SHFileOperation( &shFileOpStruct ));
}
FindNextFile( hFind, &findFileData );
errorCode = GetLastError();
}
if ( errorCode != ERROR_NO_MORE_FILES )
displayError ( stdout, errorCode );
...
仅删除第一个匹配的文件,因为 FindNextFile 以“句柄无效”终止。显然 SHFileOperation 以某种方式使文件句柄无效(或者至少我认为如此)。我能想到的唯一解决方案是保存匹配的文件/文件夹的名称,然后一一删除。还有其他更简单的解决方案吗?
谢谢
【问题讨论】:
标签: c windows winapi windows-shell