Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Search and Delete 0 byte Files in subdirectories

Posted on 2004-10-21
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
LVL 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 1400 total points
ID: 12372346

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

ID: 12372744

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 46

Accepted Solution

Kent Olsen earned 1400 total points
ID: 12372974

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,

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.


Expert Comment

ID: 12373037

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

ID: 12373328
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.



Assisted Solution

ankuratvb earned 600 total points
ID: 12378880

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

618 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