get the full path of a file

Posted on 2006-04-20
Last Modified: 2013-11-20
I want to get the full path of a file(For example c:\Mytrial\Sample\Display.txt ) by just giving the file name(For example Display.txt) as input

The File may be present at anyone of the drives in my PC (Any random directory path). I want to find the full path of the file by running a MFC code from some other drive in the same PC and giving just the file name as input( For example Display.txt ) to the code.

then after i will delete the file.

I tried with CFileFind class without success. Any VC++ experts help me by suggesting some sample code.

please help. how to find the file and how to delete.

thank you very much
Question by:haroldxious
    LVL 1

    Assisted Solution

    It takes time to do that, like file search engine. You can refer this link. I just look around and think it may be helpful for you.
    LVL 12

    Accepted Solution

    Big job.
    You need to traverse all the directory for the file.
    In that case you may find more than one file with the same name in different paths.

    You need to use the functions:

    // To open the directory
    HANDLE CreateFile(
      LPCTSTR lpFileName,                         // file name
      DWORD dwDesiredAccess,                      // access mode
      DWORD dwShareMode,                          // share mode
      LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
      DWORD dwCreationDisposition,                // how to create
      DWORD dwFlagsAndAttributes,                 // file attributes
      HANDLE hTemplateFile                        // handle to template file

    // The find the first file in directory.
    HANDLE FindFirstFile(
      LPCTSTR lpFileName,               // file name
      LPWIN32_FIND_DATA lpFindFileData  // data buffer

    // The find the next file in directory.
    BOOL FindNextFile(
      HANDLE hFindFile,                // search handle
      LPWIN32_FIND_DATA lpFindFileData // data buffer

    Remember that the file that you find could be a directory. In that case you need to use the sequence recursively.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now