You'd have to pass the list of files in the desired directory through a function that tests whether their filename (a string of characters, e.g. "myFileName.txt") contains a sub-string of characters that you specify (following your example, ".txt". To make your filetype filter more specific you'd also have to ascertain that ".txt" constitutes the last characters in the given filename, otherwise the filter will return files with ".txt"
within the filename but not necessarilly defining filetype, e.g. "myImageFileName.txt.jpg").
For any given filename, if the sub-string you specify is not contained in it, the file entry would be removed from your file list, until only the ones that contain ".txt" will remain (So you will have succesfully applied the filter / "mask" to the files).
To apply something like this, you'd need to go through the string representing (every one of) your file's names, and look for the first character of your substring (in this example, "."). Once a "." is found, then the next character in the filename would have to match the next character in your sub-string ("t"). If not, then the filter is reset and always starts looking from the first element (here, "."). Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
string fileName = ("myFileName.txt");
string subString = (".txt");
int subStringCounter = 0; //used to iterate through each character of the sub-string
bool fileNameContainsSubString = false;
for(int i=0; i<fileName.size() ; i++)
{
if(fileName[i].compare(subString[subStringCounter])==0)
{
subStringCounter++;
if(subStringCounter==subString.size())
{
fileNameContainsSubString = true;
break;
}
}
else
{
subStringCounter=0;
}
}
if(!fileNameContainsSubString) //if the file doesn't contain the sub-string specified by the filter
{
//remove file from your list, so that only the ones containing the sub-string will remain
}
|
EDIT: sorry, I wrote the code using char arrays and then changed it to std::strings but hadn't added std::string::compare in the "if" condition of line 8.