删除指定文件夹里的文件(1)

删除指定文件夹里的文件

本博客为本人原创,转载请注明转载出处!!!!!!本博客链接

  • 可以用c/c++
  • 可以用Windows API
  • 可以用Windows API与c/c++结合使用

c与Windows结合使用 删除D:\WritePics下的所有的.png文件

#include "io.h"
#include <windows.h>
#include <stdio.h>
using namespace std;
const char *to_research = "D:\\WritePics\\*.png";                          //欲查找的文件;
#define OK 0
#define ERR -1
int main()
{
    long handle;
    struct _finddata_t fileinfo;                                    //存储文件信息的结构体;
    handle = _findfirst(to_research, &fileinfo);
    char file[80];
    sprintf(file, "%s%s", "D:\\WritePics\\", fileinfo.name);
    if (remove(file) == 0)
        printf("Removed %s.\n", file);
    else
        perror("remove");

    while (!_findnext(handle, &fileinfo))
    {
        printf("%s\n", fileinfo.name);
        sprintf(file, "%s%s", "D:\\WritePics\\", fileinfo.name);
        if (remove(file) == 0)
            printf("Removed %s.\n", file);
        else
            perror("remove");
    }
    _findclose(handle); //关闭句柄;
    system("pause");
    return OK;
}

你可能感兴趣的:(windows,api,C语言)