• C

Files

How to get all existing files on a directory? (Something like a 'dir' command, but i need to keep the filenames.
lifesonAsked:
Who is Participating?
 
marcjbCommented:
/*
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
 
AlexVirochovskyCommented:
You must use findfirst/findnext function.:

    struct ffblk  fileinfo;

    int done =!findfirst("*.*", &fileinfo, 0))
    while (!done)
        {
           .....
          done = findnext(&fileinfo);//next file
        }
 
0
 
AlexVirochovskyCommented:
Of couse 1-st line
 ine done = findfirst(..);
Alex
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
lifesonAuthor Commented:
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
 
lifesonAuthor Commented:
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
 
lifesonAuthor Commented:
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
 
Answers2000Commented:
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
 
lifesonAuthor Commented:
Sorry, man! I didn't explain. It's not for Win32, it's for DOS!
0
 
Answers2000Commented:
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
 
AlexVirochovskyCommented:

    struct ffblk  fileinfo;

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

0
 
viktornetCommented:
how about something like this??

system("dir > Datafile.dat");

Merry Christmas!

-Viktor
--Ivanov
0
 
AlexVirochovskyCommented:
lifeson , do you test my last comment? if it is OK, i can write
it as reply?
Happy new year!
0
 
marcjbCommented:
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
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.

All Courses

From novice to tech pro — start learning today.