Solved

New question on  File and Directory manipulation

Posted on 2000-04-18
8
188 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

832 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