How to read a file mask?

I wanted to use a file mask as suppose there is a folder in D drive which contains some files.
Then from it want to fetch the files with *.txt* format,or with some specific name or something. I tried to find the answer on google but didn't found any relevant one. So please could help.
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.

Last edited on
@sisodia

Your question is a bit unclear.

But are you looking the the Win32 FindFirstFile/FindNextFile/FindClose functions, which allows you to enumerate the files in a folder that match a given pattern?

If that's the case, see this web page (inc. example code)

FindFirstFile function
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa364418%28v=vs.85%29.aspx

Andy

PS If you'd prefer a cross-platform solution, Boost.Fileystem provides equivalent functionality (the directory_iterator)

Boost Filesystem Library Version 3
http://www.boost.org/doc/libs/1_51_0/libs/filesystem/doc/index.htm

Last edited on
Topic archived. No new replies allowed.