Check if a path is a folder of file

Hi!
Simple question: Is there a way to find out if a path contained in a string variable corresponds to a folder or a file



Answer this question

Check if a path is a folder of file

  • pudtaan

    Using simply the string format, no.  A directory can contain all the same characters as a filename and vice versa.  The only way to tell is to actual check to see if the file or directory exists using either File.Exists or Directory.Exists.  If you need to determine this without going to the file system then your only option is to use simple heuristics.  Directory name normally end in a slash.  File names normally have an extension.  I also generally assume a name with no period is also a directory name.  If you look at the document for PathIsFileSpec and related APIs you'll see that Microsoft does the same thing.  It will also give you their rules for naming.

    Michael Taylor - 10/7/05

  • Ginesh Kumar

    Thanks Michael. Your point is pretty clear.


  • dwBIconsultant

    I agree with Taylor. The only thing you can do is follow standard practice and assume that a directory will not have a period (.) in the name.

    If you want to know the exact name you would have to create a parse routine that would take the string you mentioned and parse it out. Probably by a forward slash "/" or more commonly a back slash "\". Then check the last piece of data from the parse operation to see if it contains what you are looking for.

  • Check if a path is a folder of file