CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    determine if a string is a valid path string

    Hello, if I have a string, what's the best or easiest way to determine if the string is a valid windows path. I do not mean that the file or folder exists, but that the string itself is valid to be a path. This should also work for network folders or files. So for example "C:", "C:\", "C:\myfolder", "C:\my folder\something else", "\\some host5\some folder" would all be valid, while C:\\some folder\\something else" would be invalid, since it isn't valid in windows explorer. Note that those do not actually have to exist. They are just valid strings for a path.

    Right now I am using regular expressions to check if the string matches a windows path pattern, but maybe there is something in MFC or Win32 API to use? I've looked but all I can find is how to find if a path is indeed valid, which isn't what I am looking for. Maybe I haven't been using the right keyworkds.

    Also, somewhat related, I need to know if the path string would be a file or a folder. Is there really a good way to distinguish between the two given a path (and remember the path may not be actually something that exists)? Right now we are just making the assumption that folder paths do not have a "." and that file paths end with a . followed by 1 or more word characters for the extension. I guess it's a constraint we are imposing, but I do not like this very much, but it is as much I can do with regular expressions, and what I know.

    Any ideas are very much appreciated.

    Latem
    Last edited by Latem; May 9th, 2006 at 02:04 PM.
    Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: determine if a string is a valid path string

    Maybe ::PathSearchAndQualify() can help you.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  3. #3
    Join Date
    Feb 2002
    Posts
    3,788

    Re: determine if a string is a valid path string

    Quote Originally Posted by golanshahar
    Maybe ::PathSearchAndQualify() can help you.

    Cheers
    the link points to PathRemoveFileSpec

  4. #4
    Join Date
    May 2005
    Posts
    4,954

    Re: determine if a string is a valid path string

    Quote Originally Posted by Alin
    the link points to PathRemoveFileSpec
    I noticed that second after I posted and fixed it, you simply too fast for me

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  5. #5
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: determine if a string is a valid path string

    I was about to suggets that function myself, but after a simple test I don't think it will help you:

    Here's one input example:
    Code:
    ||<&&c:\test..\he  <>|&%ll>o.cpp
    It returned OK with this result:
    Code:
    c:\Petter\Src\CPP\VolumeTasks\VolumeTasks\||<&&c:\test.\he  <>|&%ll>o.cpp
    - petter

  6. #6
    Join Date
    May 2005
    Posts
    4,954

    Re: determine if a string is a valid path string

    Quote Originally Posted by wildfrog
    I was about to suggets that function myself, but after a simple test I don't think it will help you:

    Here's one input example:
    Code:
    ||<&&c:\test..\he  <>|&%ll>o.cpp
    It returned OK with this result:
    Code:
    c:\Petter\Src\CPP\VolumeTasks\VolumeTasks\||<&&c:\test.\he  <>|&%ll>o.cpp
    - petter
    You are right , I just tested it, and its looks bad....expect adding the full path it doesn’t give any notification for bad path ( at least for the one you posted )

    @OP sorry, I guess with that kind of performance it won’t help you.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  7. #7
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: determine if a string is a valid path string

    Have you looked into the _access() function ??

    Quote Originally Posted by MSDN

    int _access( const char *path, int mode );


    When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access.
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  8. #8
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: determine if a string is a valid path string

    Quote Originally Posted by Latem
    Right now we are just making the assumption that folder paths do not have a "." and that file paths end with a . followed by 1 or more word characters for the extension. I guess it's a constraint we are imposing, but I do not like this very much, but it is as much I can do with regular expressions, and what I know.
    Well, folders can have extensions and files don't need to have one (try it out).

    So, an assumption like the one you have in your implementation would fail you at the very first exception.

    However, why is it important for you to identify a string as a folder or file? It shouldn't really be. Reason: you say that the string doesn't really need to signify a file / folder that genuinely exists, and hence it should be sufficient to be able to predict that the path contained within it could be either a file or a folder. Right?

  9. #9
    Join Date
    Feb 2004
    Location
    Canada
    Posts
    277

    Re: determine if a string is a valid path string

    Well, folders can have extensions and files don't need to have one (try it out).
    I know, but I guess we are making this constraint, ont really an assumption that the user should follow. Basically my program has environment variables that a user can set. The values can either be a file (such as some input file), a folder (such as a path to some data), or otherwise a string (a constant value of something). I need to distinguish between the three, and not just if its a path (could be either a file, or a folder), or a string.
    Being a pessimist is wonderful; you are either proven right, or pleasantly surprised.

  10. #10
    Join Date
    May 2005
    Posts
    4,954

    Re: determine if a string is a valid path string

    Quote Originally Posted by Latem
    I know, but I guess we are making this constraint, ont really an assumption that the user should follow. Basically my program has environment variables that a user can set. The values can either be a file (such as some input file), a folder (such as a path to some data), or otherwise a string (a constant value of something). I need to distinguish between the three, and not just if its a path (could be either a file, or a folder), or a string.
    Maybe i am missing something here but if you letting the user to choose it, why not simply use ::SHBrowseForFolder() to choose directory, and CFileDialog to choose a file and then you will set the output of those functions to your environments variables such as ( DIR, FILE, STRING) in that way it cant be gibberish value cause you let the user choose existing file/folders. The only thing left for you is before using the values from the environment to check whether the files/dir really exist and wasn’t deleted somehow using functions like: ::PathFileExists() and ::PathIsDirectory().

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured