FileSetAttr not working?

There are times that I unzip files that people send me and for some reason (in some cases) the file attributes of almost all the files are set to system, read-only, hidden and archive. I had previously written a program to help me manage those files and I added a section of code that attempts to reset those attributes using FileSetAttr but the problem that I'm having is that I'm passing in the correct parameters and FileSetAttr is returning zero (successful) and yet windows explorer shows that the file at
tributes remain unchanged after I've cleared them.

I even tried running the executable at administrator in case there was a permissions problem and that didn't make any difference; likewise I checked the folder permissions for my working directory and the users account has full access to it so I don't believe that the problem is some kind of permissions issue.

Can anyone shed some light on this issue? What I don't understand is that FileSetAttr is returning zero (successful) when in fact it's failing; if I was getting some kind of error then I'd have a clue about what was wrong.
0
Chris
7/31/2015 2:01:22 AM
embarcadero.delphi.win32 2183 articles. 0 followers. Follow

3 Replies
476 Views

Similar Articles

[PageSpeed] 59

Chris wrote:

> I added a section of code that attempts to reset those attributes
> using FileSetAttr but the problem that I'm having is that I'm passing
> in the correct parameters and FileSetAttr is returning zero (successful)
> and yet windows explorer shows that the file attributes remain unchanged
> after I've cleared them.

Please show your actual code.

> Can anyone shed some light on this issue? What I don't understand
> is that FileSetAttr is returning zero (successful) when in fact it's
> failing;

Not according to Windows, it is not.

-- 
Remy Lebeau (TeamB)
0
Remy
7/31/2015 2:36:27 AM
Hi Remy:

The code I use for this is this:

{code}Function ClearArchiveAttrib(const Filename: string): Integer;

Var
  Attributes: Word;
  Count: Word;
  Attrs: AttrType;

Begin
  Attributes := FileGetAttr(Filename);
  GetAttrs( Attrs, Attributes );

  If Attrs.ReadOnly Then Attributes := (Attributes and not SysUtils.faReadOnly);
  If Attrs.Hidden Then Attributes := (Attributes and not SysUtils.faHidden);
  If Attrs.SysFile Then Attributes := (Attributes and not SysUtils.faSysFile);

  Result := FileSetAttr(Filename, Attributes );
  If Result <> 0 Then
    MessageDlg( 'Reset of attributes on '+Filename+' failed, error code '+inttoStr(Result), mtError, [mbOk], 0 );

End;{code}

When I detect a file that has any or all of the attributes set I pass the fully qualified filename into this function; each attribute is removed from the Attributes variable and then the FileSetAttr function is called to set the new attribute on the file.

I've traced through this routine a dozen times and I can see that a properly formatted file name is passed in; FileGetAttr retrieves the correct attribute i.e. I can see based on the value of "Attributes" that it's ReadOnly, Hidden and a sysfile and faInvalid is not returned so no error; I remove those unwanted attributes from the Attributes variable and then use FileSetAttr to reset the files attributes and Result always comes back as zero / successful and yet when I look at the files in question in Wind
ows Explorer after the fact the attributes are still "rhsa" when they should be only "a" as the other three attributes should have been removed.

Btw the variable Attrs is record of boolean values and GetAttrs is a simple procedure I wrote ages ago to break out file attributes into a more easily identifable values:

{code}Type
    AttrType = Record
                 ReadOnly,
                 Hidden,
                 SysFile,
                 VolumeID,
                 Directory,
                 Archive: Boolean;
                End;

Procedure GetAttrs( Var Item: AttrType; Attr: Integer );

Begin
  If (Attr And SysUtils.faReadOnly) = SysUtils.faReadOnly Then Item.ReadOnly := True Else Item.ReadOnly := False;
  If (Attr And SysUtils.faHidden) = SysUtils.faHidden Then Item.Hidden := True Else Item.Hidden := False;
  If (Attr And SysUtils.faSysFile) = SysUtils.faSysFile Then Item.SysFile := True Else Item.SysFile := False;
  If (Attr And SysUtils.faVolumeID) = SysUtils.faVolumeID Then Item.VolumeID := True Else Item.VolumeID := False;
  If (Attr And SysUtils.faDirectory) = SysUtils.faDirectory Then Item.Directory := True Else Item.Directory := False;
  If (Attr And SysUtils.faArchive) = SysUtils.faArchive Then Item.Archive := True Else Item.Archive := False;

End;
{code}
0
Chris
7/31/2015 4:04:54 AM
Chris wrote:

> Attributes: Word;

FileGetAttr() returns a 32bit Integer, so why are you using a 16bit Word? 
 There are several attribute values that are outside the range of a Word 
(FILE_ATTRIBUTE_NO_SCRUB_DATA, FILE_ATTRIBUTE_VIRTUAL, etc).

> If Attrs.ReadOnly Then Attributes := (Attributes and not SysUtils.faReadOnly);
> If Attrs.Hidden Then Attributes := (Attributes and not SysUtils.faHidden);
> If Attrs.SysFile Then Attributes := (Attributes and not SysUtils.faSysFile);

If you are just going to strip off the values anyway, why bother checking 
if they are enabled beforehand?

{code}
Function ClearArchiveAttrib(const Filename: string): Integer;
Var
  Attributes: Integer;
Begin
  Attributes := FileGetAttr(Filename);
  if Attributes = faInvalid then begin
    Result := GetLastError;
  end else
  begin
    Attributes := (Attributes and not (SysUtils.faReadOnly or SysUtils.faHidden 
or SysUtils.faSysFile);
    Result := FileSetAttr(Filename, Attributes);
    ...
  end;
End;
{code}

Alternatively:

{code}
Function ClearArchiveAttrib(const Filename: string): Integer;
Var
  OldAttributes, NewAttributes: Integer;
Begin
  OldAttributes := FileGetAttr(Filename);
  if OldAttributes = faInvalid then begin
    Result := GetLastError;
  end else
  begin
    NewAttributes := (OldAttributes and not (SysUtils.faReadOnly or SysUtils.faHidden 
or SysUtils.faSysFile);
    if NewAttributes = OldAttributes then begin
      Result := 0;
    end else
    begin
      Result := FileSetAttr(Filename, Attributes);
      ...
    end;
  end;
End;
{code}

> I've traced through this routine a dozen times and I can see that a
> properly formatted file name is passed in; FileGetAttr retrieves the
> correct attribute i.e. I can see based on the value of "Attributes"
> that it's ReadOnly, Hidden and a sysfile and faInvalid is not returned
> so no error; I remove those unwanted attributes from the Attributes
> variable and then use FileSetAttr to reset the files attributes and
> Result always comes back as zero / successful and yet when I look
> at the files in question in Windows Explorer after the fact the attributes
> are still "rhsa" when they should be only "a" as the other three
> attributes should have been removed.

Did you trace into FileSetAttr() itself to make sure it is actually calling 
the Win32 API SetFileAttributes() function?

Did you try using SysInternals Process Monitor or similar tool to make sure 
the attributes are actually being applied to the intended file on the hard 
drive, and that nothing is coming in after the fact and resetting the attributes 
before you view them in Explorer?

Are you ABSOLUTELY SURE you are updating the same file that you are looking 
at in Explorer?  Is your app 32bit or 64bit?  If 32bit, are you taking virtualization 
into account?

-- 
Remy Lebeau (TeamB)
0
Remy
7/31/2015 4:12:57 PM
Reply:

Similar Artilces:

PHP -> JSON ->Zlib -> Delphi XE7 [win32 -> work | android -> No mapping ] [Edit]
Мy program for android should get data from the server as yson when to perform under vindovs works, and in android error, please help! PHP: {code} $err = array(); $data = array('id'=>1,'name'=>'test','info'=>'1234'); $inf = array('data' => $data ,'error' => $err); $json = json_encode($inf); $json = gzencode(trim(preg_replace('/\s+/',' ',$json )),9); header('Content-Encoding: gzip'); header("content-type: text/html; charset: UTF-8"); header("cache-control: must-revalidate&qu...

PHP -> JSON ->Zlib -> Delphi XE7 [win32 -> work | android -> No mapping ] [Edit] #2
Мy program for android should get data from the server as yson when to perform under vindovs works, and in android error, please help! PHP: {code} $err = array(); $data = array('id'=>1,'name'=>'test','info'=>'1234'); $inf = array('data' => $data ,'error' => $err); $json = json_encode($inf); $json = gzencode(trim(preg_replace('/\s+/',' ',$json )),9); header('Content-Encoding: gzip'); header("content-type: text/html; charset: UTF-8"); header("cache-control: must-revalidate&qu...

PHP -> JSON ->Zlib -> Delphi XE7 [win32 -> work | android -> No mapping ] [Edit] #2
Мy program for android should get data from the server as json when to perform under Win32 works, and in android error, please help! PHP: {code} $err = array(); $data = array('id'=>1,'name'=>'test','info'=>'1234'); $inf = array('data' => $data ,'error' => $err); $json = json_encode($inf); $json = gzencode(trim(preg_replace('/\s+/',' ',$json )),9); header('Content-Encoding: gzip'); header("content-type: text/html; charset: UTF-8"); header("cache-control: must-revalidate"...

PHP -> JSON ->Zlib -> Delphi XE7 [win32 -> work | android -> No mapping ] [Edit] #2
Мy program for android should get data from the server as yson when to perform under vindovs works, and in android error, please help! PHP: {code} $err = array(); $data = array('id'=>1,'name'=>'test','info'=>'1234'); $inf = array('data' => $data ,'error' => $err); $json = json_encode($inf); $json = gzencode(trim(preg_replace('/\s+/',' ',$json )),9); header('Content-Encoding: gzip'); header("content-type: text/html; charset: UTF-8"); header("cache-control: must-revalidate&qu...

Why does this work fine in Delphi 2009, but not in Delphi 2010
I've been racking my head trying to figure this out and can't understand what is wrong, TFileStream.ReadComponentRes fails when I try to read a component containing a record, it works fine in Delphi 2009 and Delphi 2007, but fails with Delphi 2010 Here is the unit source and DFM *+//UnitSource+* unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TThresholdMode = (tm0,tm1,tm2,tm3,tm4,tm5); TThreshold = Record Mode:TThresholdMode; Filter,Start,Stop:Integer; end; ...

Converting Delphi for Win32 to Delphi .Net(Prism)
Hi, I am currently migrating a project from Delphi for Win32 to Delphi.net. Part of my code currently goes into a directory and pulls out a random file from this directory and loads the contents of the file for me. This code doesn't seem to work in Delphi.Net. It uses PString and a number of functions in SysUtils that don't seem to be present in Delphi.net's SysUtils file. If anyone can help me please, it would be greatly appreciated! Many thanks, Jonathan Mackey Jonathan Mackey a écrit : > I am currently migrating a project from Delphi for Win32 to &...

Modify a working Delphi XE4 installation does not work
Hello, i need to upgrade the intraweb component in Delphi XE4 to the official atozed license. The info from atozed is first to uninstall the bundled version. But when i start the installation wizard from XE4 and select modify, a message "serial number unvalid" appears and i can only stop with taskmanager. What can i do. Thanks Theo Ok, i found the solution myself. Here is how it goes: http://support.embarcadero.com/article/43089 ...

Migrate from Delphi 2007 for Win32 to Delphi XE
we use Delphi 2007 for Win32 to support legacy (32Bit) OWL-based pascal applications (yes i know it was a mistake not to switch to VCL 15 years ago). could our applications still be opened and compiled with Delphi XE? The existing projects are all plain Pascal-Code, coming back from the times of Turbo Pascal for Windows and later on Borland Pascal. Are there any improvements we could profit from (i.e IDE, Debugger)? Thanks Andrej > {quote:title=Andrej Dimic wrote:}{quote} > could our applications still be opened and compiled with Delphi XE? I'm not sure, but I guess ...

Delphi.NET loading Delphi.Win32 Driver
Hi, What I'm trying to do is marshal an array of cardinal (or integer) back into managed memory from a win32 dll. I know how to pass managed memory into a win32 dll {code} var aa : array of Integer; Buffer : IntPtr; begin SetLength(aa,2); aa[0] := 1; aa[1] := 80; if not Supports(ExtractFilePath(Application.ExeName)+'Win32_Library\SDK_Driver.Win32.io', TypeOf(IMyFunctions), MyFunctions) then Exit; //loads the driver into memory. MyFunctions contains the method names found in the SDK_Driver. Buffer := Marshal.AllocHGlobal(2 * {Marshal.SystemDefaultC...

delphi Win32 using delphi .NET dll
Hi, I'm trying to use a delphi.NET dll in delphi.WIN32. I am currently using CodeGear Delphi 2007 with version2(base version) of .NET I can get the dll to import into the WIN32 application the only problem is when i include things such as: "using Classes,DateUtils, SysUtils" in the .NET dll the win32 application will instantly hang when any of the dll functions are called. Any help would be great thanks. Also I have tried this example and it also crashes for me? http://cc.embarcadero.com/Item/22688 -Braden I also found this.. "The problem is that, wehn you instal...

Unit working on delphi 7
Hello, i have unit working on delphi7 - 2010 BUT on XE5 give me a lot of errors can you help me please to check whats wrong in the unit on XE5 {$WARNINGS OFF} unit CommonMemoryUnit; interface uses Windows, Messages, SysUtils, Classes, Dialogs; type TCommMemNT = class(TObject) private FMemPtr:pointer; FhProcess:THandle; FdwProcessId:DWORD; FSize:integer; FPosition : Cardinal; protected procedure Open(AllocSize:integer); procedure Close; public constructor Create(hTarget:HWND;AllocSize:integer); destructor Destroy;override; procedure Ze...

Is dll developed in Delphi 6 works on Delphi 2?
I have a one dll, whose work is to creates a form with some normal vcl controls, print selected tables and email some reports. It was developed in Delphi 6. Can any other application which was developed in Delphi 2 use that dll.? If not, please let me know in which areas i need change. The dll work is only to print and email. With regards, Srikanth Varma Srikanth varma wrote: > I have a one dll, whose work is to creates a form with some normal > vcl controls, print selected tables and email some reports. It was > developed in Delphi 6. Can any other application which was d...

Code works in Delphi 7 but not in Delphi 2010 [Edit]
hello, i have a procedure that open's a file by passing the file name as the parameter to the executable. something like this {code} C : \ P r o g r a m F i l e s \ Da c k e r \ D r a c k e r . e x e " G : \ D E l p h i 7 \ D e l p h i 7 A p p _ l o g . t " {code} The source code is {code} procedure OpenFileWithExe var hReg: HKEY; Ret: Longint; RegDataType, RegDataSize: DWORD; CmdLine: array [0..560] of Char; Len: Integer; SInfo: TStartupInfo; PInfo: TProcessInformation; begin Ret := windows.RegOpenKeyEx(HKEY_CURRENT_USER, ...

Win32 program: Delphi 7 vs Delphi XE5
How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability and performance? Is Delphi XE5 good enough for a big ERP project with several DLL's and hundreds of units and forms? Thanks in advance Am 26.12.2013 15:02, schrieb lior ilan: > How is a D7 Win32 program compared to a Delphi XE5 one in terms of stability > and performance? > Is Delphi XE5 good enough for a big ERP project with several DLL's and > hundreds of units and forms? > Thanks in advance > Hello, XE5 has increased functionality. Stability seems to be ok for most ...

dll fails when compiled with delphi 2010, but works with delphi 7
I may be doing good if someone can just point me to the right forum I have not created a dll for some time and have not written one with 2010 before. The dll I am creating will be called as a cfx function by either a ColdFusion or BlueDragon webserver. I have written cfx DLLs in the past. The function declaration is below, it is the standard declaration from ColdFusion or Bluegradon documention on creating a CFX. procedure ProcessTagRequest(Request: TCFXRequest); export; cdecl; Since this was the first time using 2010 I wrote a very basic function to build off that was not much mor...

Web resources about - FileSetAttr not working? - embarcadero.delphi.win32

Resources last updated: 11/28/2015 9:54:54 AM