Search and Delete 0 byte Files in subdirectories

Posted on 2004-10-21
Last Modified: 2012-06-21
Im new to C. I need code to delete 0 byte files from the current directory, and if an argument is given, any subdirectories of the directory the executable is in also. I need this to work on DOS and Windows. It will do more than this, but thats the part im stuck on. Im using lcc-win32 as my compiler. Whats the easiest way to do this?
Question by:bmjk
    LVL 45

    Assisted Solution


    Traversing the local directory is a piece of cake.  call findfirst() and findnext() to scan the list of files in the directory.  Every time findfirst() of findnext() returns a name, you're going to want to call stat() to find out if it's a file or a directory.

    If it's a directory, the ffblk structure returned by stat() will have a length field.  Interrogate it to see if you want to delete the file.

    The tough part comes when the returned name is a directory.  You're going to have to make a list of them, and then interrogate each one of them after you process all of the files in the current directory.  (Remember, if any of these directories has a subdirectory you'll need to recursivedly process them, too.)

    The tough part is that the find findfirst()findnext() process deals with one directory at a time.  It's up to you to track which directory is being processed.

    There is a recursive varaint, but I'm not aware of it being ANSI (and therefore, universal).


    Author Comment


    That sounds good in concept, but I'm really lost on the C stuff... what header files do i need for this? I dont even know how to delete a file in C. All I know is the basic systax. I mainly code in VB. I could write the this no problem in that langauge, but I need it in C.
    LVL 45

    Accepted Solution


    Well, that makes it sound more like a homework assignment, in which case I can offer less and less.  But I'll still help as much as the rules allow!  :)

    Very few programs of any size/complexity can get along without stdlib.h and stdio.h

    #include <stdlib.h>
    #include <stdio.h>

    Directory management/access requires dir.h.

    #include <dir.h>

    stat() requires sys\stat.h

    #include <sys\stat.h>

    Next, you're going to need to build a recursive function to scan all of the files in a directory.

    ScanDirectory (char *PathName)  /*  Name of the directory to scan  */
      struct ffblk ffblk;

      if (findfirst(const char *pathname, struct ffblk &ffblk, FA_DIREC))   /*  Scan files and directories  */
        return;    /* Nothing found  */

      while (1)
        if (ffblk->st_mode & S_IFDIR)
          /*  Entry is a directory.  Put it on a list  */
          /*  Entry is a file.  Check its length and optionally delete it  */

      /*  Now work your way down the directory list and call ScanDirectory() on each name on the list  */

    This should get you a good start,

    LVL 9

    Expert Comment


    To get you started:

    Use the remove() function from stdio.h to delete a file.
    int res;

    On success,remove() returns 0.On error it returns -1.

    For stat,you need sys/stat.h,Check up on your compiler though.

    for findfirst(),findnext(),you need to include dos.h or dir.h on Borland Compilers.
    On VC++,you need windows.h

    Author Comment

    I wish this was least then I'd have a book to fall back on. I just cant do it in vb because I need it on a DOS machine, and I have a C compiler. That was a lot of help, I just have 2 more questions about it.

    1. can you explain this: (ffblk->st_mode & S_IFDIR)

    2. I assume I can find the length in this ffblk. I'll look around for that. How do I delete the file? I found a remove() function somewhere. Will that work with this? Is there something easier to use for example something that would use that handle from findfirst()?

    Otherwise, I think this gives me enough to figure it out. The biggest problem I was having was not knowing the header files... If you dont know those, its hard to figure out what functions do what you need.


    LVL 9

    Assisted Solution


    This is the stat struct which contains all the statistics you need.

     struct stat {
       short  st_dev,   st_ino;
       short  st_mode,  st_nlink;
       int    st_uid,   st_gid;
       short  st_rdev;
       long   st_size,  st_atime;
       long   st_mtime, st_ctime;

     Element     What It Is
     st_dev        Drive number of disk containing
              the file, or file handle if the
              file is on a device
     st_mode     Bit mask giving information
              about the open file's mode
     st_nlink      Set to the integer constant 1
     st_rdev      Same as st_dev
     st_size       Size of the open file in bytes
     st_atime     Most recent time the open file
              was modified
     st_mtime    Same as st_atime
     st_ctime     Same as st_atime

    Here's an example
    #include <sys\stat.h>
    #include <stdio.h>

    #define FILENAME "TEST.$$$"

    int main(void)
       struct stat statbuf;
       FILE *stream;

       /* open a file for update */
       if ((stream = fopen(FILENAME, "w+")) == NULL)
          fprintf(stderr, "Cannot open output file.\n");

       /* get information about the file */
       stat(FILENAME, &statbuf);
       printf("Size of file in bytes: %ld\n", statbuf.st_size);
       return 0;

    As stated in my last comment,you can use remove() to delete a file.

    >(ffblk->st_mode & S_IFDIR)

    st_mode is a field in the ffblk struct that tells you what kind of an entry it is,a file or a directory etc.

    S_IFDIR is the constant that defines the mode value for a directory.
    >if (ffblk->st_mode & S_IFDIR)

    This checks whether the entry found by findfirst,findnext is a directory.
    Using ffblk->ff_name,you get the name of the file,which you can remove using the remove function.
    SOmething like remove(ffblk->ff_name);

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

    875 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

    12 Experts available now in Live!

    Get 1:1 Help Now