Solved

New question on  File and Directory manipulation

Posted on 2000-04-18
8
189 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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert image to byte array 8 208
Intel debugger, idb, only seeing .c and .h, but no .f90 1 352
Arduino EDI - Programming language 3 103
Please explain C language error codes ? 5 26
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…
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 pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

809 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