Delphi-Help

Главная

Как изменить атрибуты файла?

Оцените материал
(1 Голосовать)


Как изменить атрибуты файла?

r - ReadOnly

h - Hidden

s - SysFile

a - Archive

Способ первый

procedure SetAttribut( Path: string; r, h, s, a: char );
var
  At: integer;
begin
   At := FileGetAttr( Path );
   case r of
      '-': if ( At and faReadOnly ) = faReadOnly then
              FileSetAttr( Path, At - faReadOnly );
      '+': if ( At and faReadOnly ) = 0 then
              FileSetAttr( Path, At + faReadOnly );
   end;
   At := FileGetAttr( Path );
   case h of
      '-': if ( At and faHidden ) = faHidden then
              FileSetAttr( Path, At - faHidden );
      '+': if ( At and faHidden ) = 0 then
              FileSetAttr( Path, At + faHidden );
   end;
   At := FileGetAttr( Path );
   case s of
      '-': if ( At and faSysFile ) = faSysFile then
              FileSetAttr( Path, At - faSysFile );
      '+': if ( At and faSysFile ) = 0 then
              FileSetAttr( Path, At + faSysFile );
   end;
   At := FileGetAttr( Path );
   case a of
      '-': if ( At and faArchive ) = faArchive then
              FileSetAttr( Path, At - faArchive );
      '+': if ( At and faArchive ) = 0 then
              FileSetAttr( Path, At + faArchive );
   end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
   SetAttribut( 'c:\test.txt', '+', '-', ' ', ' ' );
end;

Способ второй

procedure SetAttribut( Path: string; r, h, s, a: char );
var
  At: integer;
begin
   At := FileGetAttr( Path );
   if r = '+' then At := At or faReadOnly;
   if r = '-' then At := At and not faReadOnly;
   if h = '+' then At := At or faHidden;
   if h = '-' then At := At and not faHidden;
   if s = '+' then At := At or faSysFile;
   if s = '-' then At := At and not faSysFile;
   if a = '+' then At := At or faArchive;
   if a = '-' then At := At and not faArchive;
   FileSetAttr( Path, At );
end;
 
procedure TForm1.Button1Click(Sender: TObject);
begin
   SetAttribut( 'c:\test.txt', '+', '-', ' ', ' ' );
end;

'+' - установить атрибут

'-' - снять атрибут

' ' - оставить без изменения.

Прочитано 9894 раз

Авторизация



Счетчики