Need help to get list of files in a directory...
-
NOW I CAN RUN IT USING VC++ PROFF 2005 NO PROBLEM :) .. Below is the code i use : 1.#include <windows.h> 2.#include <stdio.h> 3.#include <iostream> 4.#include <string> 5.#include <sstream> 6. 7. 8. 9.std::string convertWCharArrayToString(const WCHAR * const wcharArray) { 10. std::stringstream ss; 11. 12. int i = 0; 13. char c = (char) wcharArray[i]; 14. while(c != '\0') { 15. ss <<c; 16. i++; 17. c = (char) wcharArray[i]; 18. } 19. 20. std::string convert = ss.str(); 21. return convert; 22.} 23. 24.int main() 25.{ 26. WIN32_FIND_DATA findFileData; 27. HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); 28. 29. if(hFind == INVALID_HANDLE_VALUE) { 30. std::cout <<"No files found." <<std::endl; 31. } else { 32. std::cout <<"Files found." <<std::endl; 33. } 34. 35. int fileNumber = 0; 36. std::cout <<fileNumber <<":" <<convertWCharArrayToString(findFileData.cFileName) <<std::endl; 37. while(FindNextFile(hFind, &findFileData)) { 38. fileNumber++; 39. std::cout <<fileNumber <<":" <<convertWCharArrayToString(findFileData.cFileName) <<std::endl; 40. } 41. 42. FindClose(hFind); 43. 44. char a; 45. std::cin>> a; 46. return 0; 47.} But when i change HANDLE hFind = FindFirstFile((LPCWSTR)"*", &findFileData); to HANDLE hFind = FindFirstFile((LPCWSTR)"C:\*", &findFileData); its showing no file found and some funny symbols... but i wanted it to show entire list of files in a directory if i choose C:\ or D:\ and so on.. Can u guys help me plz... :)
Directly take the output from findFileData.cFileName. The problem exists in your convertWCharArrayToString().
std::cout <<fileNumber <<":" <<findFileData.cFileName <<std::endl;
-
well it jus show the folder name where is the c++ files are located if i use "." ... its not moving out of the folder....
Thilek wrote:
well it jus show the folder name where is the c++ files are located if i use "." ... its not moving out of the folder....
This makes little, if any, sense. Perhaps you are not understanding relative vs. absolute paths. For example, if you specified
"*.*"
, then".\\*.*"
is implied and the files in the current working directory (CWD) will be listed. If you specified"..\\*.*"
, then the files in the CWD's parent folder will be listed. If you specified"c:\\*.*"
, then the files in the root folder will be listed. Consider:void main( void )
{
WIN32_FIND_DATA findFileData;HANDLE hFind = FindFirstFile(\_T("\*.\*"), &findFileData); if (hFind != INVALID\_HANDLE\_VALUE) { BOOL bFound = true; int fileNumber = 0; while (bFound) { fileNumber++; wcout << fileNumber << \_T(":") << findFileData.cFileName << endl; bFound = FindNextFile(hFind, &findFileData); } FindClose(hFind); }
}
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Directly take the output from findFileData.cFileName. The problem exists in your convertWCharArrayToString().
std::cout <<fileNumber <<":" <<findFileData.cFileName <<std::endl;
well still the same.. if i put the exe file in C:/ and run it it shows the files and folders in C:/ but it does not going in further into the folders and get the file list.... The program i been working have to get the entire files in the subfolders.. since i am creating a anti-worm system... thanks :)
-
Thilek wrote:
well it jus show the folder name where is the c++ files are located if i use "." ... its not moving out of the folder....
This makes little, if any, sense. Perhaps you are not understanding relative vs. absolute paths. For example, if you specified
"*.*"
, then".\\*.*"
is implied and the files in the current working directory (CWD) will be listed. If you specified"..\\*.*"
, then the files in the CWD's parent folder will be listed. If you specified"c:\\*.*"
, then the files in the root folder will be listed. Consider:void main( void )
{
WIN32_FIND_DATA findFileData;HANDLE hFind = FindFirstFile(\_T("\*.\*"), &findFileData); if (hFind != INVALID\_HANDLE\_VALUE) { BOOL bFound = true; int fileNumber = 0; while (bFound) { fileNumber++; wcout << fileNumber << \_T(":") << findFileData.cFileName << endl; bFound = FindNextFile(hFind, &findFileData); } FindClose(hFind); }
}
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
but is there anyway if i type in C:/ or D:/ it will show files in the directory including the files in sub folders ???
Thilek wrote:
but is there anyway if i type in C:/ or D:/ it will show files in the directory including the files in sub folders ???
Yes. The easiest solution is via a recursive function call. Examples are plentiful on the Web and here at CP.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Thilek wrote:
but is there anyway if i type in C:/ or D:/ it will show files in the directory including the files in sub folders ???
Yes. The easiest solution is via a recursive function call. Examples are plentiful on the Web and here at CP.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
-
well still the same.. if i put the exe file in C:/ and run it it shows the files and folders in C:/ but it does not going in further into the folders and get the file list.... The program i been working have to get the entire files in the subfolders.. since i am creating a anti-worm system... thanks :)
Where do you have problem exactly?FindFirstFile is easy to use?
Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )
-
well still the same.. if i put the exe file in C:/ and run it it shows the files and folders in C:/ but it does not going in further into the folders and get the file list.... The program i been working have to get the entire files in the subfolders.. since i am creating a anti-worm system... thanks :)
You have to recursively call and iterate each Folders from the output if findFileData.dwFileAttributes is FILE_ATTRIBUTE_DIRECTORY. Try that.