C Program to show files in Directories.

 # 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;
 }

Post a Comment

0 Comments