Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

get the full path of a file

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
2 Solutions
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.
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.

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now