Micke_Cap
asked on
Return filename from folder! (Urgent)
Could anyone tell me how I get the first file in a folder? I have a function whith one in parameter, witch is a path. In this path i would like too return the first filename it finds.
//Micke_Cap
//Micke_Cap
ASKER
Thanks!
/*
this function recursivly scan directries. you will need to change it so it gets the first file only.
i donr this in VC++ on NT 4.0
*/
#include <stdio.h>
#include <io.h>
int scan_path(char *path)
{
char spath[256];
struct _finddata_t ff;
long fhandle;
sprintf(spath,"%s\\*",path );
fhandle=_findfirst(spath,& ff);
if(fhandle==-1L)
{
printf("Bad path\n");
return -1;
}
else
{
/* loop through each etry in the dir */
while(!_findnext(fhandle,& ff))
{
/* if its a sub-dir then recursivly call our self*/
if(ff.attrib&_A_SUBDIR)
{
if(strcmp(ff.name,".."))
{
sprintf(spath,"%s\\%s",pat h,ff.name) ;
scan_path(path2);
}
}
else
{
/* if its a file then */
if(ff.attrib^_A_SYSTEM)
{
printf("Found File %s\n",ff.name);
}
}
}
}
return 1;
}
this function recursivly scan directries. you will need to change it so it gets the first file only.
i donr this in VC++ on NT 4.0
*/
#include <stdio.h>
#include <io.h>
int scan_path(char *path)
{
char spath[256];
struct _finddata_t ff;
long fhandle;
sprintf(spath,"%s\\*",path
fhandle=_findfirst(spath,&
if(fhandle==-1L)
{
printf("Bad path\n");
return -1;
}
else
{
/* loop through each etry in the dir */
while(!_findnext(fhandle,&
{
/* if its a sub-dir then recursivly call our self*/
if(ff.attrib&_A_SUBDIR)
{
if(strcmp(ff.name,".."))
{
sprintf(spath,"%s\\%s",pat
scan_path(path2);
}
}
else
{
/* if its a file then */
if(ff.attrib^_A_SYSTEM)
{
printf("Found File %s\n",ff.name);
}
}
}
}
return 1;
}
check your manual for help on the findfirst fuction.
ASKER
makerp.. write somthing as an answer and I'll giv you the points..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It was werry helpful code.. Thanks.
//Micke_cap
//Micke_cap
- opendir
- readdir
- closedir
functions.