Recent

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

zxandris

  • Full Member
  • ***
  • Posts: 170
[Solved] - Freeing an assigned but not opened file
« on: October 16, 2023, 06:06:30 am »
Essentially I'm trying to set the hidden attiribute of a file I create/work with.  I think I'm doing that right, but then it locks the file and CloseFile doesn't work because I don't actually have the file open, Set Attribute doesn't want the file open apparently just assigned, so how do I unassign the file and unlock the file.  Any help here would be marvellous, thanks.

Or if anyone has an actual function to do that it'd be great.  I've searched around and no-where does it say how to close just an assigned file.
« Last Edit: October 20, 2023, 09:24:09 am by zxandris »

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Freeing an assigned but not opened file
« Reply #1 on: October 16, 2023, 06:57:46 am »
I have no idea about what you described. Would it not be simpler to use FileSetAttr as that only requires you to provide a filename (and the attributes) ?
Today is tomorrow's yesterday.

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Freeing an assigned but not opened file
« Reply #2 on: October 16, 2023, 06:59:25 am »
You know what that would be much simplier, thanks.  My searches believe it or not never came up with that as an option lol.  I'll do this thank you

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Freeing an assigned but not opened file
« Reply #3 on: October 16, 2023, 07:08:05 am »
It might be that you already have the file open to perform other action(s) on the file, in which case you would need to close the file first and then change the attributes (which I imagine is not the nicest of program flow) so perhaps another solution could be more practical for your use case.

Unfortunately there is some (code) context missing so without such context you would have to decide that for yourself  :)
Today is tomorrow's yesterday.

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Freeing an assigned but not opened file
« Reply #4 on: October 16, 2023, 07:13:23 am »
It might be that you already have the file open to perform other action(s) on the file, in which case you would need to close the file first and then change the attributes (which I imagine is not the nicest of program flow) so perhaps another solution could be more practical for your use case.

Unfortunately there is some (code) context missing so without such context you would have to decide that for yourself  :)

I'm just hiding an ini file I use for properties while browsing a directory, so I want to set the attirbute after I've written to the file.  I had code at one point but since i had to restart my pc several times to unlock the file I ended up deleting that.  I think the FileSetAttr thing will do the job - thank you

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Freeing an assigned but not opened file
« Reply #5 on: October 16, 2023, 07:40:33 am »
Thank you for the additional context.

In case using tinifile then the file is already closed when you destroy the object/class and besides that you have no direct way of accessing the filehandle so indeed using SetFileAttr would be the most simple and efficient approach (at least to my current knowledge)
Today is tomorrow's yesterday.

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Freeing an assigned but not opened file
« Reply #6 on: October 16, 2023, 07:50:53 am »
Awesome, thank you so much.  I'm pretty new to the forum but so far loving how helpful you guys are! :)

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: [SOLVED] - Freeing an assigned but not opened file
« Reply #7 on: October 17, 2023, 02:17:01 pm »
Okay, so a little check with you guys here, I've made this procedure that should - I think - set the hidden attribute if it's not already set

Code: Pascal  [Select][+][-]
  1. procedure TFrameImageBrowser.MakeHidden(const fname : String);
  2. var
  3.    attri : LongInt;
  4. begin
  5.      if fileExists(fname) then
  6.      begin
  7.           try
  8.               attri := FileGetAttr(fname);
  9.               if Attri <> - 1 then
  10.               begin
  11.                    if (attri and faHidden)<>0 then
  12.                    begin
  13.                         attri := attri or faHidden;
  14.                         FileSetAttr(fname, attri);
  15.                    end;
  16.               end;
  17.           except
  18.                 //
  19.           end;
  20.      end;
  21. end;    
  22.  

Will that work, I think it should but I wouldn't mind a little check there because I've never worked with files like this before.
« Last Edit: October 17, 2023, 02:27:18 pm by zxandris »

cdbc

  • Hero Member
  • *****
  • Posts: 2531
    • http://www.cdbc.dk
Re: Freeing an assigned but not opened file
« Reply #8 on: October 17, 2023, 03:07:16 pm »
Hi
Hmmm, shouldn't this:
Code: Pascal  [Select][+][-]
  1.                  if (attri and faHidden)<>0 then // <-- ?!?
  2.                    begin
  3.                         attri := attri or faHidden;
  4.                         FileSetAttr(fname, attri);
  5.                    end;
  6.  
  7.  
be like this:
Code: Pascal  [Select][+][-]
  1.                  if (attri and faHidden) = 0 then // <-- (!)
  2.                    begin
  3.                         attri := attri or faHidden;
  4.                         FileSetAttr(fname, attri);
  5.                    end;
I gather you're checking if "faHidden" is already set, and if not, then you "or" it to the attributes...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Freeing an assigned but not opened file
« Reply #9 on: October 17, 2023, 03:08:48 pm »
Hi
Hmmm, shouldn't this:
Code: Pascal  [Select][+][-]
  1.                  if (attri and faHidden)<>0 then // <-- ?!?
  2.                    begin
  3.                         attri := attri or faHidden;
  4.                         FileSetAttr(fname, attri);
  5.                    end;
  6.  
  7.  
be like this:
Code: Pascal  [Select][+][-]
  1.                  if (attri and faHidden) = 0 then // <-- (!)
  2.                    begin
  3.                         attri := attri or faHidden;
  4.                         FileSetAttr(fname, attri);
  5.                    end;
I gather you're checking if "faHidden" is already set, and if not, then you "or" it to the attributes...
Regards Benny

You see, this is why I needed someone to check my work lol. 

Thank you so much!

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Freeing an assigned but not opened file
« Reply #10 on: October 17, 2023, 03:15:16 pm »
Hmmm, shouldn't this:
Indeed and a good catch cdbc.

Even while/when your correction is evident, and for this kind of situation I prefer
Code: Pascal  [Select][+][-]
  1. if (attri and faHidden)<>faHidden
  2.  
in order to make it even more evident.

BTW: I haven't encountered it myself but be aware that this way it is theoretical possible to hide volumes.
Today is tomorrow's yesterday.

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Freeing an assigned but not opened file
« Reply #11 on: October 17, 2023, 03:17:24 pm »
Hmmm, shouldn't this:
Indeed and a good catch cdbc.

Even while/when your correction is evident, and for this kind of situation I prefer
Code: Pascal  [Select][+][-]
  1. if (attri and faHidden)<>faHidden
  2.  
in order to make it even more evident.

BTW: I haven't encountered it myself but be aware that this way it is theoretical possible to hide volumes.

That does make it easier to remember what I'm doing there and is more evident as you say.

Thanks guys,

CJ

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Freeing an assigned but not opened file
« Reply #12 on: October 17, 2023, 05:02:32 pm »
Okay, weird, but that function we've been working on seems to still lock the file becuase the next time I go to write to it I get a file is in use error.  Any ideas, becuase I'm lost :)

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Freeing an assigned but not opened file
« Reply #13 on: October 17, 2023, 05:09:47 pm »
I experimented and it appears that for the ini i use I have to unhide the file to enable writing to it, so that's what I did. I'm not entirely sure this is right but it appears to work.

Code: Pascal  [Select][+][-]
  1. procedure TFrameImageBrowser.MakeUnHidden(const fname : String);
  2. var
  3.    attri : LongInt;
  4. begin
  5.      if fileExists(fname) then
  6.      begin
  7.           try
  8.               attri := FileGetAttr(fname);
  9.               if Attri <> - 1 then
  10.               begin
  11.                   if (attri and faHidden) = faHidden then
  12.                   begin
  13.                        attri := attri xor faHidden;
  14.                        FileSetAttr(fname, attri);
  15.                   end;
  16.               end;
  17.           except
  18.                 //
  19.           end;
  20.      end;
  21.      sleep(25);
  22. end;  
  23.  

It's probably a bit clunky, but as I said it appears to work.

zxandris

  • Full Member
  • ***
  • Posts: 170
Re: Freeing an assigned but not opened file
« Reply #14 on: October 17, 2023, 05:57:38 pm »
It appears I lied, that doens't work at all, it's just my error grabbing stopped me realising lol.  SHould've run it debug!

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.
« Last Edit: October 17, 2023, 06:15:34 pm by zxandris »

 

TinyPortal © 2005-2018