| | |
Extracting folder information with c++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2006
Posts: 32
Reputation:
Solved Threads: 0
Ladies and Gents,
I am trying to write a function in my ever growing program to give me information on a selected folder. I actually only need the number of files in the folder so I can use that number in a loop. Is there a way to get the number of files in INT form for this purpose ? Thanks in advance!!
Muthu
I am trying to write a function in my ever growing program to give me information on a selected folder. I actually only need the number of files in the folder so I can use that number in a loop. Is there a way to get the number of files in INT form for this purpose ? Thanks in advance!!
Muthu
•
•
Join Date: Jan 2006
Posts: 32
Reputation:
Solved Threads: 0
I searched MSDN for examples and I got the one here but I don't know where to place the path of my folder. Can you give me a simpley example than this beast:
C++ Syntax (Toggle Plain Text)
int main(int argc, char *argv[]) { WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH + 1]; // directory specification DWORD dwError; printf ("Target directory is %s.\n", argv[1]); strncpy (DirSpec, argv[1], strlen(argv[1])+1); strncat (DirSpec, "\\*", 3); hFind = FindFirstFile(DirSpec, &FindFileData); if (hFind == INVALID_HANDLE_VALUE) { printf ("Invalid file handle. Error is %u\n", GetLastError()); return (-1); } else { printf ("First file name is %s\n", FindFileData.cFileName); while (FindNextFile(hFind, &FindFileData) != 0) { printf ("Next file name is %s\n", FindFileData.cFileName); } dwError = GetLastError(); FindClose(hFind); if (dwError != ERROR_NO_MORE_FILES) { printf ("FindNextFile error. Error is %u\n", dwError); return (-1); } } return (0); }
Last edited by WolfPack; Jul 13th, 2006 at 9:21 pm.
Here is simple code to get all the file names in a directory. It does not parse sub-directories.
C++ Syntax (Toggle Plain Text)
WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH]; // directory specification strcpy (DirSpec, argv[1]); strcat (DirSpec, "\\*"); HANDLE hFind = FindFirstFile(DirSpec, &FindFileData); if (hFind != INVALID_HANDLE_VALUE) { do { // If this filename is NOT a directory if(!FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // do something with the filename } } while( !FindNextFile(hFile,&FindFileData); FindClose(hFile);
•
•
Join Date: Jan 2006
Posts: 32
Reputation:
Solved Threads: 0
Thanks. I'll give it a try.
•
•
•
•
Originally Posted by Ancient Dragon
Here is simple code to get all the file names in a directory. It does not parse sub-directories.
C++ Syntax (Toggle Plain Text)
WIN32_FIND_DATA FindFileData; HANDLE hFind = INVALID_HANDLE_VALUE; char DirSpec[MAX_PATH]; // directory specification strcpy (DirSpec, argv[1]); strcat (DirSpec, "\\*"); HANDLE hFind = FindFirstFile(DirSpec, &FindFileData); if (hFind != INVALID_HANDLE_VALUE) { do { // If this filename is NOT a directory if(!FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { // do something with the filename } } while( !FindNextFile(hFile,&FindFileData); FindClose(hFile);
•
•
Join Date: Jul 2006
Posts: 65
Reputation:
Solved Threads: 14
If you have the boost library installed, you might consider using boost::filesystem instead (which would be more portable):
#include <boost/filesystem/operations.hpp> #include <iostream> using namespace boost::filesystem; int main() { path mypath("/boost_1_33_1"); if (!exists(mypath)) { std::cerr<<"Path does not exist!"; return EXIT_FAILURE; } directory_iterator dirIt(mypath); int filecount=0; for (directory_iterator dirIt(mypath);dirIt!=directory_iterator();dirIt++) { if (!is_directory(*dirIt)) { std::cout<<dirIt->leaf()<<std::endl; filecount++; } } std::cout<<"Number of files in directory: "<<filecount; }
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: binary tree traversal ..
- Next Thread: Factory Design pattern implementation
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






