Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Interrupt ProgrammingRSS Feeds

Program to read a file name, serach it in the whole Hard Drive and delete it if found

Posted By: Easy Tutor     Category: C Programming     Views: 2717

Write a program to read a file name, serach it in the whole Hard Drive and delete it if found.

Code for Program to read a file name, serach it in the whole Hard Drive and delete it if found in C Programming

 # include <string.h>
 # include  <stdio.h>
 # include  <conio.h>
 # include    <dos.h>
 # include    <dir.h>


 void FindFile(constchar* Path,constchar* FileName);

 constint DeleteFile(constchar* File);

 int main( )
 {
    int count;
    int cur_drive=getdisk( );
    int total_drives=setdisk(2);

    char Drive[10]={"C:\\"};
    char FileName[15]={NULL};
    char CurrentDirectory[MAXPATH]={NULL};

    clrscr( );

    getcwd(CurrentDirectory,MAXPATH);

    printf("Enter the File Name to delete: ");
    scanf("%s",FileName);

    printf("\nSearching....\n\n");

    for(count=2;count<total_drives;count++)
    {
       setdisk(count);

       if(count==getdisk( ))
       {
      Drive[0]=(65+count);

      FindFile(Drive,FileName);
       }
    }

    setdisk(cur_drive);
    chdir(CurrentDirectory);

    printf("\nPress any key to exit...");

    getch( );
    return 0;
 }

 /*************************************************************************/
 //----------------------------  FindFile( )  ----------------------------//
 /*************************************************************************/

 void FindFile(const char* Path,const char* FileName)
 {
    struct find_t ffblk;

    int flag;

    char File[MAXPATH]={NULL};

    chdir(Path);

    strcpy(File,Path);
    strcat(File,FileName);

    flag=_dos_findfirst(File,_A_NORMAL,&ffblk);

    if(!flag)
    {
       char Choice;

       while(!flag)
       {
      printf("File Found:: %s%s\n",Path,ffblk.name);
      printf("Do you want to delete it? (Y/N) : ");

      Choice=getche( );

      if(Choice=='Y' || Choice=='y')
      {
         char Temp[MAXPATH]={NULL};

         strcpy(Temp,Path);
         strcat(Temp,ffblk.name);

         if(DeleteFile(Temp))
         {
        printf("Unable to delete the specified file.");

        getch( );
         }
      }

      printf("\n\n");

      flag=_dos_findnext(&ffblk);
       }
    }

    strcpy(File,Path);
    strcat(File,"*.*");

    flag=_dos_findfirst(File,FA_DIREC,&ffblk);

    while(!flag)
    {
       if(strcmp(ffblk.name,".")!=0 && strcmp(ffblk.name,"..")!=0 &&
                              ffblk.attrib==FA_DIREC)
       {
      char Temp[MAXPATH]={NULL};

      strcpy(Temp,Path);
      strcat(Temp,ffblk.name);
      strcat(Temp,"\\");

      FindFile(Temp,FileName);
       }

       flag=_dos_findnext(&ffblk);
    }

    chdir(Path);
 }

 /*************************************************************************///---------------------------  DeleteFile( )  ---------------------------///*************************************************************************/constint DeleteFile(constchar* File)
 {
    union REGS InReg;
    union REGS OutReg;

    InReg.h.ah=0x41;
    InReg.x.dx=FP_OFF(File);

    int86(0x21,&InReg,&OutReg);

    return OutReg.x.cflag;
 }

  
Share: 



Easy Tutor
Easy Tutor author of Program to read a file name, serach it in the whole Hard Drive and delete it if found is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!