Click to See Complete Forum and Search --> : Enumerating Directory files in Win32 Console APP


fshearer
March 14th, 2005, 03:03 PM
I am just learning C++ I was wondering if someone can point me in the right direction to enumerate files in a directory in a win32 console app. I am using Visual Studio 02 .net as a IDE. This is what I have so far, and if someone can help, can you explain how you did it. Thanks greatly appreciated

#include <iostream>

using namespace System;
using namespace System::IO;

using namespace std;

//Function Prototypes
int menu();
int DoTaskHelp();
int AllFiles();

int main()
{
bool exit = false;
for(;;)
{
int choice = menu();
switch(choice)
{
case (1):
DoTaskHelp();
break;

case (2):
exit = true;
break;

case (3):
AllFiles();


}//end switch
if (exit)
break;
}//end forever

return 0;
}

int menu()
{

int choice;


cout << "\t**********\n \t MENU\n\t**********\n***Created by F. Shearer***\n\n";
cout <<"(1) Help. \n";
cout <<"(2) Exit. \n";
cout <<"(3) Search. \n";
cout << " ";
cin >> choice;

return choice;
}

int DoTaskHelp()
{
cout <<"No help info at this time!\n\n";
return 0;
}

int AllFiles()
{

int drive;

//Choose drive and dispaly directories
cout <<"Enter Drive you want to search: ";
cin >> drive;


//drive to search directories
DirectoryInfo* Info = new DirectoryInfo();
return 0;
}

cilu
March 14th, 2005, 03:57 PM
DirectoryInfo is the .NET way. You can call GetFiles() mothod that will return a FileInfo array.

You can do it in C++ with Win32 API this way.

PS: do you mean recursivly or not?

jawadhashmi
March 14th, 2005, 11:01 PM
WIN32_FIND_DATA FindFileData;

HANDLE hFile = FindFirstFile(strFolderName,&FindFileData);
//Now run in a loop of all the files and folders under this folder
while(hFile && FindNextFile(hFile,&FindFileData))
{
//Ignore system directories "." and ".." the good old MS-DOS days
if( (stricmp(FindFileData.cFileName,".")!=0) && (stricmp (FindFileData.cFileName,"..")!=0) && (stricmp(FindFileData.cFileName,"") !=0) && (FindFileData.nFileSizeLow > 0))
{
if(!((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY) || (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)) )
{
char szFile[256] = "";
sprintf(szFile, "%s%s", szPath, FindFileData.cFileName);
}
}

cilu
March 15th, 2005, 03:56 AM
Please use CODE tags to wrap your code.

fshearer
March 15th, 2005, 02:00 PM
What r code tags?

fshearer
March 15th, 2005, 02:01 PM
Thanks for the help guys!

cilu
March 15th, 2005, 02:45 PM
What r code tags?
Code tags are tags use to format the code. When you post, take a look at the button that says "Code". Use it to wrap the tags aroung your code.

Here is the effect of using code tags:

#include <iostream>

using namespace System;
using namespace System::IO;

fshearer
March 15th, 2005, 03:32 PM
Thanks for the info. I will do that from now on