Solved

New question on  File and Directory manipulation

Posted on 2000-04-18
8
185 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.

758 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

17 Experts available now in Live!

Get 1:1 Help Now