home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / move.src < prev    next >
C/C++ Source or Header  |  1989-03-18  |  4KB  |  84 lines

  1. #include <stdio.h>
  2. #include <dir.h>
  3. #include <string.h>
  4. char    newpath[MAXPATH], pathname[MAXPATH],newname[MAXPATH], oldname[MAXPATH];
  5. char    drive[MAXDRIVE], subdir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  6. struct ffblk dta;
  7. int main(int argc,char *argv[])
  8. {
  9.     int len, done, count=0;
  10.  
  11.     if(argc != 3){      /* Test for proper number of command line arguments */
  12.         printf("\nUsage:   move file(s).ext path");
  13.         printf("\n         The * and ? wild cards are permitted.\n");
  14.     return(1);
  15.     }
  16.     strcpy(newpath,argv[2]);    /* Save a copy of the destination path */
  17.     strcpy(subdir,argv[2]);     /* In this place too ! */
  18.     len = strlen(newpath);      /* Get destination path length */
  19.     if(newpath[len-1] == 92)    /* Did user supply a '\' on destination path */
  20.        subdir[len-1] = 0;       /* If yes, remove it from the secondary copy */
  21.       else                      /* If no, add one to primary copy */
  22.        {
  23.     newpath[len] = 92;      /* Add the '\' */
  24.     newpath[len+1] = 0;     /* Don't forget to terminate it */
  25.        }
  26.     getcwd(newname,MAXPATH);    /* Save the directory we were called from */
  27.     if(chdir(subdir))          /* See if the destination directory exists */
  28.       {
  29.        printf("Cannot change directory to %s ... quitting.",subdir);
  30.        return 1;
  31.       }
  32.     chdir(newname);                         /* Go back to home directory */
  33.     fnsplit(argv[1],drive,subdir,file,ext); /* Break up the source file name */
  34.     sprintf(pathname,"%s%s",drive,subdir);  /* Save path of souce file(s) */
  35.     done = findfirst(argv[1],&dta,47);      /* Go look for first file */
  36. while(!done){
  37.     strcpy(oldname,pathname);      /* Start "creating" the old filename */
  38.     strcat(oldname,dta.ff_name);
  39.     strupr(oldname);               /* Make it all upper case for DOS */
  40.     strcpy(newname,newpath);   /* Start "creating" destination filename */
  41.     strcat(newname,dta.ff_name);
  42.     strupr(newname);              /* Make it upper case too */
  43.     if(rename(oldname,newname)==0)    /* Try to rename the file */
  44.       {                              /* If successful, ... */
  45.     count++;                     /* Increment total files moved */
  46.     printf("%-15s moved to %s\n",oldname,newname);   /* Notify user */
  47.     done=findnext(&dta);         /* Look for next one */
  48.     continue;
  49.       }
  50. /* If we can't rename it, A) File already exists, or B) File has permissions */
  51.    printf("The file %s exists, do you want to overwite it (y/n) ",newname);
  52.    len = getche();      /* Get their keypress */
  53.    putchar('\n');
  54.    if(len=='y' || len=='Y')
  55.      {
  56.       if(_chmod(newname,1,0))        /* Set file permissions to r/w */
  57.          printf("Error changing mode of %s. %s not moved.\n",newname,oldname);
  58.         else
  59.           if(remove(newname))
  60.              printf("Error removing %s. %s not moved.\n",newname,oldname);
  61.             else
  62.              continue;           /* After removing file, re-attempt rename */
  63. /* If we can't change the mode or delete the file, forget it! */
  64.      }
  65.    done=findnext(&dta);           /* Find next file matching argv[1] */
  66. }                                  /* End of while */
  67.    if(count>0)                        /* Tell user how much work we did */
  68.       printf("\nNumber of files moved: %3d\n",count);
  69.     else
  70.       printf("No files match.\n");
  71. }
  72. /*  Program compiled using TURBO C VER-2.0
  73.     Written by Shawn Antol
  74.     AT&T Bell Labs
  75.     312-979-5622
  76.     att!ihlpb!santol
  77.     My employer and I are not accountable for any damages
  78.     resulting from the use of this program. It has been tested on
  79.     PC Compatibles using DOS 3.1 and DOS 3.2 and found to have no
  80.     no known bugs.
  81. */
  82.  
  83.  
  84.