home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / MOVE.CMM < prev    next >
Text File  |  1995-10-02  |  3KB  |  112 lines

  1. /*
  2.  * move.cmm
  3.  *
  4.  * This .CMM file implements a Move command which combines the functionality
  5.  * of Move and Rename.
  6.  */
  7.  
  8. #include "Netware.lib"
  9.  
  10. usage()
  11. {
  12.   printf("Use the MOVE command to change a file's name or directory.\n");
  13.   printf("The contents of the file(s) remains unchanged. RENAME is a\n");
  14.   printf("valid synonym for MOVE.\n");
  15.   printf("Syntax:\n");
  16.   printf("  MOVE [drive:][path]filename1 [drive:][path]filename2\n");
  17.   printf("where:\n");
  18.   printf("  drive:/path/filename1   Specifies the file to rename.\n");
  19.   printf("  drive:/path/filename2   Specifies the new filename.\n");
  20.   exit(EXIT_FAILURE);
  21. }
  22.  
  23. /* ---------------------------------------------------------------------- */
  24.  
  25. //
  26. // Move a file to the given destination.
  27. //
  28. move_file(source,dest)
  29. {
  30.   if( rename(source,dest) )
  31.     {
  32.       printf("Unable to rename file \"%s\" to \"%s\".\n",source,dest);
  33.     }
  34. }
  35.  
  36. main(argc,argv)
  37. {
  38.   if( argc!=3 ) usage();
  39.  
  40.   source = argv[1];
  41.   target = argv[2];
  42.  
  43. // First we get the source files to transfer
  44.  
  45.   orig = Directory(source);
  46.   if( orig==NULL )
  47.     {
  48.       printf("No such file or directory \"%s\"\n",source);
  49.       exit(EXIT_FAILURE);
  50.     }
  51.  
  52. // Get the destination. It may either be a directory, in which case we
  53. // move all the files into it. It may be a filename, in which case we
  54. // can only have once source file and we move it to the new one. Otherwise,
  55. // it contains wildcards and we do a block move hacking the filename as
  56. // we go.
  57.  
  58.   if( strchr(target,'*') || strchr(target,'?') )
  59.     {
  60.       printf("Wildcard destinations not yet implemented.\n");
  61.       exit(EXIT_FAILURE);
  62.     }
  63.  
  64.   if( !strcmp(target,".") )
  65.     {
  66.       dest = NULL;
  67.     } else {
  68.       dest = Directory(target);
  69.       if( target[strlen(target)-1]=='/' || target[strlen(target)-1]=='\\' )
  70.         {
  71.           target[strlen(target)-1] = '\0';
  72.           dest = Directory(target);
  73.         }
  74.  
  75.       if( dest==NULL )
  76.         {
  77. // First check to see if he specified a volume name.
  78.           if( !(isalpha(target[0]) && target[1]==':' &&
  79.                 (target[2]=='\0' || target[2]=='/' || target[2]=='\\')) )
  80.             {
  81.               if( GetArraySpan(orig)>0 )
  82.                 printf("There is no directory named \"%s\".\n",target);
  83.             }
  84.         } else {
  85.           if( GetArraySpan(dest)!=0 )
  86.             {
  87.               printf("You cannot move a file to multiple destinations.\n");
  88.               exit(EXIT_FAILURE);
  89.             }
  90.         }
  91.     }
  92.  
  93.   if( dest==NULL || dest[0].attrib & _A_SUBDIR )
  94.     {
  95.       for( i=0;i<=GetArraySpan(orig);i++ )
  96.         {
  97.           filestruct = SplitFileName(orig[i].name);
  98.           if( dest==NULL && strcmp(target,".") )
  99.              {
  100.                strcpy(tmpname,target);
  101.              } else {
  102.                sprintf(tmpname,"%s%c%s%s",dest?dest[0].name:target,
  103.                        defined(_NWNLM_)?'/':'\\',filestruct.name,filestruct.ext);
  104.              }
  105.           move_file(orig[i].name,tmpname);
  106.         }
  107.     } else {
  108.       printf("Target file already exists.\n");
  109.       exit(EXIT_FAILURE);
  110.     }
  111. }
  112.