base\files\file_enumerator.h
namespace base {
// A class for enumerating the files in a provided path. The order of the
// results is not guaranteed.
//
// This is blocking. Do not use on critical threads.
//
// Example:
//
// base::FileEnumerator e(my_dir, false, base::FileEnumerator::FILES,
// FILE_PATH_LITERAL("*.txt"));
// for (base::FilePath name = e.Next(); !name.empty(); name = e.Next())
// ...
class BASE_EXPORT FileEnumerator {
public:
// Note: copy & assign supported.
class BASE_EXPORT FileInfo {
public:
FileInfo();
~FileInfo();
bool IsDirectory() const;
// The name of the file. This will not include any path information. This
// is in constrast to the value returned by FileEnumerator.Next() which
// includes the |root_path| passed into the FileEnumerator constructor.
FilePath GetName() const;
int64_t GetSize() const;
// On POSIX systems, this is rounded down to the second.
Time GetLastModifiedTime() const;
#if BUILDFLAG(IS_WIN)
// Note that the cAlternateFileName (used to hold the "short" 8.3 name)
// of the WIN32_FIND_DATA will be empty. Since we don't use short file
// names, we tell Windows to omit it which speeds up the query slightly.
const WIN32_FIND_DATA& find_data() const {
return *ChromeToWindowsType(&find_data_);
}
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
const stat_wrapper_t& stat() const { return stat_; }
#endif
private:
friend class FileEnumerator;
#if BUILDFLAG(IS_WIN)
CHROME_WIN32_FIND_DATA find_data_;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
stat_wrapper_t stat_;
FilePath filename_;
#endif
};
enum FileType {
FILES = 1 << 0,
DIRECTORIES = 1 << 1,
INCLUDE_DOT_DOT = 1 << 2,
// Report only the names of entries and not their type, size, or
// last-modified time. May only be used for non-recursive enumerations, and
// implicitly includes both files and directories (neither of which may be
// specified). When used, an enumerator's `GetInfo()` method must not be
// called.
NAMES_ONLY = 1 << 3,
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
SHOW_SYM_LINKS = 1 << 4,
#endif
};
// Search policy for intermediate folders.
enum class FolderSearchPolicy {
// Recursive search will pass through folders whose names match the
// pattern. Inside each one, all files will be returned. Folders with names
// that do not match the pattern will be ignored within their interior.
MATCH_ONLY,
// Recursive search will pass through every folder and perform pattern
// matching inside each one.
ALL,
};
省略其他........
};
例子:
base::FilePath path;//要遍历的路径
base::FileEnumerator file_enumerator(
path, false, base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.txt"));
for (base::FilePath config_file = file_enumerator.Next();
!config_file.empty();
config_file = file_enumerator.Next()) {
//do sth
}
08-14
2759