Solved

Return filename from folder! (Urgent)

Posted on 2000-03-27
7
175 Views
Last Modified: 2010-04-15
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
Comment
Question by:Micke_Cap
7 Comments
 
LVL 2

Expert Comment

by:fremsley
ID: 2660560
check the manual pages for the posix

  - opendir
  - readdir
  - closedir

functions.
0
 

Author Comment

by:Micke_Cap
ID: 2660629
Thanks!
0
 
LVL 10

Expert Comment

by:makerp
ID: 2660969
/*
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Expert Comment

by:guyss
ID: 2663108
check your manual for help on the findfirst fuction.
0
 

Author Comment

by:Micke_Cap
ID: 2663262
makerp.. write somthing as an answer and I'll giv you the points..
0
 
LVL 10

Accepted Solution

by:
makerp earned 80 total points
ID: 2663271
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
 

Author Comment

by:Micke_Cap
ID: 2663431
It was werry helpful code.. Thanks.

//Micke_cap
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now