Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

New question on  File and Directory manipulation

Posted on 2000-04-18
8
Medium Priority
?
197 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
[X]
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
  • 4
  • 3
8 Comments
 
LVL 2

Accepted Solution

by:
fremsley earned 200 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

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.

Question has a verified solution.

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

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 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 while-loops in the C programming language.

705 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