Recent

Author Topic: How to make readonly file not readonly????  (Read 5370 times)

vonskie

  • Full Member
  • ***
  • Posts: 184
How to make readonly file not readonly????
« on: October 26, 2016, 04:56:38 pm »
How to make readonly file not readonly????
var
 Attr : Word;
   f    : File;   

{etc}

f:= SourceDirectoryAndFileName;
              Assign(f,ParamStr(1));
              GetFAttr(f,Attr);
              if (Attr and readonly)<>0 then setfattr(>Help?<);


Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: How to make readonly file not readonly????
« Reply #1 on: October 26, 2016, 05:01:44 pm »
Perhaps:

Code: [Select]
  Attr := FileGetAttr(Fn);
  if ((Attr and faReadOnly) = faReadOnly) then
    FileSetAttr(Fn, Attr and (not faReadOnly));

Bart

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: How to make readonly file not readonly????
« Reply #2 on: October 26, 2016, 09:13:57 pm »
How to make readonly file not readonly????

Code: [Select]
var
  Attr : Word;
  f    : File;   
  ...

  Assign(f, ParamStr(1));
  GetFAttr(f,Attr);
  if (Attr and ReadOnly) <> 0 then
    SetFAttr(f, Attr and (not ReadOnly));
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: How to make readonly file not readonly????
« Reply #3 on: October 26, 2016, 10:01:47 pm »
Mine is 1 line shorter  O:-)

Bart

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: How to make readonly file not readonly????
« Reply #4 on: October 27, 2016, 02:54:30 am »
Mine is 1 line shorter  O:-)

Code: [Select]
Assign(f, ParamStr(1)); GetFAttr(f,Attr);
if (Attr and ReadOnly) <> 0 then SetFAttr(f, Attr and (not ReadOnly));

Now mine is 1 line shorter than yours :P

Code: [Select]
Assign(f, ParamStr(1)); GetFAttr(f,Attr); SetFAttr(f, Attr and (not ReadOnly));

Now 2 lines shorter :P
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: How to make readonly file not readonly????
« Reply #5 on: October 27, 2016, 04:09:38 pm »
Mine does not need the Assign() part.

Bart

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: How to make readonly file not readonly????
« Reply #6 on: October 27, 2016, 04:18:34 pm »
Code: [Select]
Attr := FileGetAttr(Fn);
if ((Attr and faReadOnly) <> 0) then FileSetAttr(Fn, Attr xor faReadOnly);

Mine is faster than Bart's! ;)

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: How to make readonly file not readonly????
« Reply #7 on: October 27, 2016, 05:03:25 pm »
Code: Pascal  [Select][+][-]
  1. Attr := FileGetAttr(Fn);
  2. if ((Attr and faReadOnly) <> 0) then FileSetAttr(Fn, Attr xor faReadOnly);

Mine is faster than Bart's! ;)

Oh well, how about solving it in pure boolean expressions?
I see this? Really? Seriously? THINK!
Can I plz read the proper solution?

Giveaway: just one gets rid of the if then....
« Last Edit: October 27, 2016, 05:07:56 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: How to make readonly file not readonly????
« Reply #8 on: October 27, 2016, 05:50:38 pm »
@Thaddy (aka. Grumpy): Do you call for this sollution:

Code: [Select]
FileSetAttr(Fn, FileGetAttr(Fn) and (not faReadOnly));

If so, it is a flawed sollution ;)

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: How to make readonly file not readonly????
« Reply #9 on: October 27, 2016, 11:14:00 pm »
The xor solution may give wrong result.

let's assume you have some flag
F1 = %0001
F2 = %0010
F3 = %0100
F4 = %1000

F  = %1011 ( F1 OR F2 OR F4 )
F AND (NOT F3) = %1011
but F XOR F3   = %1111 = F OR F3  <---- WRONG - With this it includes the flag

So Fungus, sorry but your solution with XOr won't exclude/negate the Flag in question. (or did I miss something?!)

EDIT:
It'll only work when the Flag is set, so it should only used after checking (handle with care, please)
« Last Edit: October 27, 2016, 11:19:31 pm by shobits1 »

Fungus

  • Sr. Member
  • ****
  • Posts: 353
Re: How to make readonly file not readonly????
« Reply #10 on: October 28, 2016, 01:21:45 pm »
So Fungus, sorry but your solution with XOr won't exclude/negate the Flag in question. (or did I miss something?!)

EDIT:
It'll only work when the Flag is set, so it should only used after checking (handle with care, please)

As you probably figured out, the XOR method above works 100% as intended since the bits presence is tested before it is flipped. In the later "one line" sollution I reverted to Bart's AND NOT sollution because I'm aware of the fact that it would not work with XOR there :)

 

TinyPortal © 2005-2018