Link to home
Start Free TrialLog in
Avatar of lifeson
lifeson

asked on

Files

How to get all existing files on a directory? (Something like a 'dir' command, but i need to keep the filenames.
Avatar of AlexVirochovsky
AlexVirochovsky

You must use findfirst/findnext function.:

    struct ffblk  fileinfo;

    int done =!findfirst("*.*", &fileinfo, 0))
    while (!done)
        {
           .....
          done = findnext(&fileinfo);//next file
        }
 
Of couse 1-st line
 ine done = findfirst(..);
Alex
Avatar of lifeson

ASKER

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);
 
Avatar of lifeson

ASKER

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);
 
Avatar of lifeson

ASKER

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);
 
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) ;


Avatar of lifeson

ASKER

Sorry, man! I didn't explain. It's not for Win32, it's for DOS!
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 */ }



    struct ffblk  fileinfo;

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

how about something like this??

system("dir > Datafile.dat");

Merry Christmas!

-Viktor
--Ivanov
lifeson , do you test my last comment? if it is OK, i can write
it as reply?
Happy new year!
ASKER CERTIFIED SOLUTION
Avatar of marcjb
marcjb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.