Thread: Getting a file list from a hard disk?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    Getting a file list from a hard disk?

    Hi all,

    I need to scan the contents of a hard drive on a computer and return the list of files on that drive. I'm able to scan the first level of hierarchy for a given drive letter, and am using a fairly primitive means of determining which is a folder and which is a file (presence of a period) and it works for most things, although will need to be changed later. However, I'm going to need to traverse the entire hierarchy and I'm a little unsure as to how I'm supposed to do it.

    Can anyone provide any hints/pointers on how to approach this?

    Thanks.
    Last edited by osiris^; 03-07-2010 at 08:31 AM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You need to mention your OS.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by MK27 View Post
    You need to mention your OS.
    Argh, sorry. The program needs to run on Windows XP.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Open a console window (DOS window). Type:

    dir *.* /s >all_files.txt

    and hit enter. All files (except hidden, I believe), on the HD will be listed in the new all_files.txt file.

    Before you do this, you should experiment and study up on all the dir options that Windows has. Enter
    dir /?
    to see the extensive list.

    You can sort the files by name, date, type, size, etc., and have them organized in different ways. You can also include or exclude the hidden and system files, etc.

    Very useful.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by Adak View Post
    Open a console window (DOS window). Type:

    dir *.* /s >all_files.txt

    and hit enter. All files (except hidden, I believe), on the HD will be listed in the new all_files.txt file.

    Before you do this, you should experiment and study up on all the dir options that Windows has. Enter
    dir /?
    to see the extensive list.

    You can sort the files by name, date, type, size, etc., and have them organized in different ways. You can also include or exclude the hidden and system files, etc.

    Very useful.
    I was aware of this, but is there anyway to embed it into a C program? It's part of a larger problem and thus I need to be able to visit every file on the hard disk.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    Code:
    system("dir *.* /s >all_files.txt") ;
    Or, are you wanting to do this yourself and reinvent the wheel?
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Ah this looks like what I was looking for.

    Thanks.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What do you mean by "visit every file"?? <eek!>

    What does your program do?

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by Adak View Post
    What do you mean by "visit every file"?? <eek!>

    What does your program do?
    The end result has to be a virus scanner, or at least something that is capable of looking inside files for certain patterns/signatures.

  11. #11
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    What you need to do is implement a recursive "find" function, that will recursively traverse the directory structure, this is the fastest way I know of finding every file in a filesystem structure

    Here is some example code I wrote that executes a supplied function on every file within a supplied path including all sub-directories, ie so you could give it the root path / to get all files in the filesystem....

    This example is for a Linux machine using the dirent structure:
    Code:
    /* These are the macros being used in the function below */
    #define ff_get_dname(d) ((d)->d_name)  /* just change it once here :) if needbe */
    #define ff_fmt_str(p) (*((p)+strlen((p))-1)=='/'?"%s%s":"%s/%s")     
    #define ff_overhead 2 /* \0 and possible / so we add 2 to the string buffer */

    Code:
    int find_path(int (*exec)(const char *), const char * path)
    {
      DIR * dptr;                  /* directory pointer */
      register struct dirent * cur_dirent;  /* current entry in directory */
      struct stat sb;              /* information about entry, type etc.. */
      register char * pathbuf;              /* string buffer for constructing filepath */
      register char * p;                    /* working pointer */
      size_t allocsiz;             /* holds size to currently allocate */
    
      if (!(dptr = opendir(path)))
        return 1;
    
      while (cur_dirent = readdir(dptr)) { /* intentional = */
        allocsiz = strlen(path)+strlen(ff_get_dname(cur_dirent))+ff_overhead;;
        if (!(pathbuf = malloc(allocsiz)))
          continue; /* skip file, couldn't get that much memory */
        sprintf(pathbuf,ff_fmt_str(path),path,ff_get_dname(cur_dirent));
    
        if (lstat(pathbuf,&sb) < 0) {
          free(pathbuf); /* free up our buffer */
          continue; /* failed to stat, skip */
        }
    
        if (S_ISDIR(sb.st_mode)) {
          p = ff_get_dname(cur_dirent);
          while (*p == '.') /* skip . or .. or ...etc.. */
            p++;
          if (*p)
            find_path(exec,pathbuf);
        }
        if (S_ISREG(sb.st_mode)) /* regular file */
          (void) exec(pathbuf); /* postive value indicates some sort of error occured */
    
        free(pathbuf); /* free our string buffer */
      }
    
      closedir(dptr);
      return 0;
    }
    Some declarations might be left out but you get the idea
    Last edited by nonpuz; 03-07-2010 at 01:32 PM. Reason: added ff_* macro definitions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM