Cambiar los atributos a un fichero

2199 vistas

Para cambiar los atributos a un fichero, tendremos que usar la función del API SetFileAttributes definida en la unit Windows.

Veamos un ejemplo:

Queremos quitarle el atributo de sólo lectura a un fichero para poder trabajar con él y, posteriormente, volverselo a activar



delphi
  1. var
  2.   Atributos: Cardinal;
  3. begin
  4.   // capturamos atributos actuales
  5.   Atributos := GetFileAttributes(PChar('C:\MiArchivo.txt'));
  6.   // quitamos el atributo de sólo lectura
  7.   SetFileAttributes(PChar('C:\MiArchivo.txt'), Atributos and not FILE_ATTRIBUTE_READONLY);
  8.  
  9.   // realizamos proceso con el archivo
  10.  
  11.   // establecemos el atributo de sólo lectura
  12.   SetFileAttributes(PChar('C:\MiArchivo.txt'), Atributos and FILE_ATTRIBUTE_READONLY);
  13. end;