使用MFC c ++删除已安装驱动器的文件

时间:2017-07-18 07:57:23

标签: c++ winapi

I want to delete first 10 files of the mounted drive. This drive is Unix system drive. I have written code which working fine for local drive but not mounted drive. Its deleting randomly but not sequentially.我在MFC C ++中编写了代码。如果有人知道解决方案,请告诉我。代码如下所示。

char fileFound[256];
WIN32_FIND_DATA info;
HANDLE hp=INVALID_HANDLE_VALUE;
int count=10;
swprintf_s(fileFound,256,"%s\\*.*","G:\\foldername");
hp=FindFirstFile(fileFound,&info);
do
{
    swprintf_s(fileFound,256,"%s\\%s","G:\foldername",info.cFileName);
    DeleteFile(fileFound);
    count--;
}while(FindNextFile(hp,&info)&&count);
FindClose(hp);

1 个答案:

答案 0 :(得分:0)

  

随机删除但不按顺序删除。

此行为为documented

  

[...] FindFirstFile不会对搜索结果进行排序。

以及here

  

搜索返回文件的顺序(例如字母顺序)无法保证,并且取决于文件系统。如果必须对数据进行排序,则应用程序必须在获得所有结果后进行排序。

如果需要从一组文件中删除第一个 n 文件,则需要收集整个文件集,根据任意谓词对集合进行排序,然后执行操作第一个 n 项目。