• C

Search and Delete 0 byte Files in subdirectories

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kent OlsenData Warehouse Architect / DBACommented:

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

bmjkAuthor Commented:

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.
Kent OlsenData Warehouse Architect / DBACommented:

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,


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.


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
bmjkAuthor Commented:
I wish this was homework....at 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.



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);
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.