[filesystem] Checking if a file exists within a sepcific directory tree

Suppose the following directory: C:\export As a command line argument in one of my applications, I get an absolute path to a file. I want to check if this file exists at some nested level inside of C:\export. Suppose the file passed in is: C:\export\blah\myfile.txt I would like to be able to call some sort of function that returns 'true' if the file (myfile.txt) exists within the directory tree rooted at C:\export. Now suppose this next file: C:\work\anotherfile.txt The function in question would return 'false' here since anotherfile.txt does not exist inside of c:\export. Something like the following would work for me. is_file_within_directory_tree( "C:\export", "C:\work\anotherfile.txt" ); // returns false is_file_within_directory_tree( "C:\export", "C:\export\blah\myfile.txt" ); // returns true Is there an easy way to do this with Boost.Filesystem? Thanks, Michael Marcin

Suppose the following directory:
C:\export
As a command line argument in one of my applications, I get an absolute path to a file. I want to check if this file exists at some nested level inside of C:\export. Suppose the file passed in is:
C:\export\blah\myfile.txt
I would like to be able to call some sort of function that returns 'true' if the file (myfile.txt) exists within the directory tree rooted at C:\export.
Now suppose this next file:
C:\work\anotherfile.txt
The function in question would return 'false' here since anotherfile.txt does not exist inside of c:\export.
Something like the following would work for me.
is_file_within_directory_tree( "C:\export", "C:\work\anotherfile.txt" ); // returns false is_file_within_directory_tree( "C:\export", "C:\export\blah\myfile.txt" ); // returns true
Is there an easy way to do this with Boost.Filesystem?
Ooops, meant to post this to the users list. Since it's here already I won't cross post it there. Sorry, Michael Marcin

On 11/06/07, Michael Marcin <mmarcin@method-solutions.com> wrote:
Something like the following would work for me.
is_file_within_directory_tree( "C:\export", "C:\work\anotherfile.txt" ); // returns false is_file_within_directory_tree( "C:\export", "C:\export\blah\myfile.txt" ); // returns true
Is there an easy way to do this with Boost.Filesystem?
Depending what you want, you could do a find for the leaf of the file over a recursive_directory_iterator of the path, or you could see if the iterator range in the path is a prefix of that in the file. ~ Scott McMurray
participants (2)
-
me22
-
Michael Marcin