WIN32_FIND_DATA - Get the absolute path
Solution 1
See the GetFullPathName
function.
Solution 2
You can try GetFullPathName
Or you can use SetCurrentDirectory
and GetCurrentDirectory
. You might want to save the current directory before doing this so you can go back to it afterwards.
In both cases, you only need to get the full path of your search directory. API calls are slow. Inside the loop you just combine strings.
Mihai Todor
Software Engineer at your service. You can find me on: Linkedin GitHub Twitter Stack Overflow tools that I created: Top N users for tag located in city / country (for answers) Top N users for tag located in city / country (for questions)
Updated on June 11, 2022Comments
-
Mihai Todor almost 2 years
I'm using something like this:
std::string tempDirectory = "./test/*"; WIN32_FIND_DATA directoryHandle; memset(&directoryHandle, 0, sizeof(WIN32_FIND_DATA));//perhaps redundant??? std::wstring wideString = std::wstring(tempDirectory.begin(), tempDirectory.end()); LPCWSTR directoryPath = wideString.c_str(); //iterate over all files HANDLE handle = FindFirstFile(directoryPath, &directoryHandle); while(INVALID_HANDLE_VALUE != handle) { //skip non-files if (!(directoryHandle.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { //convert from WCHAR to std::string size_t size = wcslen(directoryHandle.cFileName); char * buffer = new char [2 * size + 2]; wcstombs(buffer, directoryHandle.cFileName, 2 * size + 2); std::string file(buffer); delete [] buffer; std::cout << file; } if(FALSE == FindNextFile(handle, &directoryHandle)) break; } //close the handle FindClose(handle);
which prints the names of each file in the relative directory
./test/*
.Is there any way to determine the absolute path of this directory, just like
realpath()
does on Linux without involving any 3rd party libraries like BOOST? I'd like to print the absolute path to each file.