Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extracting folder Information

Status
Not open for further replies.

muthuivs

Technical User
Jan 30, 2006
36
CA
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
 
Not that I know of.
Use FindFirstFile() and FindNextFile() until you get all the files in the directory and count each one.
 
can you give me an example of the FindFirstFile() and FindNextFile() function? I am trying the MSDN example and I am getting errors left, right and centre!! here is what I typed, I am not sure where to enter the path to my folder in this code:


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);
}
 
What errors are you getting? Compile-time or run-time?
 
It is a runtime error but I think it is because I dont know where to specify my path in the code. I tried forcing it into char *argv[] but I get a conversion error, so i leave it and it it goes into cmd line but terminated with a fatal error. Any Idea?
 
Does this line print the path properly?
Code:
printf ("Target directory is %s.\n", argv[1]);
What if you try hard-coding the path into a const char array? Does that still give you an error?
 
The program also terminates before anything shows up on the Cmd line, it is a windows invoked error. I gave up on that example and decided to go a different route, I am not reading the file any more, instead I am running the loop to the max possible scenario about 99 times so it still runs within 6 seconds.
 
your code seems right... look like microsoft code...

I guest you don't pass parameters to your .exe
like :
MyApp.exe c:\
or
MyApp.exe "c:\program files
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top