#include <stdio.h>
#include <dir.h>
#include <string.h>
char    newpath[MAXPATH], pathname[MAXPATH],newname[MAXPATH], oldname[MAXPATH];
char    drive[MAXDRIVE], subdir[MAXDIR], file[MAXFILE], ext[MAXEXT];
struct ffblk dta;
int main(int argc,char *argv[])
{
    int len, done, count=0;

    if(argc != 3){      /* Test for proper number of command line arguments */
        printf("\nUsage:   move file(s).ext path");
        printf("\n         The * and ? wild cards are permitted.\n");
	return(1);
    }
    strcpy(newpath,argv[2]);    /* Save a copy of the destination path */
    strcpy(subdir,argv[2]);     /* In this place too ! */
    len = strlen(newpath);      /* Get destination path length */
    if(newpath[len-1] == 92)    /* Did user supply a '\' on destination path */
       subdir[len-1] = 0;       /* If yes, remove it from the secondary copy */
      else                      /* If no, add one to primary copy */
       {
	newpath[len] = 92;      /* Add the '\' */
	newpath[len+1] = 0;     /* Don't forget to terminate it */
       }
    getcwd(newname,MAXPATH);    /* Save the directory we were called from */
    if(chdir(subdir))          /* See if the destination directory exists */
      {
       printf("Cannot change directory to %s ... quitting.",subdir);
       return 1;
      }
    chdir(newname);                         /* Go back to home directory */
    fnsplit(argv[1],drive,subdir,file,ext); /* Break up the source file name */
    sprintf(pathname,"%s%s",drive,subdir);  /* Save path of souce file(s) */
    done = findfirst(argv[1],&dta,47);      /* Go look for first file */
while(!done){
    strcpy(oldname,pathname);      /* Start "creating" the old filename */
    strcat(oldname,dta.ff_name);
    strupr(oldname);               /* Make it all upper case for DOS */
    strcpy(newname,newpath);   /* Start "creating" destination filename */
    strcat(newname,dta.ff_name);
    strupr(newname);              /* Make it upper case too */
    if(rename(oldname,newname)==0)    /* Try to rename the file */
      {                              /* If successful, ... */
	count++;                     /* Increment total files moved */
	printf("%-15s moved to %s\n",oldname,newname);   /* Notify user */
	done=findnext(&dta);         /* Look for next one */
	continue;
      }
/* If we can't rename it, A) File already exists, or B) File has permissions */
   printf("The file %s exists, do you want to overwite it (y/n) ",newname);
   len = getche();      /* Get their keypress */
   putchar('\n');
   if(len=='y' || len=='Y')
     {
      if(_chmod(newname,1,0))        /* Set file permissions to r/w */
         printf("Error changing mode of %s. %s not moved.\n",newname,oldname);
        else
          if(remove(newname))
             printf("Error removing %s. %s not moved.\n",newname,oldname);
            else
             continue;           /* After removing file, re-attempt rename */
/* If we can't change the mode or delete the file, forget it! */
     }
   done=findnext(&dta);           /* Find next file matching argv[1] */
}                                  /* End of while */
   if(count>0)                        /* Tell user how much work we did */
      printf("\nNumber of files moved: %3d\n",count);
    else
      printf("No files match.\n");
}
/*  Program compiled using TURBO C VER-2.0
    Written by Shawn Antol
    AT&T Bell Labs
    312-979-5622
    att!ihlpb!santol
    My employer and I are not accountable for any damages
    resulting from the use of this program. It has been tested on
    PC Compatibles using DOS 3.1 and DOS 3.2 and found to have no
    no known bugs.
*/


