python版本:
import os
import shutil
def deleteF(path, fileName):
for files in os.listdir(path):
tmpPath = os.path.join(path, files)
filePath = os.path.join(path, fileName)
if os.path.isdir(tmpPath):
deleteF(tmpPath, fileName)
elif os.path.isfile(filePath):
os.remove(filePath)
print "deleted file in " + files
deleteF('E:\\py\\test', 'back1.bmp')
c++版本:
#include <stdio.h>
#include <iostream>
#include <io.h>
#include <string>
using namespace std;
void dir(string path)
{
long hFile = 0;
struct _finddata_t fileInfo;
string pathName, exdName;
if ((hFile = _findfirst(pathName.assign(path).append("\\*").c_str(), &fileInfo)) == -1) {
cout << "error no file!" << endl;
return;
}
do
{
cout << fileInfo.name << (fileInfo.attrib&_A_SUBDIR ? "[folder]" : "[file]") << endl;
if (fileInfo.attrib&_A_SUBDIR) {
{
if (strcmp(fileInfo.name, ".") != 0 && strcmp(fileInfo.name, "..") != 0) {
string tmp;
tmp = path + "\\" + fileInfo.name;
dir(tmp);
}
}
}
else {
if (strcmp(fileInfo.name, ".") != 0 && strcmp(fileInfo.name, "..") != 0) {
if (strcmp(fileInfo.name, "back1.bmp")) {
string delpath = path + "\\" + fileInfo.name;
if (remove(delpath.c_str()) != 0)
perror("Error deleting file");
else {
cout << fileInfo.name << "deleted" << endl;
}
}
}
}
} while (_findnext(hFile, &fileInfo) == 0);
_findclose(hFile);
return;
}
int main()
{
string path = "E:\\inpainting\\pics";
dir(path);
system("pause");
return 0;
}