使用 SHFileOperation 按日期删除文件和文件夹
我正在尝试编写一个程序,该程序将使用 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 以“The句柄无效。”显然 SHFileOperation 以某种方式使文件句柄无效(或者至少我认为如此)。我能想到的唯一解决方案就是保存匹配的文件/文件夹的名称并一一删除。还有其他更简单的解决方案吗?
谢谢
I'm trying to write a program that will delete a set of files/folders that are matching a specific naming pattern (wild cards) based on their dates using the 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 );
...
Only the first matching file is deleted, because FindNextFile terminates with "The handle is invalid." apparently SHFileOperation somehow invalidates the file handle (or at least so I suppose). The only solution I can think of is to save the name of the matching files/folders and delete them one by one. Is there any other simpler solution?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是错误的。 仅当 FindNextFile() 返回 FALSE 时才调用 GetLastError()。使固定:
That's wrong. Only call GetLastError() when you get a FALSE return from FindNextFile(). Fix:
当函数成功时,不会设置线程的最后一个错误代码。您必须检查
FindNextFile
的返回值,而不是调用GetLastError
。FindNextFile 函数
循环应如下所示:
The thread's last error code is not set when a function succeeds. Instead of calling
GetLastError
, you have to check the return value ofFindNextFile
.FindNextFile function
The loop should look like this: