遍历文件,用FindFirstFile或者_findfirstfile及其配套的函数,下面是一个简单的例子
//.h
struct file_tree {
public:
file_tree();
int find_first(const char *path);
int find_next();
int is_file();
int is_folder();
FILETIME get_create_time();
FILETIME get_last_modified_time();
FILETIME get_last_access_time();
const char *get_name();
~file_tree();
private:
HANDLE handle;
WIN32_FIND_DATA fd;
};
//.cpp
file_tree::file_tree() :handle(INVALID_HANDLE_VALUE) {
}
int file_tree::find_first(const char * path) {
if (handle != INVALID_HANDLE_VALUE) {
FindClose(handle);
}
static char all[MAX_FILE_PATH_LEN + 5];
sprintf(all, "%s\\*", path);
handle = FindFirstFile(all, &fd);
return 0;
}
int file_tree::find_next() {
return FindNextFile(handle, &fd);
}
int file_tree::is_file() {
return (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0;
}
int file_tree::is_folder() {
return fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
}
FILETIME file_tree::get_create_time() {
return fd.ftCreationTime;
}
FILETIME file_tree::get_last_modified_time() {
return fd.ftLastWriteTime;
}
FILETIME file_tree::get_last_access_time() {
return fd.ftLastAccessTime;
}
const char * file_tree::get_name() {
return fd.cFileName;
}
file_tree::~file_tree() {
if (handle != INVALID_HANDLE_VALUE)FindClose(handle);
}
然后使用递归,
void doFolder(){
file_tree ft;
ft.find_first(data->from);
ft.find_next();
while (ft.find_next()) {
if (ft.is_file()) {
doFile();
} else if (ft.is_folder()) {
doFolder();
} else {
//TODO
}
}
}