Extracting folder information with c++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2006
Posts: 32
Reputation: muthuivs is an unknown quantity at this point 
Solved Threads: 0
muthuivs muthuivs is offline Offline
Light Poster

Extracting folder information with c++

 
0
  #1
Feb 1st, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: Extracting folder information with c++

 
2
  #2
Feb 1st, 2006
What operating system are you using?
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 32
Reputation: muthuivs is an unknown quantity at this point 
Solved Threads: 0
muthuivs muthuivs is offline Offline
Light Poster

Re: Extracting folder information with c++

 
0
  #3
Feb 1st, 2006
I am using WIndows XP, but will have to write a similar program for linux later.


Originally Posted by Rashakil Fol
What operating system are you using?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Extracting folder information with c++

 
0
  #4
Feb 1st, 2006
use FindFirstFile() and FindNextFile() to loop through all the files in a directory. Search MSDN and you will find some examples.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 32
Reputation: muthuivs is an unknown quantity at this point 
Solved Threads: 0
muthuivs muthuivs is offline Offline
Light Poster

Re: Extracting folder information with c++

 
0
  #5
Feb 1st, 2006
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:


  1. int main(int argc, char *argv[])
  2. {
  3. WIN32_FIND_DATA FindFileData;
  4. HANDLE hFind = INVALID_HANDLE_VALUE;
  5. char DirSpec[MAX_PATH + 1]; // directory specification
  6. DWORD dwError;
  7.  
  8. printf ("Target directory is %s.\n", argv[1]);
  9. strncpy (DirSpec, argv[1], strlen(argv[1])+1);
  10. strncat (DirSpec, "\\*", 3);
  11.  
  12. hFind = FindFirstFile(DirSpec, &FindFileData);
  13.  
  14. if (hFind == INVALID_HANDLE_VALUE)
  15. {
  16. printf ("Invalid file handle. Error is %u\n", GetLastError());
  17. return (-1);
  18. }
  19. else
  20. {
  21. printf ("First file name is %s\n", FindFileData.cFileName);
  22. while (FindNextFile(hFind, &FindFileData) != 0)
  23. {
  24. printf ("Next file name is %s\n", FindFileData.cFileName);
  25. }
  26.  
  27. dwError = GetLastError();
  28. FindClose(hFind);
  29. if (dwError != ERROR_NO_MORE_FILES)
  30. {
  31. printf ("FindNextFile error. Error is %u\n", dwError);
  32. return (-1);
  33. }
  34. }
  35. return (0);
  36. }
Last edited by WolfPack; Jul 13th, 2006 at 9:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Extracting folder information with c++

 
0
  #6
Feb 2nd, 2006
Here is simple code to get all the file names in a directory. It does not parse sub-directories.


  1. WIN32_FIND_DATA FindFileData;
  2. HANDLE hFind = INVALID_HANDLE_VALUE;
  3. char DirSpec[MAX_PATH]; // directory specification
  4.  
  5. strcpy (DirSpec, argv[1]);
  6. strcat (DirSpec, "\\*");
  7. HANDLE hFind = FindFirstFile(DirSpec, &FindFileData);
  8. if (hFind != INVALID_HANDLE_VALUE)
  9. {
  10. do {
  11. // If this filename is NOT a directory
  12. if(!FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  13. {
  14. // do something with the filename
  15.  
  16. }
  17. } while( !FindNextFile(hFile,&FindFileData);
  18. FindClose(hFile);
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 32
Reputation: muthuivs is an unknown quantity at this point 
Solved Threads: 0
muthuivs muthuivs is offline Offline
Light Poster

Re: Extracting folder information with c++

 
0
  #7
Feb 2nd, 2006
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.


  1. WIN32_FIND_DATA FindFileData;
  2. HANDLE hFind = INVALID_HANDLE_VALUE;
  3. char DirSpec[MAX_PATH]; // directory specification
  4.  
  5. strcpy (DirSpec, argv[1]);
  6. strcat (DirSpec, "\\*");
  7. HANDLE hFind = FindFirstFile(DirSpec, &FindFileData);
  8. if (hFind != INVALID_HANDLE_VALUE)
  9. {
  10. do {
  11. // If this filename is NOT a directory
  12. if(!FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  13. {
  14. // do something with the filename
  15.  
  16. }
  17. } while( !FindNextFile(hFile,&FindFileData);
  18. FindClose(hFile);
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 1
Reputation: Jacku Lee is an unknown quantity at this point 
Solved Threads: 0
Jacku Lee Jacku Lee is offline Offline
Newbie Poster

Re: Extracting folder information with c++

 
0
  #8
Jul 13th, 2006
I found a little error in earlier replies.
The line:

if(!FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

is wrong.

It should be:

if( ! (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )


That's all.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Extracting folder information with c++

 
0
  #9
Jul 13th, 2006
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;
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC