Thread: Searching for File/Folder

  1. #1
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986

    Searching for File/Folder

    Hey

    I'm currently writing a web server for windows. I have managed to get the file requested from the browser, but I'm not sure how to go about checking if it exists.

    If they request something like www.site.com/news, I need to check if '/news' is a folder, or a file. I figured if theres a / after news, like "/news/" then I'll look for a folder first, otherwise if theres no '/' at the end I'll check for a file first. If theres no file, then I'll look for a folder.

    I figured I'll try FindFirst/FindNext for this, and I've found numerous examples on how to do it, so I don't need any help there.

    But I was wondering if there was other functions (parhaps that aren't platform specific) that could also do the job?

    All I need to do is something like this
    Code:
    if (file string has '/')
    {
        open as a folder and index files. if not found report 404 file not found
    }
    else 
    {
        look for a file by the name, if we cant find it look for a folder. if not found report 404 file not found
    }

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    There's GetfileAttributes()...platform specific alternative, or stat()...a little more portable.
    As for a more portable method for doing FindFirst/FindNext stuff, you should be able to find what you want by searching the Linux Programming board.

    gg

  3. #3
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    OMG... I'm such an idiot.. I just realised that under Windows (well 2000 at least) you can't have a file and a folder with the same name I can't believe I didn't know this, I knew you can't have 2 files with the same name but I never knew the same went for folders.

    That just made my job easier, and thanks for the links Codeplug, it looks like GetFileAttributes will be the function I use.

    I don't plan to port this to linux (for the time bieng) as theres already hundreds of web servers and I'm designing this to be easy for people knew to web servers to pick up (and most people who use linux are probably already very familiar with them).

    Thanks Codeplug!

  4. #4
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Quick question:

    If I have
    Code:
    if (GetFileAttributes(FileRequested == FILE_ATTRIBUTE_DIRECTORY)
    Should I change the '==' to '&', because there may be more attributes for the folder than it just being a directory? I don't know much about bitwise stuff, but I've heard this used elsewhere when FILE_ATTRIBUTES are concerned.

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Also, MSDN says http://msdn.microsoft.com/library/de...attributes.asp that if GetFileAttributes failes it will return INVALID_FILE_ATTRIBUTES, but when i try to compile that it says theres no such thing as INVALID_FILE_ATTRIBUTES, and i searched all the include files for that string but I got no matches.

    What should I use instead?

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Yeah, they forgot that #define....this is what I use:
    Code:
    #ifndef INVALID_FILE_ATTRIBUTES
    #define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
    #endif
    Yes, the returned value is a bitmask, specifying any of the possible file attributes. Use a bitwise AND to see if a particular attribute is turned on:
    Code:
        DWORD attribs = GetFileAttributes(pathName);
    
        if ((attribs & FILE_ATTRIBUTE_DIRECTORY) &&
            (attribs & FILE_ATTRIBUTE_HIDDEN))
        {
            //Do something with this hidden directory
        }
    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM