Help needed with a tiny script!

Discuss and share scripts and script files...
Post Reply
Biggynuff
Posts: 110
Joined: 13 May 2010 14:08

Help needed with a tiny script!

Post by Biggynuff »

Hi all,

I've been having some success with some very simple but amazingly useful scripts, but there's one little job I'd love to be able to do and the solution escapes me every time . . .

I'm trying to write a script to take the size of selected folders (directories) in the list view and copy the folder size to the comment field. It should be so SIMPLE but it's driving me MAAAAAADDD!!!

Anyway, heres what I've done so far:

Code: Select all


#431; // Toggle showing folder sizes

 $size = (report("{Size GB}",1)); // Get the size of the folder

 tag "$size", , 2; // Set the size in the comment field

 #431; // Toggle off showing folder sizes

 savesettings; // So I don't lose my comments when I exit XY!

This works for one (and only one) selected directory. Useful, yes, but not quite there yet. So I've been looking at using a 'foreach' loop to take the selected directories and tag the comment field for each. I've tried a few things, but this is my latest attempt:

Code: Select all


#431; // Toggle showing folder sizes

   $Files = get("SelectedItemsPathNames", "|");

   foreach($file, $Files, "|") {
     
     $size = ""; // Reset the variable I'll use as a container for the size

     $size = (report("{Size GB}",1)); // Get the size of the folder or file

     tag "$size", , 2; // Set the size in the comment field

   }

 #431;

 savesettings;

Now, what happens here is that the selected folders are listed correctly in the variable $Files. Each appears to be stepped through, but the result is that all of the folder sizes are concatenated together and each of the selected folders gets the comment field tagged with ALL of the folder sizes i.e. the comment field looks like this:

Code: Select all


 6.7GB3.4GB2.9GB5.8GB etc etc

I just look at this code and I feel that I'm using the 'foreach' command in completely the wrong way, but whatever I try I can't seem to figure this.

Having tried several times I know I'm on the wrong track here. Can anyone advise and adjust this code so it get's closer to the desired outcome?

Thanks for any advice

Biggy

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Help needed with a tiny script!

Post by highend »

<- Scripting novice as well but I've learned a bit from Stefan *g*

Code: Select all

#431; // Toggle showing folder sizes

	$Files = get("SelectedItemsPathNames", "|");

	$size = report("{Size GB}|", 1); // Get the size of the folder or file

	$i = 1;
	foreach($file, $Files, "|") {
		$result = gettoken($size, $i, "|");
			tag "$result", $file, 2; // Set the size in the comment field
			$i++;
	}

	#431;
Errors you've made:

The report belongs outside the foreach loop, it gets the size for all selected items in one (big) list. Forgot to supply the separator for it (to handle it easier).

From the help file:
itemlist |-separated list of items (full path) to tag;
if empty then current list selections are tagged
-> One tag belongs to one file, if you leave it empty you'll try to write one tag to multiple files at once (at least that's how I understand the help file).

You don't have to reset the size in the foreach loop each time.

How the script works:
gettoken will get the corresponding entry from the $size variable for each entry in the $Files list via the variable $i that will be counted up with each loop so folder size 3 (from $size array) will be tagged into the comments of folder 3 from the $Files list.

Thanks for explaining your problem with such a high grade of detail (with lot's of code).
Makes it easy to understand it and allows script novices to help, too :D

Regards,
highend
One of my scripts helped you out? Please donate via Paypal

Biggynuff
Posts: 110
Joined: 13 May 2010 14:08

Re: Help needed with a tiny script!

Post by Biggynuff »

Wonderful!

Thanks highend, I'll be looking over this one for some time to come. Same as you, I've learned a lot from Stefan but every now and again I suffer from mental blocks like brick walls!

Biggy

Edit:

highend, I've been reading through your explanation again, particularly the errors I made with the script. Once again, thank you very much indeed. This makes so much sense now. I knew just looking at the script I'd tried that there was something clearly and obviously wrong. The really, really weird thing is that now I've seen the solution it seems so obvious. DOH :oops:

Stefan
Posts: 1360
Joined: 18 Nov 2008 21:47
Location: Europe

Re: Help needed with a tiny script!

Post by Stefan »

Aha, we need 431 enabled to make report(size) work on folder too. I don't know that and was thinking something is wrong.

Now i wanted to provide my way too, but that doesn't work on folders, only on files.



// needs "Show folder size" enabled to get the size of folders
// #431; // Toggle showing folder sizes

Code: Select all

foreach($folder, <get selecteditemspathnames |>){
    tag . report("{Size GB}", "$folder"), $folder, 2;
}

highend
Posts: 14940
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Help needed with a tiny script!

Post by highend »

This makes so much sense now. I knew just looking at the script I'd tried that there was something clearly and obviously wrong. The really, really weird thing is that now I've seen the solution it seems so obvious.
This happens to me quite a lot but Stefan is very helpful when I have stupid questions :)

My rsync / ssh script (for Strato - HiDrive) is nearly finished and I'm afraid it could have been done with 1/3 of the code, hihi...
One of my scripts helped you out? Please donate via Paypal

Post Reply