[英]Execute a file using a pattern as the filename
As part of my application I need to give the users the ability to execute files (videos) from my application, but the problem is that the application does not store a list of these files anywhere in the application itself and the only infomation that is known about the file is the directory it's located in and that it must match a specific pattern, which looks like this: * - Ep[0-99] - *
where the *
character represents any string, integer, unicode character, etc of any length and Ep
& -
are string literals and finally, the [0-99]
is any integer value between 0 and 99. 作为我的应用程序的一部分,我需要使用户能够执行我的应用程序中的文件(视频),但问题是该应用程序未在应用程序本身中的任何位置存储这些文件的列表,并且该信息是唯一已知的信息。关于文件的信息是文件所在的目录,并且必须匹配特定的模式,如下所示: * - Ep[0-99] - *
其中*
字符表示任何长度的字符串,整数,unicode字符等和Ep
& -
是字符串文字,最后[0-99]
是0到99之间的任何整数值。
This sounds like a perfect job for regular expressions, but the problem is regular expressions require a string to match against ahead of time before a match can be found and as previously stated, the application does not store this string anywhere so using RegEx would not work unless I first used something like the FindFirst
function to search the directory the files are located in and then iterate over it, loading each of the files names in a variable that RegEx can match against, but I would rather avoid doing things like this if there is an better alternative available. 对于正则表达式来说,这听起来似乎是一项完美的工作,但是问题在于正则表达式需要先与字符串进行匹配才能找到匹配项,并且如前所述,应用程序不会将此字符串存储在任何地方,因此使用RegEx将无法正常工作除非我首先使用FindFirst
函数之类的东西来搜索文件所在的目录,然后对其进行遍历,然后将每个文件名加载到RegEx可以匹配的变量中,但是我宁愿避免这样做是更好的选择。
I have also looked into the MatchesMask
function to accomplish this task, but it suffers from the same issue that using regular expressions does so I would appreciate some advice or an alternative to the method I already mentioned above. 我也研究了MatchesMask
函数来完成此任务,但是它也遇到了使用正则表达式的问题,因此,我希望能获得一些建议或替代上面已经提到的方法的问题。
There's no getting away from enumerating the contents of the directory. 枚举目录的内容无可避免。 If you must avoid that, for reasons unknown, then your only remaining option is to guess the filenames. 如果出于某种未知原因必须避免这种情况,那么您唯一剩下的选择就是猜测文件名。 If you wish to find them all then you'd need to try every possible filename that matches your pattern. 如果希望找到所有文件,则需要尝试所有与模式匹配的文件名。 That is not tractable. 那是很难处理的。 There are far too many. 太多了。
Which takes us back to FindFirst
or some other directory enumeration scheme. 这使我们回到FindFirst
或其他目录枚举方案。 Personally I would use FindFirst
directly. 我个人将直接使用FindFirst
。 It is simple to call, and efficient. 调用简单,效率高。 In pseudo-code: 用伪代码:
retval := FindFirst(TPath.Combine(dir, '*'),
faAnyFile and not faDirectory, searchRec);
if retval = 0 then
try
repeat
filename := TPath.Combine(dir, searchRec.Name);
if MatchesRegex(filename) then
DoStuff(filename);
until FindNext(searchRec) <> 0;
finally
FindClose(searchRec);
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.