Recent

Author Topic: [Solved] - Freeing an assigned but not opened file  (Read 2801 times)

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Freeing an assigned but not opened file
« Reply #15 on: October 17, 2023, 07:21:20 pm »
Help!  Lol, I have no idea how the file is getting locked, or why even.  Is my unhiding not working, I'm not entirely sure how to set the attribute to remove the faHidden to be honest.
SO how do you write your inifile data ? using TIniFile class ? If so then where in your program do your create it and where do you free it ? At what point do you try to change the attribute ?

If things fail then try to create a small testproject (not your current project) that does exactly the minimal thing to re-create the issue and that is compileable. Zip your project source-code files and attach it to a new post with a small description of the issue so that others can test it or at least look at the code. That will provide you with the quickest and most accurate answers, otherwise it is just a crystal ball (and they are scarce and very expensive :) )
Today is tomorrow's yesterday.

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Freeing an assigned but not opened file
« Reply #16 on: October 17, 2023, 08:02:27 pm »
Oh. Oops  :-[

from the manual:
Quote
FileSetAttr sets the attributes of FileName to Attr. If the function was successful, 0 is returned, -1 otherwise. Attr can be set to an OR-ed combination of the predefined faXXX constants.

This function is not implemented on Unixes.
I completely missed that  :-[

I have no idea how I can make up for such a dumb mistake otehr than to say I'm terribly sorry for the inconvenience zxandris.
Today is tomorrow's yesterday.

cdbc

  • Hero Member
  • *****
  • Posts: 2527
    • http://www.cdbc.dk
Re: Freeing an assigned but not opened file
« Reply #17 on: October 17, 2023, 08:10:44 pm »
Hi
Well, if you are on *nix/*nux, why bother, just give your ini-file a name, that starts with a '.' e.g.: /home/zx/tmp/.hiddenfile.ini
edit: No attrib-fiddling necessary  ;)
Regards Benny
« Last Edit: October 17, 2023, 08:14:33 pm by cdbc »
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Freeing an assigned but not opened file
« Reply #18 on: October 17, 2023, 08:56:10 pm »
How about...
Code: Pascal  [Select][+][-]
  1. function SwapHidden(const AFilename: string): Boolean;
  2. var
  3.   Attr: DWORD;
  4. begin
  5.   Result := False;
  6.   if (not FileExists(AFilename)) then
  7.     Exit;
  8.   Attr := GetFileAttributes(PChar(AFileName));
  9.   if ((Attr and FILE_ATTRIBUTE_HIDDEN) <> 0) then
  10.     Attr := Attr and not FILE_ATTRIBUTE_HIDDEN;
  11.   else
  12.     Attr := Attr + FILE_ATTRIBUTE_HIDDEN;
  13.   Result := Bool(SetFileAttributes(PChar(AFileName), Attr));
  14. end;
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Freeing an assigned but not opened file
« Reply #19 on: October 18, 2023, 10:17:04 am »
@KodeZerg ok that actually worked much better, at least so far so good.  I adjusted (Badly) into my existing procedures (below) and it worked well.

Code: Pascal  [Select][+][-]
  1. procedure TFrameImageBrowser.MakeHidden(const fname : String);
  2. var
  3.    attr : DWORD;
  4. begin
  5.      if fileExists(fname) then
  6.      begin
  7.           try
  8.               attr := GetFileAttributes(pchar(fname));
  9.               if ((Attr and FILE_ATTRIBUTE_HIDDEN) <> 0) then
  10.               begin
  11.                   // This is lazy
  12.               end else
  13.               begin
  14.                    Attr := Attr + FILE_ATTRIBUTE_HIDDEN;
  15.                    SetFileAttributes(PCHAR(fname), attr);
  16.               end;
  17.           except
  18.                 //
  19.           end;
  20.      end;
  21.      sleep(50);
  22.      application.ProcessMessages;
  23. end;
  24.  
  25. procedure TFrameImageBrowser.MakeUnHidden(const fname : String);
  26. var
  27.    attr : DWORD;
  28. begin
  29.      if fileExists(fname) then
  30.      begin
  31.           try
  32.               attr := GetFileAttributes(PCHAR(fname));
  33.               if ((Attr and FILE_ATTRIBUTE_HIDDEN) <> 0) then
  34.               begin
  35.                    Attr := Attr and not FILE_ATTRIBUTE_HIDDEN;
  36.                    SetFileAttributes(PCHAR(fname), attr);
  37.               end;
  38.           except
  39.                 //
  40.           end;
  41.      end;
  42.      sleep(50);
  43.      application.ProcessMessages;
  44. end;
  45.  

As you can see the make hidden is a little clumsy in the if statement, and since I don't actually know it the file is hidden or not to toggle this method does work.  Thanks for that it appears to have done the job.  The problem might even have been that I don't use IniFiles to write my inifiles, I use a unit i've been using for ever that was originally intended to overcome the 64kb limit on ini files.  I'm not even sure if that is even a thing anymore but it's always worked well for me.

CharlyTango

  • Full Member
  • ***
  • Posts: 171
Re: Freeing an assigned but not opened file
« Reply #20 on: October 19, 2023, 07:29:58 am »
As i repeatedly run into file locking issuse using TInifile i changed to TMemIniFile.

The main difference is that read and write actions can be done code controlled on a single point and that the IniFile is competely stored in memory. Only one single read and one single write actions.
The contents of the memory based INI information is written in one single action to file.

All functions to access inifile data are named the same as in TInifile. Changes are onlx necessaty wehn reading or writing to/from disc.

Lazarus stable, Win32/64

 

TinyPortal © 2005-2018