home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / FILESET / FILESET.PAS
Pascal/Delphi Source File  |  1988-09-02  |  6KB  |  229 lines

  1. {$I-,R-,S-}
  2.  
  3. {
  4.  Sets date, time and attribute of files
  5.  Call with no parameters to see syntax
  6.  
  7.  Written by Kim Kokkonen, TurboPower Software
  8.  1/88
  9.  Released to the public domain
  10. }
  11.  
  12. program FileSet;
  13.  
  14. uses
  15.   dos,
  16.   tpstring;
  17.  
  18. const
  19.   NotUsed = MaxInt;
  20. var
  21.   FileMask : string;
  22.   SetAttr : Integer;
  23.   SetDateTime : DateTime;
  24.   Frec : SearchRec;
  25.   FilesSet : Integer;
  26.  
  27.   procedure FatalError(Msg : string);
  28.     {-Report error and halt}
  29.  
  30.   begin                      {FatalError}
  31.     WriteLn;
  32.     WriteLn(Msg);
  33.     Halt(1);
  34.   end;                       {FatalError}
  35.  
  36.   function ParseDateTime(S : string; var Drec) : Boolean;
  37.     {-Convert a string of form nn:nn:nn to part of a DateTime record}
  38.   type
  39.     ParseType = 1..3;
  40.     ParseArray = array[ParseType] of Integer;
  41.   var
  42.     P : ParseArray absolute Drec;
  43.     I : Integer;
  44.     Pt : ParseType;
  45.     Temp : string;
  46.     TempInt : Integer;
  47.  
  48.   begin                      {ParseDateTime}
  49.     ParseDateTime := False;
  50.     Pt := 1;
  51.     Temp := '';
  52.     {Terminate string}
  53.     S := S+'/';
  54.     for I := 1 to Length(S) do
  55.       case S[I] of
  56.         '0'..'9' : Temp := Temp+S[I];
  57.       else
  58.         {Accept any other character as a delimiter}
  59.         if not Str2Int(Temp, TempInt) then
  60.           Exit;
  61.         P[Pt] := TempInt;
  62.         if Pt = 3 then begin
  63.           {Record filled}
  64.           ParseDateTime := True;
  65.           Exit;
  66.         end else
  67.           Inc(Pt);
  68.         Temp := '';
  69.       end;
  70.  
  71.     ParseDateTime := True;
  72.   end;                       {ParseDateTime}
  73.  
  74.   procedure Rearrange(var DT: DateTime);
  75.     {-Put Y:M:D in correct order}
  76.   var
  77.     T:integer;
  78.  
  79.   begin     {Rearrange}
  80.     with DT do begin
  81.       T:=year;
  82.       year:=day;
  83.       day:=month;
  84.       month:=T;
  85.       if year < 1980 then
  86.         inc(year,1900);
  87.     end;
  88.   end;      {Rearrange}
  89.  
  90.   procedure GetParameters;
  91.     {-Analyze the command line parameters}
  92.   var
  93.     I : Word;
  94.     Arg : string;
  95.     GotOne : Boolean;
  96.  
  97.   begin                      {GetParameters}
  98.  
  99.     GotOne := False;
  100.     FileMask := '';
  101.     SetAttr := NotUsed;
  102.     FillChar(SetDateTime, SizeOf(DateTime), 0);
  103.     with SetDateTime do begin
  104.       year := NotUsed;
  105.       hour := NotUsed;
  106.     end;
  107.  
  108.     I := 1;
  109.     while I <= ParamCount do begin
  110.       Arg := ParamStr(I);
  111.       if (Length(Arg) = 2) and ((Arg[1] = '-') or (Arg[1] = '/')) then
  112.         case Upcase(Arg[2]) of
  113.           'A' : if I < ParamCount then begin
  114.                   Inc(I);
  115.                   if not Str2Int(ParamStr(I), SetAttr) then
  116.                     FatalError('Invalid attribute specified');
  117.                   GotOne := True;
  118.                 end else
  119.                   FatalError('Attribute missing after A');
  120.  
  121.           'D' : if I < ParamCount then begin
  122.                   Inc(I);
  123.                   if not ParseDateTime(ParamStr(I), SetDateTime.year) then
  124.                     FatalError('Invalid date specified');
  125.                   Rearrange(SetDateTime);
  126.                   GotOne := True;
  127.                 end else
  128.                   FatalError('Date missing after D');
  129.  
  130.           'T' : if I < ParamCount then begin
  131.                   Inc(I);
  132.                   if not ParseDateTime(ParamStr(I), SetDateTime.hour) then
  133.                     FatalError('Invalid time specified');
  134.                   GotOne := True;
  135.                 end else
  136.                   FatalError('Time missing after T');
  137.         else
  138.           FatalError('Unrecognized command line option '+Arg);
  139.         end
  140.       else if FileMask = '' then
  141.         FileMask := Arg
  142.       else
  143.         FatalError('More than one file mask specified');
  144.       Inc(I);
  145.     end;
  146.  
  147.     if FileMask = '' then
  148.       FatalError('No files specified');
  149.  
  150.     if not(GotOne) then
  151.       {Use as a touch utility}
  152.       with SetDateTime do begin
  153.         getdate(year, month, day, I);
  154.         gettime(hour, min, sec, I);
  155.       end;
  156.  
  157.   end;                       {GetParameters}
  158.  
  159.   procedure SetAttributes(Name : string);
  160.     {-Set the time/date/protection attributes for the named file}
  161.   var
  162.     F : file;
  163.     GetDateTime : DateTime;
  164.     GetAttr : Word;
  165.     T : LongInt;
  166.  
  167.   begin                      {SetAttributes}
  168.  
  169.     Assign(F, Name);
  170.  
  171.     GetFattr(F, GetAttr);
  172.     {Make file normal for a moment}
  173.     SetFattr(F, 0);
  174.  
  175.     with SetDateTime do
  176.       if (year <> NotUsed) or (hour <> NotUsed) then begin
  177.         Reset(F, 1);
  178.         if IoResult = 0 then begin
  179.           {At least one of the two was changed}
  180.           if (year = NotUsed) or (hour = NotUsed) then begin
  181.             {At least one of the two was not changed, get the current}
  182.             GetFTime(F, T);
  183.             UnpackTime(T, GetDateTime);
  184.             {Transfer in new values}
  185.             if year <> NotUsed then
  186.               Move(SetDateTime.year, GetDateTime.year, 6);
  187.             if hour <> NotUsed then
  188.               Move(SetDateTime.hour, GetDateTime.hour, 6);
  189.           end else
  190.             {Both were changed}
  191.             GetDateTime := SetDateTime;
  192.           {Pack it up}
  193.           PackTime(GetDateTime, T);
  194.           {Change the file}
  195.           SetFtime(F, T);
  196.           Close(F);
  197.         end;
  198.       end;
  199.  
  200.     if SetAttr <> NotUsed then
  201.       GetAttr := SetAttr;
  202.     SetFattr(F, GetAttr);
  203.  
  204.   end;                       {SetAttributes}
  205.  
  206. begin
  207.   if ParamCount = 0 then begin
  208.     WriteLn('Usage: FILESET FileNameMask -a Attr -d Date -t Time');
  209.     FatalError('Example: FILESET *.PAS -a 0 -d 10-1-88 -t 4:02');
  210.   end;
  211.  
  212.   GetParameters;
  213.  
  214.   {Scan all normal files in the current directory}
  215.   FindFirst(FileMask, AnyFile, Frec);
  216.  
  217.   if DosError <> 0 then
  218.     FatalError('No matching files found');
  219.  
  220.   FilesSet := 0;
  221.   repeat
  222.     Inc(FilesSet);
  223.     SetAttributes(addbackslash(justpathname(FileMask))+Frec.Name);
  224.     FindNext(Frec);
  225.   until DosError <> 0;
  226.  
  227.   WriteLn('Files Set: ', FilesSet);
  228. end.
  229.