• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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
0
Micke_Cap
Asked:
Micke_Cap
1 Solution
 
fremsleyCommented:
check the manual pages for the posix

  - opendir
  - readdir
  - closedir

functions.
0
 
Micke_CapAuthor Commented:
Thanks!
0
 
makerpCommented:
/*
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",path,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;
}
      
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
guyssCommented:
check your manual for help on the findfirst fuction.
0
 
Micke_CapAuthor Commented:
makerp.. write somthing as an answer and I'll giv you the points..
0
 
makerpCommented:
cheers Micke_Cap

did that help you. i use that bit of code all the time !!!.

you can make it into quite a generic search function and bug it in your own library
0
 
Micke_CapAuthor Commented:
It was werry helpful code.. Thanks.

//Micke_cap
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now