删除指定目录下(包含子目录)的文件

类别:编程语言 点击:0 评论:0 推荐:

源代码如下:
/*Project:
 *Author: fuweilin  [email protected]
 *Date:   2005-1-28
 */
 #include <stdio.h>
 #include <conio.h>
 #include <dir.h>
 #include <io.h>

 #include <dos.h>
 #define TRUE 1;
 #define FALSE 0;
 char * path;     /* path */
 char * fileName; /* file */
 void showInfo(void)
 {
     textbackground(BLUE);
     clrscr();
     window(15,2,65,12);
     textbackground(YELLOW);
     clrscr();
     textcolor(RED);
     clrscr();
     cprintf("\n                 <DelFile>\n\n");
     textcolor(RED);
     cprintf("\rAuthor:FUWEILIN [email protected]\n\n");
     cprintf("\rDate  :2005-1-28\n\n");
     cprintf("\rVer   :1.0 Build:0025\n\n");
     textcolor(BLACK);
     cprintf("\rFunction:Delete a file under the path:\n");
     window(5,14,75,24);
     textbackground(BLACK);
     textcolor(WHITE);
     clrscr();
     cprintf("\rUsage:DelFile path filename. \n\n");
     cprintf("\rExample:DelFile -d:\\abc\\ def.txt \n\n");
     cprintf("\rIt's will delete all named 'def.txt' file under d:\\abc\\ \n\r");
     cprintf("\r\n\n\n\n\n");
  }
  void usage()
  {
     printf("\n\r\t\t DELFILE \n");
     printf("Author:fuweilin [email protected]\n");
     printf("Date:2005-1-28\n");
     printf("Ver:1.0\n");
     printf("Funtion:Delete all files named 'filename' under the path\ n");
     printf("Usage:delfile [path] filename\n");

  }
  int validateArg(int argc,char *argv[])
  {

    switch (argc)
    {
      case 2:
 path = ".";
 fileName = argv[1];
 break;
      case 3:
       path = argv[1];
 fileName = argv[2];
 break;
      default:
 printf("arg error!refence the usage!\n\r");
 return 0;
    }
    return 1;


  }


 int findfile(path,file)
 {
    char filename[MAXDIR];  char dir[MAXDIR];
    int rtn;
    struct ffblk ff;

    /*find all dir*/

    strcpy(filename,path);
    strcat(filename,"\\*.*");
    rtn = findfirst(filename,&ff,FA_DIREC);
    while(rtn == 0)
    {

       if ( ((ff.ff_attrib & 0xF0) == 0x10) && (strcmp(ff.ff_name,".")) && (strcmp(ff.ff_name,".."))  )
       {
  strcpy(dir,path);
  strcat(dir,"\\");
  strcat(dir,ff.ff_name);
  /*find into sub dir*/
  findfile(dir,file);

       }
       rtn = findnext(&ff);
    }

    /*find all file*/
    strcpy(filename,path);
    strcat(filename,"\\");
    strcat(filename,file);
    rtn = findfirst(filename,&ff,FA_SYSTEM|FA_HIDDEN|FA_ARCH|FA_RDONLY);
    while(rtn == 0)
    {
       printf("%s\\%s\t",path,ff.ff_name);
       /*delete the file */
       strcpy(dir,path);
       strcat(dir,"\\");
       strcat(dir,ff.ff_name);
       if(unlink(dir)!=0)
  printf("delete %s failed!\n",dir);
       else
 printf("delete %s success!\n",dir);
       rtn = findnext(&ff);
    }

  }

  int main(int argc,char * argv[])

  {
    int rtn;
    char dir[MAXDIR];
    showInfo();
    printf("\r\n");
   /* usage(); */
    if (validateArg(argc,argv)==0)
      return 0;
    printf("path is '%s',filename is '%s'\n",path,fileName);
    findfile(path,fileName);
    printf("thank you,88.");
   }

本文地址:http://com.8s8s.com/it/it23379.htm