[comp.binaries.ibm.pc] v01i229: move, a file moving utility, C source

santol@ihlpb.att.com (Shawn Antol) (03/16/89)

Checksum: 2830838538  (Verify or update with "brik")
Posting-number: Volume 01, Issue 229
Originally-from: santol@ihlpb.att.com (Shawn Antol)
Submitted-by: santol@ihlpb.att.com (Shawn Antol)
Archive-name: move_sa/move.txt

Hello world. This is my first post so please forgive me if something is
FUBAR.  I am new to the net and I didn't see a place to upload PC
related source code so I figured this was the next best place.

WHAT MOVE DOES:

MOVE allows DOS users the ability to MOVE files from one directory to
another with a minimum of effort.  No fancy windows or selection
prompts, just bump and grind command line arguments to speed up its
execution.

FEATURES:

  1)   MOVE is extremely FAST (because it uses the rename() function)
  2)   The * and ? are fully supported.
  3)   If a file in the destination directory exists under the same name, MOVE
       will prompt the user if he/she wants to overwrite it.
  4)   Files with special attributes are handled accordingly.
Note:  Compile using Turbo-C. I used Version 2.0 but I don't think I used
       any functions that are not in earlier versions.

This program utility CANNOT cross disk drives.   I.e, you cannot issue
the following command:   move c:\foo.txt d:\junk

The reason is:  The MOVE utility is LIGHTNING fast because it doesn't
actually move the data, but it moves the directory entry pointing to
the data. Thus, to "move" data to a different drive physically requires
that the data be moved. The program wasn't written to work that way.

                           Shawn Antol
                           att!ihlpb!santol
                           312-979-5622
			   ATT - BELL LABS

[
Since this posting is a single source file, here it is, in raw text
form.  Save the lines between the BEGIN--cut and END--cut lines into
move.c.  Not tested.

checksum     size (bytes)  file (between BEGIN--cut and END--cut lines)
   50519         3647      this source posting

-- R.D.
]

BEGIN--cut here--cut here
#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.
*/
END--cut here--cut here