Solved

Files

Posted on 1998-12-15
13
225 Views
Last Modified: 2010-05-18
How to get all existing files on a directory? (Something like a 'dir' command, but i need to keep the filenames.
0
Comment
Question by:lifeson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
  • +2
13 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1255156
You must use findfirst/findnext function.:

    struct ffblk  fileinfo;

    int done =!findfirst("*.*", &fileinfo, 0))
    while (!done)
        {
           .....
          done = findnext(&fileinfo);//next file
        }
 
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1255157
Of couse 1-st line
 ine done = findfirst(..);
Alex
0
 

Author Comment

by:lifeson
ID: 1255158
I don't know how to access the filename after to do that!

my code:

int done;
struct ffblk fileinfo;
done = !findfirst("*.*", &fileinfo, 0);

while (!done)
  done = findnext(&fileinfo);
 
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!

 

Author Comment

by:lifeson
ID: 1255159
I don't know how to access the filename after to do that!

my code:

int done;
struct ffblk fileinfo;
done = !findfirst("*.*", &fileinfo, 0);

while (!done)
  done = findnext(&fileinfo);
 
0
 

Author Comment

by:lifeson
ID: 1255160
I don't know how to access the filename after to do that!

my code:

int done;
struct ffblk fileinfo;
done = !findfirst("*.*", &fileinfo, 0);

while (!done)
  done = findnext(&fileinfo);
 
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1255161
Win32 code to do this, say szDirectory is the directory you want to scan

char szCleanDir[MAX_PATH+10] ;
         char szSearchPath[MAX_PATH+5] ;
         WIN32_FIND_DATA fd ;
         HANDLE hh ;
         int ll ;
         BOOL bOK = TRUE ;

         /* Make sure dir has final slash */
         strcpy( szCleanDir, szDirectory ) ;
         ll = strlen(szCleanDir) ;
         assert( ll > 0 ) ;
         if ( szCleanDir[ll-1] != '\\' )
         {
         strcat( szCleanDir, "\\" ) ;
         }

         /* Generate search path */
         strcpy( szSearchPath, szCleanDir ) ;
         strcat( szSearchPath, "*.*" ) ;

         /* Do search */
         hh = FindFirstFile( szSearchPath, &fd ) ;
         while ( ( hh != NULL ) && ( bOK ) )
         {
         if ( ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0 )
         {
         /* it's a directory */
         char szSubDirectory[MAX_PATH+10] ;
         if ( strcmp( (char *)fd.cFileName, "." ) == 0 ) continue ;
         if ( strcmp( (char *)fd.cFileName, ".." ) == 0 ) continue ;
         strcpy( szSubDirectory, szCleanDir ) ;
         strcat( szSubDirectory, (char *)fd.cFileName ) ;
         printf( "Subdirectory '%s'\n", szSubDirectory ) ;
         } else
         {
         /* it's a file */
         printf( "File '%s'\n", (char *)fd.cFileName ) ;
         }

         bOK = FindNextFile( hh, &fd ) ;

         } /* while* /

         if ( hh != NULL ) FindClose(hh) ;


0
 

Author Comment

by:lifeson
ID: 1255162
Sorry, man! I didn't explain. It's not for Win32, it's for DOS!
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1255163
Okay - in VC.#include <io.h>

use _findfirst instead of FindFirstFile
use _findnext instead of FindNextFile
use _findclose instead of FindClose

change type of hh to a long
change tests on hh against NULL to tests against -1

change fd's type to struct _finddata_t

the filename is in the name member of this struct

attrib member of structure contains attributes of the found item (do bitwise tests)

e.g.
if ( ( fd.attrib & _A_SUBDIR ) != 0 ) { /* it's a sub dir */ }


0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1255164

    struct ffblk  fileinfo;

    int done =!findfirst("*.*", &fileinfo, 0))
    while (!done)
        {
          char *szFileName = fileinfo.ff_name;
          ..... you code
          done = findnext(&fileinfo);//next file
        }
 

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1255165
how about something like this??

system("dir > Datafile.dat");

Merry Christmas!

-Viktor
--Ivanov
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1255166
lifeson , do you test my last comment? if it is OK, i can write
it as reply?
Happy new year!
0
 
LVL 3

Accepted Solution

by:
marcjb earned 50 total points
ID: 1255167
/*
The following code will find and save all the files in a given directory.  char *dir[MAX] declares an array of pointers.  For every entry that is found in the directory, we allocate a string to store the name.  At the end of searching the directory, the number of files in the directory is 1 + ctr.  

This code assumes that no directory has more than 250 files.  If you think this is too small, simple increase MAX.

This solves the problem of keeping all the filenames.  See the printf at the end of the program.

Hope this helps :)
*/

#include<stdio.h>
#include<string.h>
#include<dir.h>

#define  MAX     250
#define  LENGTH  20

int main()
{
char *dir[MAX];
struct ffblk ffblk;
int done;
int ctr = -1;

done = findfirst("*.*",&ffblk,0);
while (!done)
{
   dir[++ctr] = (char*) malloc( LENGTH*sizeof(char) );
   strcpy(dir[ctr], ffblk.ff_name);
   done = findnext(&ffblk);
}

/* print out all the files in the directory */
while(ctr >= 0)
     printf("\n%s", dir[ctr--]);

return 0;
}
0
 
LVL 3

Expert Comment

by:marcjb
ID: 1255168
A note about my solution and the findfirst/findnext functions.
The following are the 'modes' that can be passed to the findfirst function.  This is the file atrribute of the file.

#define FA_NORMAL   0x00        /* Normal file, no attributes */
#define FA_RDONLY   0x01        /* Read only attribute */
#define FA_HIDDEN   0x02        /* Hidden file */
#define FA_SYSTEM   0x04        /* System file */
#define FA_LABEL    0x08        /* Volume label */
#define FA_DIREC    0x10        /* Directory */
#define FA_ARCH     0x20        /* Archive */

Now, the code you have has the line:

done = findfirst("*.*",&ffblk,0);

The 0 in the above represents a 'normal' file.  To get a more complete listing of a directory's contents, you may want to mask the mode.
For example:

#define FA_LIST (FA_RDONLY|FA_HIDDEN|FA_SYSTEM|FA_DIREC|FA_ARCH)

Now, if you use FA_LIST like this:

done = findfirst("*.*",&ffblk,FA_LIST);

you will get a listing of everything in the directory, including sub-directories.  If you want to know what has been returned to the ffblk structure, check ffblk.ff_attrib and compare it with the #defines listed above.  If you don't want what is returned (maybe you are not interested in hidden files for example), you simply don't allocate a string to keep that listing.

Hope this helps.
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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops 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.

724 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