home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / XMOVE.ZIP / XMOVE.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1989-08-27  |  6.6 KB  |  240 lines

  1. {$M 32864,0,655360}
  2. Program XMOVE(input,output);
  3.  
  4. {
  5.   This program was written by John Gatewood Ham 7/31/89 using
  6.   Turbo Pascal 5.5.
  7.   "Created using Turbo Pascal, Copyright (c) Borland International
  8.   1987,1988."
  9. }
  10.  
  11. uses dos;
  12. const okletter:set of char = ['a'..'z','A'..'Z','0'..'9','?'];
  13. type dirrec_pointer = ^dirrec;
  14.      dirrec = record
  15.                 path:string;
  16.                 next:dirrec_pointer;
  17.               end;
  18.  
  19. var name1,
  20.     name2,
  21.     parm1:string;
  22.     original_dir:dirstr;
  23.     original_name:namestr;
  24.     original_extension:extstr;
  25.     top:dirrec_pointer;
  26.  
  27. Procedure getlegal;
  28. begin
  29.   writeln('Created using Turbo Pascal, Copyright (c) Borland',
  30.           ' International 1987,1988.');
  31. end;
  32.  
  33. Procedure rename_a_file(oldname,newname:string;var errcode:integer);
  34. var regs:registers;
  35. begin
  36.    oldname:=fexpand(oldname);
  37.    newname:=fexpand(newname);
  38.    oldname:=oldname+#0;
  39.    newname:=newname+#0;
  40.    regs.ah:=$56;
  41. {must be var[1] because var[0] is length byte and we don't
  42.  want that byte included in the file name}
  43.    regs.ds:=seg(oldname[1]);
  44.    regs.dx:=ofs(oldname[1]);
  45.    regs.es:=seg(newname[1]);
  46.    regs.di:=ofs(newname[1]);
  47.    msdos(regs);
  48.    if (regs.flags and fcarry) = 1 then
  49.      begin
  50.       case regs.ax of
  51.         2 : writeln('File not found');
  52.         3 : writeln('Path not found');
  53.         5 : writeln('Access Denied; Target file exists');
  54.       else
  55.         writeln('error number ',regs.ax);
  56.       end;
  57.       errcode:=regs.ax;
  58.      end
  59.    else
  60.      errcode:=0;
  61. end;
  62.  
  63. procedure push(item:string);
  64. var    newdragitem,dragitem,prevdragitem:dirrec_pointer;
  65. begin
  66.   if memavail <= sizeof(newdragitem) then
  67.      begin
  68.         writeln('Error: Out of Memory.  XMOVE Aborted.');
  69.         halt;
  70.      end;
  71.   new(newdragitem);
  72.   newdragitem^.path:=item;
  73.   newdragitem^.next:=nil;
  74.   dragitem:=top;
  75.   prevdragitem:=nil;
  76.   while dragitem <> nil do
  77.     begin
  78.       prevdragitem:=dragitem;
  79.       dragitem:=dragitem^.next;
  80.     end;
  81.   if prevdragitem=nil then
  82.       top:=newdragitem
  83.   else
  84.       prevdragitem^.next:=newdragitem;
  85. end;
  86.  
  87. procedure makepath(pathname:string);
  88. var q,res:integer;
  89. begin
  90.   pathname:=copy(pathname,1,length(pathname)-1);
  91.   writeln('Creating path "',pathname,'"');
  92.   {$i-}
  93.   mkdir(pathname);
  94.   {$i+}
  95.   res:=ioresult;
  96.   if res = 3 then
  97.     {need to make this things parent}
  98.     begin
  99.       q:=length(pathname);
  100.       while pathname[q] <> '\' do
  101.         q:=q-1;
  102.       makepath(copy(pathname,1,q));
  103.       {$i-}
  104.       mkdir(pathname);
  105.       {$i+}
  106.       res:=ioresult;
  107.     end;
  108.   if res <> 0 then
  109.     writeln('ioresult="',res,'"');
  110.   push(pathname);
  111. end;
  112.  
  113. procedure searchdirectory(filename:string;
  114.                           recurse:boolean);
  115. var
  116.   oldname,newname:string;
  117.   nextdirectory:string;
  118.   tempdirectory:string;
  119.   currentdta   :searchrec;
  120.   dir:dirstr;
  121.   name:namestr;
  122.   extension:extstr;
  123.   searchspec:string;
  124.   results:integer;
  125.  
  126.   function created_directory:boolean;
  127.   var  skip:boolean;
  128.        dragpointer:dirrec_pointer;
  129.   begin
  130.     skip:=false;
  131.     dragpointer:=top;
  132.     repeat
  133.       if dragpointer^.path=dir+currentdta.name then
  134.             skip:=true;
  135.       dragpointer:=dragpointer^.next;
  136.     until skip or (dragpointer=nil);
  137.     created_directory:=skip;
  138.   end;
  139.  
  140. begin
  141.   filename:=fexpand(filename);
  142.   fsplit(filename,dir,name,extension);
  143.   searchspec:=name+extension;
  144.   if recurse then
  145.   begin
  146.       tempdirectory:=dir + '*.*';
  147.       findfirst(tempdirectory,anyfile,currentdta);
  148.       while (doserror <> 2) and (doserror <> 18) do
  149.         begin
  150.           if ( ( (currentdta.attr and Directory) = Directory )
  151.           and (currentdta.name[1] <> '.'))
  152.           then
  153.             begin
  154.               if not created_directory then
  155.                 begin
  156.                   filename:=dir+currentdta.name+'\'+searchspec;
  157.                   searchdirectory(filename,
  158.                                  recurse);
  159.                 end;
  160.             end;
  161.           findnext(currentdta);
  162.         end;
  163.   end;
  164.  
  165.   tempdirectory:=dir+searchspec;
  166.   findfirst(tempdirectory,anyfile,currentdta);
  167.  
  168.    while (doserror = 0) do
  169.     begin
  170.       if (not ( (currentdta.attr and Directory) = Directory )) and
  171.          (currentdta.name[1] <> '.') then
  172.        begin
  173.          oldname:=dir+currentdta.name;
  174.          newname:=name2+copy(dir,length(original_dir)+1,length(dir)-
  175.                   length(original_dir)+1)+currentdta.name;
  176.          writeln('Renaming "',oldname,'" --> "',newname,'"');
  177.          rename_a_file(oldname,newname,results);
  178.          if results = 3 then
  179.          {make the path}
  180.            begin
  181.              makepath(name2+copy(dir,length(original_dir)+1,length(dir)-
  182.                       length(original_dir)+1));
  183.              rename_a_file(oldname,newname,results);
  184.              if results <> 0 then
  185.                 begin
  186.                    writeln('Fatal Error: Abort....');
  187.                    halt;
  188.                 end;
  189.            end;
  190.        end;
  191.       findnext(currentdta);
  192.     end;
  193.  end;
  194.  
  195. begin
  196.   top:=nil;
  197.   getlegal;
  198.   if paramcount < 2 then
  199.      begin
  200.        writeln('XMOVE Utility by John Gatewood Ham');
  201.        writeln('Usage is:');
  202.        writeln('         --Source--             --Target--');
  203.        writeln('XMOVE [drive:][path\]filespecs [drive:]path [/S]');
  204.        writeln('XMOVE will move all files matching filespecs from the source');
  205.        writeln('directory to the target directory on a drive, creating the');
  206.        writeln('Target directory if necessary.  The /S switch will cause the');
  207.        writeln('Program to recur through subdirectorys.  Both Source and');
  208.        writeln('Target must be on the same drive.');
  209.        halt;
  210.      end;
  211.    name1:=paramstr(1);
  212.    name2:=paramstr(2);
  213.    if name2[length(name2)] <> '\' then
  214.       name2:=name2+'\';
  215.    name1:=fexpand(name1);
  216.    fsplit(name1,
  217.           original_dir,
  218.           original_name,
  219.           original_extension);
  220.    name2:=fexpand(name2);
  221.    push(name2); { if name2 does not exist it will get pushed again, but
  222.                   that doesn't hurt anything and you could check for
  223.                   duplicates in the push but I'm lazy }
  224.    if name1[1] <> name2[1] then
  225.       begin
  226.         writeln('XMOVE can only move files from one place on a drive to');
  227.         writeln('another.  Use DOS XCOPY to copy files to another drive.');
  228.         halt;
  229.       end;
  230.    parm1:=paramstr(3);
  231.    if paramcount = 3 then
  232.       if (parm1 = '/s') or
  233.          (parm1 = '/S') then
  234.           searchdirectory(name1,true)
  235.       else
  236.           searchdirectory(name1,false)
  237.    else
  238.       searchdirectory(name1,false);
  239. end.
  240.