home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / TURBOPAS / GDEL.ZIP / GDEL.PAS
Encoding:
Pascal/Delphi Source File  |  1985-12-28  |  15.2 KB  |  535 lines

  1. program gdel;
  2.  
  3. { global delete - delete all occurrances of a file in all directories }
  4.  
  5. type LStr = string[255];
  6. var command_line : boolean;
  7.     Param_Num    : integer;
  8.     in_str       : LStr;
  9.     verify       : boolean;
  10.  
  11. { PCDOS.INC }
  12. {
  13.   PcDos functions
  14.   Turbo Pascal 3.0
  15.   IBM PC DOS 3.1
  16.  
  17.   By Michael A. Quinlan  5/11/85
  18. }
  19.  
  20. type PcDos_reg_type  = record
  21.                          case integer of
  22.                            0 : (ax, bx, cx, dx, bp, si, di, ds, es, flags : integer);
  23.                            1 : (al, ah, bl, bh, cl, ch, dl, dh            : byte)
  24.                        end;
  25.      PcDos_dta_type  = array[1..128] of byte;
  26.      PcDos_Find_Area = record
  27.                          resvd1    : array [1..21] of byte;
  28.                          attr      : byte;
  29.                          time      : integer;
  30.                          date      : integer;
  31.                          size_low  : integer;
  32.                          size_high : integer;
  33.                          name      : array [1..13] of char;
  34.                          resvd2    : array [1..85] of byte;
  35.                        end;
  36.      PcDos_dta_ptr   = ^PcDos_dta_type;
  37.      PcDos_Asciiz    = array [1..66] of char;
  38.  
  39. const PcDos_Attr_ReadOnly    = $01;
  40.       PcDos_Attr_Hidden      = $02;
  41.       PcDos_Attr_System      = $04;
  42.       PcDos_Attr_Label       = $08;
  43.       PcDos_Attr_Directory   = $10;
  44.       PcDos_Attr_Archive     = $20;
  45.  
  46. var PcDos_old_dta   : PcDos_dta_ptr;
  47.     PcDos_ErrCode   : Integer;
  48.     PcDos_ErrClass  : Integer;
  49.     PcDos_ErrAction : Integer;
  50.     PcDos_ErrLocus  : Integer;
  51.  
  52. procedure PcDos_Clr_Dta(var a);
  53.   var i : integer;
  54.       b : PcDos_dta_type absolute a;
  55.   begin
  56.     for i := 1 to 128 do b[i] := 0
  57.   end;
  58.  
  59. function Asciiz_To_String(var a) : LStr;
  60.   var s : LStr;
  61.       i : integer;
  62.       aa : PcDos_Asciiz absolute a;
  63.   begin
  64.     i := 1;
  65.     while aa[i] <> Chr(0) do begin
  66.       s[i] := aa[i];
  67.       i := i + 1
  68.     end;
  69.     s[0] := Chr(i-1);
  70.     Asciiz_To_String := s
  71.   end;
  72.  
  73. procedure String_To_Asciiz(s1 : LStr; var s2 : PcDos_Asciiz);
  74.   var i : integer;
  75.   begin
  76.     for i := 1 to length(s1) do
  77.       s2[i] := s1[i];
  78.     s2[i+1] := Chr(0)
  79.   end;
  80.  
  81. function PcDos_get_dta : PcDos_dta_ptr;
  82.   var r : PcDos_reg_type;
  83.   begin
  84.     with r do begin
  85.       ah := $2F;
  86.       MsDos(r);
  87.       PcDos_get_dta := Ptr(es, bx)
  88.     end
  89.   end;
  90.  
  91. procedure PcDos_set_dta(var a);
  92.   var r   : PcDos_reg_type;
  93.       dta : PcDos_dta_type absolute a;
  94.   begin
  95.     with r do begin
  96.       ah := $1A;
  97.       ds := Seg(dta);
  98.       dx := Ofs(dta);
  99.       MsDos(r)
  100.     end
  101.   end;
  102.  
  103. function PcDos_Error_Meaning(i : integer) : LStr;
  104.   const Num_Errors = 88;
  105.         Error_Meaning : array[0..Num_Errors] of String[50] = (
  106.           '00 - No Error',
  107.           '01 - Invalid Function Number',
  108.           '02 - File Not Found',
  109.           '03 - Path Not Found',
  110.           '04 - Too Many Open Files',
  111.           '05 - Access Denied',
  112.           '06 - Invalid Handle',
  113.           '07 - Memory Control Blocks Destroyed',
  114.           '08 - Insufficient Memory',
  115.           '09 - Invalid Memory Block Address',
  116.           '10 - Invalid Environment',
  117.           '11 - Invalid Format',
  118.           '12 - Invalid Access Code',
  119.           '13 - Invalid Data',
  120.           '14 - Reserved',
  121.           '15 - Invalid Drive',
  122.           '16 - Attempt to Remove Current Directory',
  123.           '17 - Not Same Device',
  124.           '18 - No More Files',
  125.           '19 - Attempt to Write on Write-Protected Diskette',
  126.           '20 - Unknown Unit',
  127.           '21 - Drive Not Ready',
  128.           '22 - Unknown Command',
  129.           '23 - Data Error (CRC)',
  130.           '24 - Bad Request Structure Length',
  131.           '25 - Seek Error',
  132.           '26 - Unknown Media Type',
  133.           '27 - Sector Not Found',
  134.           '28 - Printer Out Of Paper',
  135.           '29 - Write Fault',
  136.           '30 - Read Fault',
  137.           '31 - General Fault',
  138.           '32 - Sharing Violation',
  139.           '33 - Lock Violation',
  140.           '34 - Invalid Disk Change',
  141.           '35 - FCB Unavailable',
  142.           '36 - Sharing Buffer Overflow',
  143.           '37 - Reserved',
  144.           '38 - Reserved',
  145.           '39 - Reserved',
  146.           '40 - Reserved',
  147.           '41 - Reserved',
  148.           '42 - Reserved',
  149.           '43 - Reserved',
  150.           '44 - Reserved',
  151.           '45 - Reserved',
  152.           '46 - Reserved',
  153.           '47 - Reserved',
  154.           '48 - Reserved',
  155.           '49 - Reserved',
  156.           '50 - Network Request Not Supported',
  157.           '51 - Remote Computer Not Listening',
  158.           '52 - Duplicate Name On Network',
  159.           '53 - Network Name Not Found',
  160.           '54 - Network Busy',
  161.           '55 - Network Device No Longer Exists',
  162.           '56 - Net BIOS Command Limit Exceeded',
  163.           '57 - Network Adapter Hardware Error',
  164.           '58 - Incorrect Response From Network',
  165.           '59 - Unexpected Network Error',
  166.           '60 - Incompatible Remote Adapter',
  167.           '61 - Print Queue Full',
  168.           '62 - Not Enough Space For Print File',
  169.           '63 - Print File Was Deleted',
  170.           '64 - Network Name Was Deleted',
  171.           '65 - Access Denied',
  172.           '66 - Network Device Type Incorrect',
  173.           '67 - Network Name Not Found',
  174.           '68 - Network Name Limit Exceeded',
  175.           '69 - Net BIOS Session Limit Exceeded',
  176.           '70 - Temporarily Paused',
  177.           '71 - Network Request Not Accepted',
  178.           '72 - Print Or Disk Redirection is Paused',
  179.           '73 - Reserved',
  180.           '74 - Reserved',
  181.           '75 - Reserved',
  182.           '76 - Reserved',
  183.           '77 - Reserved',
  184.           '78 - Reserved',
  185.           '79 - Reserved',
  186.           '80 - File Exists',
  187.           '81 - Reserved',
  188.           '82 - Cannot Make Directory Entry',
  189.           '83 - Fail on INT 24',
  190.           '84 - Too Many Redirections',
  191.           '85 - Duplicate Redirection',
  192.           '86 - Invalid Password',
  193.           '87 - Invalid Parameter',
  194.           '88 - Network Device Fault');
  195.   begin
  196.     writeln('Error Meaning for code ', i);
  197.     PcDos_Error_Meaning := Error_Meaning[i]
  198.   end;
  199.  
  200. function PcDos_Error_Action(i : integer) : LStr;
  201.   const Num_Actions = 7;
  202.         Error_Action : array [1..Num_Actions] of String[50] = (
  203.           '01 - Retry',
  204.           '02 - Delay Retry',
  205.           '03 - Ask User to Reenter Input',
  206.           '04 - Abort With Cleanup',
  207.           '05 - Immediate Exit',
  208.           '06 - Ignore',
  209.           '07 - Retry After User Intervention');
  210.   begin
  211.     PcDos_Error_Action := Error_Action[i]
  212.   end;
  213.  
  214. function PcDos_Error_Class(i : integer) : LStr;
  215.   const Num_Classes = 13;
  216.         Error_Class : array [1..Num_Classes] of String[50] = (
  217.           '01 - Out Of Resource',
  218.           '02 - Temporary Situation',
  219.           '03 - Authorization',
  220.           '04 - Internal',
  221.           '05 - Hardware Failure',
  222.           '06 - System Failure',
  223.           '07 - Application Program Error',
  224.           '08 - Not Found',
  225.           '09 - Bad Format',
  226.           '10 - Locked',
  227.           '11 - Media',
  228.           '12 - Already Exists',
  229.           '13 - Unknown');
  230.   begin
  231.     PcDos_Error_Class := Error_Class[i]
  232.   end;
  233.  
  234. function PcDos_Error_Locus(i : integer) : LStr;
  235.   const Num_Loci = 5;
  236.         Error_Locus : array [1..Num_Loci] of String[50] = (
  237.           '01 - Unknown',
  238.           '02 - Block Device',
  239.           '03 - Network',
  240.           '04 - Serial Device',
  241.           '05 - Memory');
  242.   begin
  243.     PcDos_Error_Locus := Error_Locus[i]
  244.   end;
  245.  
  246. procedure PcDos_Error;
  247.   var r : PcDos_reg_type;
  248.   begin
  249.     with r do begin
  250.       ah := $59;
  251.       bx := 0;
  252.       MsDos(r);
  253.  
  254.       { AX = extended error code;
  255.         BH = error class;
  256.         BL = recommended action;
  257.         CH = locus }
  258.  
  259.       PcDos_ErrCode   := ax;
  260.       PcDos_ErrClass  := bh;
  261.       PcDos_ErrAction := bl;
  262.       PcDos_ErrLocus  := ch
  263.     end
  264.   end;
  265.  
  266. procedure PcDos_Error_Halt;
  267.   begin
  268.     WriteLn('Program Halted Due to Unrecoverable Error in PcDos Routines');
  269.     WriteLn('Extended Error Code = ', PcDos_Error_Meaning(PcDos_ErrCode));
  270.     WriteLn('Error Class = ', PcDos_Error_Class(PcDos_ErrClass));
  271.     WriteLn('Recommended Action = ', PcDos_Error_Action(PcDos_ErrAction));
  272.     WriteLn('Error Locus = ', PcDos_Error_Locus(PcDos_ErrLocus));
  273.     Halt
  274.   end;
  275.  
  276. function PcDos_Handle(var r : PcDos_reg_type) : integer;
  277.   var r2 : PcDos_reg_type;
  278.       ok : boolean;
  279.       retry_count : integer;
  280.       ret_value   : integer;
  281.   begin
  282.     retry_count := 0;
  283.     ret_value   := 0;
  284.     repeat
  285.       ok := TRUE;
  286.       r2 := r;
  287.       MsDos(r2);
  288.       if (r2.flags and $0001) = $0001 then begin  { carry flag on; ERROR }
  289.         PcDos_Error;
  290.         case PcDos_ErrAction of
  291.           1 : begin             { retry }
  292.                 if retry_count > 10 then PcDos_Error_Halt;
  293.                 retry_count := retry_count + 1;
  294.                 ok := FALSE
  295.               end;
  296.           2 : begin             { delay retry }
  297.                 if retry_count > 10 then PcDos_Error_Halt;
  298.                 delay(500);
  299.                 retry_count := retry_count + 1;
  300.                 ok := FALSE
  301.               end;
  302.           3 : ret_value := PcDos_ErrCode;  { invalid input from user }
  303.           4 : PcDos_Error_Halt;  { abort }
  304.           5 : PcDos_Error_Halt;  { immediate exit }
  305.           6 : ret_value := PcDos_ErrCode;  { ignore }
  306.           7 : ret_value := PcDos_ErrCode;  { retry after user intervention }
  307.         else
  308.           ret_value := PcDos_ErrCode
  309.         end; { case }
  310.       end  { if }
  311.     until ok;
  312.     r := r2;
  313.     PcDos_Handle := ret_value
  314.   end;
  315.  
  316. procedure PcDos_find_first(fn : LStr; attr : integer; var dta : PcDos_Find_Area);
  317.   var r   : PcDos_reg_type;
  318.       e   : integer;
  319.       s   : PcDos_Asciiz;
  320.   begin
  321.     PcDos_Clr_Dta(dta);
  322.     PcDos_old_dta := PcDos_get_dta;
  323.     PcDos_set_dta(dta);
  324.  
  325.     String_To_Asciiz(fn, s);
  326.     with r do begin
  327.       ah := $4E;
  328.       ds := Seg(s);
  329.       dx := Ofs(s);
  330.       cx := attr
  331.     end;
  332.     e := PcDos_Handle(r);
  333.     if not (e in [0, 2, 3, 18]) then PcDos_Error_Halt;
  334.     if e <> 0 then dta.name[1] := Chr(0);
  335.  
  336.     PcDos_set_dta(PcDos_old_dta^)
  337.   end;
  338.  
  339. procedure PcDos_Find_Next(var dta : PcDos_Find_Area);
  340.   var r   : PcDos_reg_type;
  341.       e   : integer;
  342.   begin
  343.     PcDos_old_dta := PcDos_get_dta;
  344.     PcDos_set_dta(dta);
  345.  
  346.     r.ah := $4F;
  347.     e := PcDos_Handle(r);
  348.     if not (e in [0, 2, 3, 18]) then PcDos_Error_Halt;
  349.     if e <> 0 then dta.name[1] := Chr(0);
  350.  
  351.     PcDos_set_dta(PcDos_old_dta^)
  352.   end;
  353.  
  354. procedure PcDos_ChMod(fn : LStr; attr : integer);
  355.   var r : PcDos_reg_type;
  356.       e : integer;
  357.       f : PcDos_Asciiz;
  358.   begin
  359.     String_To_Asciiz(fn, f);
  360.     with r do begin
  361.       ah := $43;
  362.       ds := seg(f);
  363.       dx := ofs(f);
  364.       cx := attr;
  365.       al := $01
  366.     end;
  367.     e := PcDos_Handle(r);
  368.     if e <> 0 then PcDos_Error_Halt
  369.   end;
  370.  
  371. procedure PcDos_Delete_File(fn : LStr);
  372.   var r : PcDos_reg_type;
  373.       e : integer;
  374.       f : PcDos_Asciiz;
  375.   begin
  376.     String_To_Asciiz(fn, f);
  377.     with r do begin
  378.       ah := $41;
  379.       ds := seg(f);
  380.       dx := ofs(f)
  381.     end;
  382.     e := PcDos_Handle(r);
  383.     if e <> 0 then PcDos_Error_Halt
  384.   end;
  385.  
  386. function PcDos_Mem_Avail : integer;
  387. { returns size (in paragraphs) of the largest memory block }
  388.   var r : PcDos_reg_type;
  389.   begin
  390.     r.ah := $48;
  391.     r.bx := $FFFF;  { ask for 64K paragraphs -- will be too big }
  392.     MsDos(r);
  393.     if r.ax <> 8 then begin
  394.       PcDos_Error;
  395.       PcDos_Error_Halt
  396.     end;
  397.     PcDos_Mem_Avail := r.bx
  398.   end;
  399.  
  400. procedure PcDos_Get_Date(var day_of_week, year, month, day : integer);
  401.   var r : PcDos_reg_type;
  402.   begin
  403.     r.ah := $2A;
  404.     MsDos(r);
  405.     day_of_week := r.al;
  406.     year        := r.cx;
  407.     month       := r.dh;
  408.     day         := r.dl
  409.   end;
  410.  
  411. procedure PcDos_Get_Time(var hour, minute, second, hundredths : integer);
  412.   var r : PcDos_reg_type;
  413.   begin
  414.     r.ah := $2C;
  415.     MsDos(r);
  416.     hour       := r.ch;
  417.     minute     := r.cl;
  418.     second     := r.dh;
  419.     hundredths := r.dl
  420.   end;
  421.  
  422. {====================}
  423. {  END OF PCDOS.INC  }
  424. {====================}
  425.  
  426. function dir_del(d, f : LStr) : integer;
  427. { delete file in given directory (and all its subdirectories);
  428.   return number of files deleted }
  429.  
  430.   var dta : PcDos_Find_Area;
  431.       n   : integer;
  432.       s   : LStr;
  433.  
  434.   function do_delete(s : LStr) : integer;
  435.     var n : integer;
  436.         f : LStr;
  437.         c : char;
  438.         fullname : LStr;
  439.     begin { do_delete }
  440.       n := 0;
  441.       PcDos_Find_First(s, PcDos_Attr_Hidden+PcDos_Attr_System, dta);
  442.       f := Asciiz_To_String(dta.name);
  443.       while f <> '' do begin
  444.         fullname := d + '\' + f;
  445.         if verify then begin
  446.           write('Delete ', fullname, '? ');
  447.           readln(c);
  448.           if c in ['Y', 'y'] then begin
  449.             if (dta.attr and PcDos_Attr_ReadOnly) <> 0 then begin
  450.               write('File is Read Only; Are You SURE? ');
  451.               readln(c);
  452.               if c in ['Y', 'y'] then begin
  453.                 PcDos_ChMod(fullname, $00);
  454.                 PcDos_Delete_File(fullname);
  455.                 n := n + 1;
  456.                 writeln(fullname, ' deleted')
  457.               end
  458.             end else begin
  459.               PcDos_Delete_File(fullname);
  460.               n := n + 1;
  461.               writeln(fullname, ' deleted')
  462.             end
  463.           end
  464.         end else begin
  465.           if (dta.attr and PcDos_Attr_ReadOnly) <> 0 then
  466.             PcDos_ChMod(fullname, $00);
  467.           PcDos_Delete_File(fullname);
  468.           n := n + 1;
  469.           writeln(fullname, ' deleted')
  470.         end;
  471.         PcDos_Find_Next(dta);
  472.         f := Asciiz_To_String(dta.name)
  473.       end;
  474.       do_delete := n
  475.     end; { do_delete }
  476.  
  477.   begin { dir_del }
  478.     s := d + '\' + f;
  479.     writeln('Searching ', d, '...');
  480.     n := do_delete(s);  { delete file(s) from current directory }
  481.     PcDos_Find_First(d+'\*.*', PcDos_Attr_Directory, dta);
  482.     s := Asciiz_To_String(dta.name);
  483.     while s <> '' do begin
  484.       if (s <> '.') and (s <> '..') and ((dta.attr and PcDos_Attr_Directory) <> 0) then
  485.         n := n + dir_del(d + '\' + s, f);
  486.       PcDos_Find_Next(dta);
  487.       s := Asciiz_To_String(dta.name)
  488.     end;
  489.     dir_del := n
  490.   end;
  491.  
  492. procedure global_del(s : LStr);
  493. { search all directories and delete file from them }
  494.   var drive : String[2];
  495.       fn    : String[12];
  496.       n     : integer;
  497.   begin
  498.     if s[2] = ':' then begin
  499.       drive := copy(s, 1, 2);
  500.       fn    := copy(s, 3, 12)
  501.     end else begin
  502.       drive := '';
  503.       fn    := s
  504.     end;
  505.     n := dir_del(drive, fn);
  506.  
  507.     if n = 0 then writeln('No files deleted')
  508.     else if n = 1 then writeln('1 file deleted')
  509.     else writeln(n, ' files deleted');
  510.  
  511.     writeln
  512.   end;
  513.  
  514. begin
  515.   writeln('Global Delete Files');
  516.   verify := TRUE;
  517.  
  518.   if ParamCount <> 0 then begin
  519.     command_line := TRUE;
  520.     for Param_Num := 1 to ParamCount do
  521.       if (ParamStr(Param_Num) = '/n') or (ParamStr(Param_Num) = '/N') then
  522.         verify := FALSE
  523.       else
  524.         global_del(ParamStr(Param_Num))
  525.   end else begin
  526.     command_line := FALSE;
  527.     while TRUE do begin
  528.       write('Name of file to delete: ');
  529.       readln(in_str);
  530.       if in_str = '' then halt;
  531.       global_del(in_str)
  532.     end
  533.   end
  534. end.
  535.