how to reset file attributes with FileCopy()
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.
- You could call
ShellExecute
using theattrib -r C:\path\FileName.txt
- You could build the functionality into a DLL and import the DLL
- You could use a COM object to do this
- 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;
精彩评论