xtd 0.2.0
Loading...
Searching...
No Matches

◆ enumerate_files() [1/2]

static xtd::io::directory::file_iterator xtd::io::directory::enumerate_files ( const xtd::string path)
static

Returns an enumerable collection of full file names in a specified path.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
An xtd::io::directory::directory_iterator of the full names (including paths) for the files in the directory specified by path.
Exceptions
xtd::io::io_exceptionThe directory specified by path is a file.
xtd::argument_exceptionpath is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the xtd::io::path::get_invalid_path_chars method.
xtd::io::path_too_long_exceptionThe specified path, file name, or both exceed the system-defined maximum length.
xtd::io::directory_not_found_exceptionThe specified path is invalid (for example, it is on an unmapped drive).
xtd::not_supported_exceptionpath contains a colon character (:) that is not part of a drive label ("C:\"). @par Examples The following example shows how to retrieve all the text files from a directory and move them to a new directory. After the files are moved, they no longer exist in the original directory. @icode{cpp} #include <xtd/xtd> using namespace xtd; using namespace xtd::io; class program { public: static auto main() { string source_directory = R"(C:\current)"; string archive_directory = R"(C:\archive)";

try { auto txt_files = directory::enumerate_files(source_directory);

for (string current_file : txt_files) { string file_name = current_file.substring(source_directory.size() + 1); directory::move(current_file, path::combine(archive_directory, file_name)); } } catch (exception& e) { console::write_line(e.message()); } } };

startup_(program::main);

Remarks
You can specify relative path information with the path parameter. Relative path information is interpreted as relative to the current working directory, which you can determine by using the xtd::io::directory::get_current_directory method.
The xtd::io::directory::enumerate_files and xtd::io::directory::get_files methods differ as follows: When you use xtd::io::directory::enumerate_files, you can start enumerating the collection of names before the whole collection is returned. When you use xtd::io::directory::get_files, you must wait for the whole array of names to be returned before you can access the array. Therefore, when you are working with many files and directories, xtd::io::directory::enumerate_files can be more efficient.