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

◆ get_file_system_infos() [2/2]

std::vector< xtd::sptr< xtd::io::file_system_info > > xtd::io::directory_info::get_file_system_infos ( const xtd::string search_pattern) const

Retrieves an array of strongly typed FileSystemInfo objects representing the files and subdirectories that match the specified search criteria.

Parameters
search_patternThe search string to match against the names of directories and files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters, but it doesn't support regular expressions.
Returns
An array of strongly typed xtd::io::file_system_info entries.
Exceptions
xtd::io::directory_not_found_exceptionThe specified path is invalid, such as being on an unmapped drive.
xtd::security::security_exceptionThe caller does not have code access permission to create the directory.
Examples
The following example counts the files and directories that match the specified search pattern.
#include <xtd/xtd>
using namespace xtd;
using namespace xtd::io;
class program {
public:
static auto main() {
try {
console::write_line("Enter the path to a directory:");
string directory = console::read_line();
console::write_line("Enter a search string (for example *p*):");
string search_string = console::read_line();
// Create a new directory_info object.
if (!dir.exists()) {
throw directory_not_found_exception("The directory does not exist.", csf_);
}
// Call the GetFileSystemInfos method.
std::vector<xtd::sptr<file_system_info>> infos = dir.get_file_system_infos(search_string);
console::write_line("Working...");
// Pass the result to the list_directories_and_files
// method defined below.
list_directories_and_files(infos, search_string);
// Display the results to the console.
console::write_line("Directories: {0}", directories);
console::write_line("Files: {0}", files);
} catch (const exception& e) {
console::write_line(e.message());
}
}
private:
static void list_directories_and_files(std::vector<xtd::sptr<file_system_info>> fs_info, const string& search_string) {
// Iterate through each item.
for (xtd::sptr<file_system_info> i : fs_info) {
// Check to see if this is a directory_info object.
if (is<directory_info>(i)) {
// Add one to the directory count.
directories++;
// Cast the object to a directory_info object.
xtd::sptr<directory_info> d_info = as<directory_info>(i);
// Iterate through all sub-directories.
list_directories_and_files(d_info->get_file_system_infos(search_string), search_string);
}
// Check to see if this is a FileInfo object.
else if (is<file_info>(i)) {
// Add one to the file count.
files++;
}
}
}
inline static long files = 0;
inline static long directories = 0;
};
startup_(program::main);
Defines the base class for predefined exceptions in the xtd namespace.
Definition exception.h:26
Exposes instance methods for creating, moving, and enumerating through directories and subdirectories...
Definition directory_info.h:129
The exception that is thrown when part of a file or directory cannot be found.
Definition directory_not_found_exception.h:29
Exposes static methods for creating, moving, and enumerating through directories and subdirectories....
Definition directory.h:101
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.h:175
#define csf_
Provides information about the current stack frame.
Definition current_stack_frame.h:30
std::shared_ptr< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.h:25
@ e
The E key.
The xtd::io namespace contains types that allow reading and writing to files and data streams,...
Definition binary_reader.h:16
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Remarks
search_pattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions. The following wildcard specifiers are permitted in search_pattern.
Wildcard specifier Matches
* (asterisk) Zero or more characters in that position.
 ? (question mark) Zero or one character in that position.
Characters other than the wildcard are literal characters. For example, the string "*t" searches for all names in ending with the letter "t". ". The search_pattern string "s*" searches for all names in path beginning with the letter "s".
The xtd::io::directory_info::enumerate_file_system_infos and xtd::io::directory_info::get_file_system_infos methods differ as follows:
Therefore, when you are working with many files and directories, xtd::io::directory_info::enumerate_file_system_infos can be more efficient.
This method pre-populates the values of the following xtd::io::file_system_info properties: