开发者

how to reset file attributes with FileCopy()

开发者 https://www.devze.com 2023-03-08 11:20 出处:网络
I useFilecopy() function to copy filesfrom CDROM media, it seems that READONLY attribute is copied and it causes problem with reinstall.

I use Filecopy() function to copy files from CDROM media, it seems that READONLY attribute is copied and it causes problem with reinstall.

how to remove read o开发者_运维百科nly attribute?


The built in support functions don't have a routine to do this.

So it comes down to one of several other options.

  1. You could call ShellExecute using the attrib -r C:\path\FileName.txt
  2. You could build the functionality into a DLL and import the DLL
  3. You could use a COM object to do this
  4. Call the Windows API directly. UPDATED

Here is an example function that does it with COM

procedure RemoveReadOnly(FileSpec : String);
var
 FSO : Variant;
 File : Variant;
begin
 FSO := CreateOleObject('Scripting.FileSystemObject');
 File := FSO.GetFile(FileSpec);
 if File.attributes and 1 then // Check if Readonly already
    File.attributes := File.attributes - 1;
end;

MSDN Contains the documentation on FilesSystemObject and the use of Attributes.

UPDATED

Here is an example of calling the Windows API directly, which is best option.

function GetFileAttributes(lpFileName: PAnsiChar): DWORD;
 external 'GetFileAttributesA@kernel32.dll stdcall';

function SetFileAttributes(lpFileName: PAnsiChar; 
   dwFileAttributes: DWORD): BOOL; 
external 'SetFileAttributesA@kernel32.dll stdcall';

procedure RemoveReadOnly(FileName : String);
var
 Attr : DWord;
begin
  Attr := GetFileAttributes(FileName);
  if (Attr and 1) = 1  then          
  begin
    Attr := Attr -1;
    SetFileAttributes(FileName,Attr);
  end;
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号