Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • 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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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