Page 1 of 1

How to set file properties with a script command?

Posted: 05 Sep 2011 18:17
by Jerry
Hi all,
I've looked everywhere but can't find the answer: How do you use a script to set file properties? For example, I want to simply have a script or script command to change all selected files to read-only. More specifically, I want to use it as a on/off toggle. I see the property command but it only retrieves properties.

Re: How to set file properties with a script command?

Posted: 05 Sep 2011 18:25
by admin
There is currently no scripting command to set attributes.

But you can simply do it from the Properties tab on the Info Panel (F12). Click on one file to get the info tab filled, then select the files you want to set the attributes to, then click the attributes checkboxes, then click the appearing "green tick" button to apply the settings.

Re: How to set file properties with a script command?

Posted: 05 Sep 2011 19:17
by Jerry
admin wrote:There is currently no scripting command to set attributes.
But you can simply do it from the Properties tab on the Info Panel (F12). Click on one file to get the info tab filled, then select the files you want to set the attributes to, then click the attributes checkboxes, then click the appearing "green tick" button to apply the settings.
Yes, I occasionally use that method, but it involves a lot of steps: After selecting the files, you have to 1) click on the toolbar to show the properties panel; 2) click on the Read-only checkbox; 3) click on the "green tick" button; and 4) click on the toolbar again to dismiss the properties panel. That's because when I use XYplorer, I always have it sized to the full width of my desktop but only half the height so I therefore don't want the info panel to always show.

Alternatively, I have using the Attributes plugin to Creative Elements Powertools but even this is cumbersome when there is a mixed set of read and read-only files in the selected list -- in that situation it always first toggles off before toggling on. Since discovering XYplorer, I have been using CE Powertools less and less but this read-only toggle function is still more efficient than doing it in XYplorer.

So, I would like to see a scripting command for setting/clearing properties, at least one for the basic file properties, read-only, hidden, system, etc. Is this a possibility or does Don consider it too specific a request?

Re: How to set file properties with a script command?

Posted: 05 Sep 2011 21:16
by admin
Yes, it's a possibility. I'll add it.

Re: How to set file properties with a script command?

Posted: 05 Sep 2011 22:59
by Jerry
admin wrote:Yes, it's a possibility. I'll add it.
Wonderful news! Thanks Don.

Re: How to set file properties with a script command?

Posted: 12 Sep 2011 09:45
by admin
So, does it work for you now?

Re: How to set file properties with a script command?

Posted: 12 Sep 2011 19:09
by Jerry
admin wrote:So, does it work for you now?
Oh, in the current beta. Yes, looks good. I had to write the following script around it to get the precise toggling behavior I want, since attrstamp("r", 3); won't turn the attribute off on all the selected files if any of them are already set. But I recognize that's a specific usage not to reasonably expect in the core function. ;-)
Thanks Don.

Code: Select all

// Toggles readonly attribute on selected files: If any file has readonly already
// set than all the files are unset. Otherwise all are set
$fileList = Report("{Fullname}<crlf>", 1); 
  $anyReadOnly=0;  // indicates whether any of the files have readonly set
  foreach ($file, "$fileList", "<crlf>") {
     if ((attrstamp("r", 0, $file) % 2) == 1) {
   	  $anyReadOnly=1;
 	  break;
    }
  }
  if ($anyReadOnly == 1) {
    attrstamp("r", 4); // remove readonly on selected files
  }
  else {
    attrstamp("r");  // set readonly on selected files
  }

Re: How to set file properties with a script command?

Posted: 12 Sep 2011 20:27
by TheQwerty
Jerry wrote:

Code: Select all

// Toggles readonly attribute on selected files: If any file has readonly already
// set than all the files are unset. Otherwise all are set
$fileList = Report("{Fullname}<crlf>", 1); 
  $anyReadOnly=0;  // indicates whether any of the files have readonly set
  foreach ($file, "$fileList", "<crlf>") {
     if ((attrstamp("r", 0, $file) % 2) == 1) {
   	  $anyReadOnly=1;
 	  break;
    }
  }
  if ($anyReadOnly == 1) {
    attrstamp("r", 4); // remove readonly on selected files
  }
  else {
    attrstamp("r");  // set readonly on selected files
  }
You could really shorten this by using report to get the items' attributes and then checking for 'r':

Code: Select all

"Toggle Read-Only"
  if (StrPos(Report('{Attr}', 1), 'r') < 0) {
    AttrStamp('r', 1);
  } else {
    AttrStamp('r', 4);
  }

Don, any reason the attribute argument of AttrStamp is case-sensitive? And worse so quiet about it?

Re: How to set file properties with a script command?

Posted: 12 Sep 2011 23:51
by Jerry
You could really shorten this by using report to get the items' attributes and then checking for 'r':
Excellent! And more efficient performance-wise no doubt. I am just starting out with XY's scripting so I appreciate the tips!

Re: How to set file properties with a script command?

Posted: 13 Sep 2011 08:29
by admin
TheQwerty wrote:Don, any reason the attribute argument of AttrStamp is case-sensitive? And worse so quiet about it?
Right, I just made it case-insensitive. Error on invalid argument does not seem necessary.

Re: How to set file properties with a script command?

Posted: 13 Sep 2011 12:34
by TheQwerty
admin wrote:Right, I just made it case-insensitive.
Much better, and the fact that it ignores other characters silently is actually a good thing since you can plug in the results of Report's {Attr} value without reformatting it.
admin wrote:Error on invalid argument does not seem necessary.
Indeed, especially since my own laziness and lack of thinking led me to ignore the returned values from AttrStamp which I could have used to verify the operation's success.

Re: How to set file properties with a script command?

Posted: 06 Mar 2022 03:07
by Jerry
Jerry wrote: ↑05 Sep 2011 18:17 Hi all,
I've looked everywhere but can't find the answer: How do you use a script to set file properties? ...
Here's the script that I wrote and have been using, oh my goodness, for over 10 years now:

// Toggles readonly attribute on selected files:
// If any file has readonly already set then all the files are unset. Otherwise all are set.
if (StrPos(Report('{Attr}', 1), 'r') < 0) {
AttrStamp('r', 1);
} else {
AttrStamp('r', 4);
}