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

◆ get_files() [1/2]

static std::vector< xtd::string > xtd::io::directory::get_files ( const xtd::string path)
static

Returns the names of files (including their paths) in the specified directory.

Parameters
pathThe relative or absolute path to the directory to search. This string is not case-sensitive.
Returns
An array of the full names (including paths) for the files in the specified directory, or an empty array if no files are found.
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 demonstrates how to use the GetFiles method to return file names from a user-specified location. The example is configured to catch all errors common to this method. @icode{cpp} #include <xtd/xtd> using namespace xtd; using namespace xtd::collections::generic; using namespace xtd::io; class program { public: static auto main(const std::vector<list>& args) { for (string path : args) { if (file::exists(path)) { // This path is a file process_file(path); } else if(directory::exists(path)) { // This path is a directory process_directory(path); } else { console::write_line("{0} is not a valid file or directory.", path); } } } // Process all files in the directory passed in, recurse on any directories // that are found, and process the files they contain. static void process_directory(const string& target_directory) { // Process the list of files found in the directory. std::vector<string> file_entries = directory::get_files(target_directory); for (string file_name : file_entries) process_file(file_name); // Recurse into subdirectories of this directory. std::vector<string> subdirectory_entries = directory::get_directories(target_directory); for (string subdirectory : subdirectory_entries) process_directory(subdirectory); } // Insert logic for processing found files here. static void process_file(const string& path) { console::write_line("Processed file '{0}'.", path); } }; startup_(program::main); @endicode @remarks 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. @remarks The returned file names are appended to the supplied path parameter. @remarks This method is identical to xtd::io::directory::get_files(string, string) with the asterisk (*) specified as the search pattern. @remarks The path parameter can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see xtd::io::directory::get_current_directory. @remarks The order of the returned file names is not guaranteed; use the std:::sort method if a specific sort order is required. @remarks The path parameter is not case-sensitive. @remarks For a list of common I/O tasks, see <a href="https://gammasoft71.github.io/xtd/docs/documentation/Guides/xtd.core/Common%20I%3AO%20tasks" >Common I/O Tasks.