I am using Visual Studio C 2017 Professional and MFC
I am working on a project which has a function that collects file paths (like C:/foo1/foo2/foo3.txt
) as string
s, if a file with that path exists (found using the filesystem
library).
At first this looks straightforward until I see that the file paths often are simply (for lack of a better word) templates.
My program is given a template file path such as this:
C:/User/Documents/%A/%B.txt
, where %A
represents a year (which my program has a range for and iterates through each year, comparing to the template folder) and %B is the month, again iterating.
It only gets more complicated, because we now add two more template symbols: *
and ?
.
For example:
C:/User/Documents/*_%A_??_*/%B_*.txt
.
Here, *
represents 0 or more characters and ?
represents one character.
So in the example, a filepath like: C:/User/Documents/smile_2022_A1_/07_ABCDEFGHIJKLMNOP.txt
should be found and saved as a string
.
I am able to separate the file path via tokenization, meaning I already have a function that collects a vector
filled with, say, C:
, foo1
, foo2
, foo3.txt
. With this I can iterate through the vector
either with a loop or recursively enter a folder until I either:
- reach a deadend (aka the folder or file does not exist)
- reach the file I want and save its entire path as a
string
One thought I have come up with is look for _
symbols and separate the folder or file name with those but this gets countered by:
- if the
*
or?
contain a_
- if the folder or file name simply do not contain
_
The solution I am looking for ideally does not utilize regex
. However, if that is the only solution (obviously I do not want to recreate regex
), please advise me on how I can use it on varying path complexities, as my function should allow for both regular folder paths without template symbols and those with them.
The reason I say no regex
is because from my understanding, it is a little more strict when it comes to comparing a file. It expects the string
to be written in a certain generic but consistent way. However, in my case I cannot expect a user to name their folders in a consistent manner...
CodePudding user response:
Once you replace YOUR env variables (%A, %B, etc.), then FindFirstFile
, FindNextFile
API calls support wild cards.