Solved

New question on  File and Directory manipulation

Posted on 2000-04-18
8
192 Views
Last Modified: 2010-04-15
I asked the question yesterday titled, "File and Directory manipulation".  The answer was for me to use the unlink cmd instead of the system command with the rm system command as its parameter.  I have a followup question.  It appears that you can not use wildcards when calling unlink s.a. unlink( somdir/*).
Am I back to using the system command again or is there a way to easily do what I want?

Thanks in advance.

0
Comment
Question by:c095276
  • 4
  • 3
8 Comments
 
LVL 2

Accepted Solution

by:
fremsley earned 50 total points
ID: 2729873
No. The Unix system calls do not handle wildcards, that's the job of the shell. You could however implement your own name matching function scanning the directories with opendir(), readdir(), and closedir().
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2730114
//next code works in Dos(for delete
directory with all files)

#include <cderr.h>
#include <string.h>
#include <dir.h>
#include <sys\stat.h>

void ClearDir(char *szFind)
{
  int done;
  char szLocalFind[255];

  strcpy(szLocalFind,szFind); strcat(szLocalFind,"\\*.*");
//delete all file
  struct ffblk ffblk;
  done = findfirst(szLocalFind,&ffblk,
       FA_ARCH | FA_SYSTEM | FA_HIDDEN);
  while (!done) //files
    {
      char szName[255];
      strcpy(szName, szFind); strcat(szName,"\\");
      strcat(szName,ffblk.ff_name);
      if (!unlink(szName)) //read only!
{
  chmod(szName,S_IREAD | S_IWRITE);
  unlink(szName);
}
      done = findnext(&ffblk);
    }
  done = findfirst(szLocalFind,&ffblk,FA_DIREC);//all subdirs
  while (!done)
    { //now dir:
      char szName[255];
      strcpy(szName, szFind); strcat(szName,"\\");
      strcat(szName,ffblk.ff_name);
      if (*ffblk.ff_name != '.' && !rmdir(szName))
{
  ClearDir(szName); //delete all sub dir
  rmdir(szName); //must be empty!
}
      done = findnext(&ffblk);
    }
}
0
 
LVL 2

Expert Comment

by:fremsley
ID: 2730217
AlexVirochovsky: I remember c095276 wants his/her program to run under Unix. The findfirst/findnext functions are only available under DOS/Windows. For Unix the code has to be re-written using the functions I mentioned above.
0
Technology Partners: 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!

 

Author Comment

by:c095276
ID: 2730367
I have tried and like the opendir(), readdir(), and closedir() suggestion, but readdir returns everything in a particular directory including other directories.  How do I differentiate between files and directories?
0
 

Author Comment

by:c095276
ID: 2730428
I have tried and like the opendir(), readdir(), and closedir() suggestion, but readdir returns everything in a particular directory including other directories.  How do I differentiate between files and directories?
0
 
LVL 2

Expert Comment

by:fremsley
ID: 2730438
You can use the stat() function on the filename which returns a struct stat that contains the mode. There are some macros defined in <sys/stat.h> with which you can evaluate that mode, e.g. S_ISDIR(mode).
0
 

Author Comment

by:c095276
ID: 2730477
I have tried and like the opendir(), readdir(), and closedir() suggestion, but readdir returns everything in a particular directory including other directories.  How do I differentiate between files and directories?
0
 

Author Comment

by:c095276
ID: 2732170
Works like a champ thanks
0

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

713 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