Logo 
Search:

C Programming Articles

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

Program read a file name, serach it in C Drive, if multiple files are found, then display a list of these files with path on the screen...

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

Write a program read a file name, search it in C Drive, if multiple files are found, then display a list of these files with path on the screen, then prompt the user for file number to delete.

Code for Program read a file name, serach it in C Drive, if multiple files are found, then display a list of these files with path on the screen... 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);

 FILE* oFile;

 int file_counter=0;

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

    int cur_drive=getdisk( );

    clrscr( );

    getcwd(CurrentDirectory,MAXPATH);

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

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

    oFile=fopen("E:\\TC\\BIN\\Search.txt","wt");

    fclose(oFile);

    setdisk(2);

    if(getdisk( )==2)
       FindFile(Drive,FileName);

    if(file_counter==0)
       printf("No File Found.");

    else if(file_counter==1)
    {
       char Choice=NULL;
       char Temp[MAXPATH]={NULL};

       oFile=fopen("E:\\TC\\BIN\\Search.txt","rt");

       fgets(Temp,MAXPATH,oFile);

       fclose(oFile);

       Temp[(strlen(Temp)-1)]=NULL;

       printf("File Found:: %s\n",Temp);
       printf("\nDo you want to delete it? (Y/N) : ");

       Choice=getche( );

       if(Choice=='Y' || Choice=='y')
       {
      if(DeleteFile(Temp))
      {
         printf("\n\nUnable to delete the specified file.");

         getch( );
      }
       }
    }

    else
    {
       int counter=0;
       int file_number=0;

       char Choice=NULL;
       char Temp[MAXPATH]={NULL};

       oFile=fopen("E:\\TC\\BIN\\Search.txt","rt");

       printf("The Files Found are ::\n");

       for(counter=1;counter<=file_counter;counter++)
       {
      fgets(Temp,MAXPATH,oFile);

      printf("%d : %s",counter,Temp);

      if((counter%15)==0)
      {
         printf("\n Press any key to continue...");

         getch( );

         printf("\n\n");
      }
       }

       fclose(oFile);

       printf("\nEnter the File Number to Delete = ");
       scanf("%d",&file_number);

       oFile=fopen("E:\\TC\\BIN\\Search.txt","rt");

       strset(Temp,NULL);

       for(counter=1;counter<=file_number;counter++)
      fgets(Temp,MAXPATH,oFile);

       fclose(oFile);

       Temp[(strlen(Temp)-1)]=NULL;

       printf("\nSelected File :: %s\n",Temp);
       printf("\nDo you want to delete it? (Y/N) : ");

       Choice=getche( );

       if(Choice=='Y' || Choice=='y')
       {
      if(DeleteFile(Temp))
      {
         printf("\n\nUnable to delete the specified file.");

         getch( );
      }
       }
    }

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

    setdisk(cur_drive);
    chdir(CurrentDirectory);

    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)
       {
      char Temp[MAXPATH]={NULL};

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

      oFile=fopen("E:\\TC\\BIN\\Search.txt","at");

      fputs(Temp,oFile);
      fputs("\n",oFile);

      fclose(oFile);

      file_counter++;

      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 read a file name, serach it in C Drive, if multiple files are found, then display a list of these files with path on the screen... 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!