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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hi JohnFill!! please could you give me more info!!! 2

Status
Not open for further replies.

oreganr8

Programmer
Aug 6, 2001
7
GB
hi John mate!
I still require a small bit of code that will let me read the contents of a directory - so i can find all .htm files!



i tried that #include <windows.h>
and used FindFirstFile

but i had no information on the parameters..
so i tried quite a few things - but no joy..
i then did a find for all the header and libry files that contain FindFirstFile..

i got FindFirstFileA, FindFirstFileW - but there was no real information except that there are two fields - and i think the first is an array
?



if you could give me a small example showing me how to do this i would be really grateful
!

thanks
!
 
Refer to MSDN contained in your VC++ installation
or internet. You need to use FindFirstFile and
Find NextFile to implement it and I think it
is not difficult if MSDN is available.
 
#define _WIN32_WINNT 0x0400
#include<windows.h>
void main()
{
HFILE hFile = 0;
WIN32_FIND_DATA pwf;
hFile = (HFILE)FindFirstFile(&quot;d:\\*.*&quot;,&pwf);
if (hFile == (HFILE)INVALID_HANDLE_VALUE) {
printf (&quot;Invalid File Handle. Get Last Error reports %d\n&quot;, GetLastError ());
}
printf((char*)pwf.cFileName);
hFile = (HFILE)FindNextFile((void*)hFile,&pwf);
printf((char*)pwf.cFileName);
FindClose((void*)hFile);
return;
} John Fill
1c.bmp


ivfmd@mail.md
 
hey John - many thanks for that!

the people in this forum are really helpful - i have now
two solutions to the problem! (the other one uses a dos command )
!

well i tried your programme - i had to change the main
to int main(void)..
and its works!
it printed the first two files on the directory!!
nice one!
i tried to make it read more but didnt quite succeed - but now my mother is here and she wants to go out for lunch , so i must work on this later - im sure i'll do it now thanks!!!

i didnt reconise the code format ? is it C++ ?
!

thanks very much matie!!

Rich
 
Code:
#include <stdio.h>
#include <string.h>
#include <io.h>


void printFilesDir(char *mypath);

//FILE *fp;

int main(){


	//char mypath[]=&quot;c:\\&quot;;
	char *mypath;


	//fp=fopen(&quot;c:\\test.txt&quot;,&quot;w&quot;);

	mypath=strdup(&quot;c:\\&quot;);

	printFilesDir(mypath);


	//fclose(fp);

	return 0;

}



void printFilesDir(char *mypath){


	long hsearch;
	struct _finddata_t findinfo;
	long success;
	char *buffer;
	char strtmp[1000];
	char spec[]=&quot;*.*&quot;;
	char root[1000];

	strtmp[0]=0;



	strcpy(root,mypath);

	if( mypath[strlen(root)-1] != '\\')
		strcat(root,&quot;\\&quot;);


	sprintf(strtmp,&quot;%s%s&quot;,root,spec);


	hsearch= _findfirst(strtmp, &findinfo );

	if(hsearch == -1)
		return;


	do{
    	buffer = strdup(findinfo.name);

    	if( (strcmp(buffer,&quot;.&quot;)) != 0 && (strcmp(buffer,&quot;..&quot;)) != 0){


    		if(findinfo.attrib & _A_SUBDIR){

				strtmp[0]=0;
				sprintf(strtmp,&quot;%s%s&quot;,root,buffer);

        		printFilesDir(strtmp);

    		}else
        		fprintf(stdout,&quot;%s%s\n&quot;,root,buffer);
        		//fprintf(fp,&quot;%s%s\n&quot;,root,buffer);

		}

		success = _findnext(hsearch, &findinfo);
	}while(success == 0);


	_findclose(hsearch);

}
bluenote@uyuyuy.com
(excuse my english)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top