Having problems with the foreach function

Discuss and share scripts and script files...
Mesh
Posts: 956
Joined: 24 Mar 2008 21:22

Re: Having problems with the foreach function

Post by Mesh »

tiago wrote:
I should also point that """selfilter""" has completely different effect than "selfilter", and if (which I don't believe, because my version of your script works 100% of the times, it's not a theoretical solution, it's a WORKING SOLUTION) this is not working for you, I'd like to suggest you to try selecitems, which in its turn perform awesomely under specific situations, as I'm learning.

What I did was to take the two lines in question from your version:

Code: Select all


selfilter """$token""";
#1048;


...and add it to the top of my foreach loop. When I do that and run the script, I get an error "Nothing selected in the file list", when it gets to the rename operation.


However, using "selectitems" does seem to fix the problem, so thank you for that.

tiago wrote:
In the other hand: why are you using foreach, if you're processing the same batch of files all at once?

I'm not. Why do you think I am?

Mesh
Posts: 956
Joined: 24 Mar 2008 21:22

Re: Having problems with the foreach function

Post by Mesh »

Tiago's suggestion to use selectitems fixed the original issue I posted about.

But now that it's working, I'm seeing that using the "recase" command is adding a period at the end of the string it's acting on.

Does anyone know why this is happening?

Here's the current version of code I'm using:

Code: Select all


// Test Script - takes a file like "the before.txt" and *should* rename it to "The After - the before.txt"
// WORKING - but with the recase issue.
	
	// Temporarily disable background file processing.
	setting "BackgroundFileOps", 0;


	foreach($token, <get selecteditemspathnames |>)
	{


	      selectitems "$token"; //ensure no similar names get selected to get the job done
	      // #1048; //ensure the selected item is also focused
	
		//  Copy filename to variables.
		set $work_name, <curname>;

		//  Set variable to store orig filename.
		set $work_origname, <curname>;


		regexreplace $work_name, $work_name, "before", "after";
		regexreplace $work_name, $work_name, "\.txt", "";
	
	
		//  Workaround to change case for the title to Title Case.
		// rename re, "^.*>>$work_name";
		// #126;
		// set $work_name, <curname>;
		$work_name = recase($work_name, "title", 1);
		
	
		//  Final file rename - re-attach original name.
		rename re, "^.*>>$work_name - $work_origname";
		
	}
	
	
	// Restore the original state of background file processing.
	setting "BackgroundFileOps", r;
	
	


If you step through this script, you'll see this progression of events:


Before this line:
regexreplace $work_name, $work_name, "\.txt", "";

$work_name == "the after 1.txt"


After that line:

$work_name == "the after 1"


After the next line:
$work_name = recase($work_name, "title", 1);

$work_name == "The After 1."



Is this a bug, or is something else going on?

nas8e9
Posts: 2232
Joined: 21 Jun 2008 14:50

Re: Having problems with the foreach function

Post by nas8e9 »

Mesh wrote:Tiago's suggestion to use selectitems fixed the original issue I posted about.

But now that it's working, I'm seeing that using the "recase" command is adding a period at the end of the string it's acting on.

Does anyone know why this is happening?

Here's the current version of code I'm using:

Code: Select all


// Test Script - takes a file like "the before.txt" and *should* rename it to "The After - the before.txt"
// WORKING - but with the recase issue.
	
	// Temporarily disable background file processing.
	setting "BackgroundFileOps", 0;


	foreach($token, <get selecteditemspathnames |>)
	{


	      selectitems "$token"; //ensure no similar names get selected to get the job done
	      // #1048; //ensure the selected item is also focused
	
		//  Copy filename to variables.
		set $work_name, <curname>;

		//  Set variable to store orig filename.
		set $work_origname, <curname>;


		regexreplace $work_name, $work_name, "before", "after";
		regexreplace $work_name, $work_name, "\.txt", "";
	
	
		//  Workaround to change case for the title to Title Case.
		// rename re, "^.*>>$work_name";
		// #126;
		// set $work_name, <curname>;
		$work_name = recase($work_name, "title", 1);
		
	
		//  Final file rename - re-attach original name.
		rename re, "^.*>>$work_name - $work_origname";
		
	}
	
	
	// Restore the original state of background file processing.
	setting "BackgroundFileOps", r;
	
	


If you step through this script, you'll see this progression of events:


Before this line:
regexreplace $work_name, $work_name, "\.txt", "";

$work_name == "the after 1.txt"


After that line:

$work_name == "the after 1"


After the next line:
$work_name = recase($work_name, "title", 1);

$work_name == "The After 1."



Is this a bug, or is something else going on?
Purely going by the help file, you're adding the only supported flag (1) to the recase command. This flag acts on full file names (including extensions) whereas $work_name *doesn't* contain the extension. Based on that, removing the flag might fix it?

admin
Site Admin
Posts: 66249
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Having problems with the foreach function

Post by admin »

Mesh wrote:
admin wrote:Please explain in general terms what kind of rename operation you are trying to make, and what are the filenames you are going to rename.
This test script is a stripped down version of the real one I was working with. When I experienced this issue, I stripped out most of the extraneous operations to make it easier to duplicate and demonstrate this issue.

This script will take the original filename, and prefix it with a modified version of the information in the original name, while keeping the original name intact. Here is what the script *should* be doing (which it does just fine if you only use it on a single file):


Original Files:

the before 1.txt
the before 2.txt


What they should be after renaming:

The After 1.txt - the before 1.txt
The After 2.txt - the before 2.txt
Looks simple. But: what is "the After". Can you give an example?

Mesh
Posts: 956
Joined: 24 Mar 2008 21:22

Re: Having problems with the foreach function

Post by Mesh »

nas8e9 wrote:
Purely going by the help file, you're adding the only supported flag (1) to the recase command. This flag acts on full file names (including extensions) whereas $work_name *doesn't* contain the extension. Based on that, removing the flag might fix it?

You were right, that was it. The behavior I expected using the flag was that *if* there was an extension, it would make it lowercase, but if there wasn't an extension, it wouldn't affect anything. That assumption was my mistake, but I do believe that what I expected is what the behavior probably should be.

Thanks for pointing me in the right direction. :)

Mesh
Posts: 956
Joined: 24 Mar 2008 21:22

Re: Having problems with the foreach function

Post by Mesh »

admin wrote:
Looks simple. But: what is "the After". Can you give an example?

Using selecteditems has fixed this issue for me. I think the original problem is that I expected the foreach command to change the selection and focus to each token as it iterated through the loops. Because it doesn't, I had to do it manually with the selecteditems command.


But to explain the overall purpose of what I'm doing, I can take files like this:

budget.01.14.95.accounting.xls
2008.4.7.accounting.expenditures.xls
operations.10192001.personnel.doc
operations budget-3-13-12.doc


And turn them into this:

Accounting - 1995-01-14 - Budget - budget.01.14.95.accounting.xls
Accounting - 2008-04-07 - Expenditures - 2008.4.7.accounting.expenditures.xls
Operations - 2012-03-13 - Budget - operations budget-3-13-12.doc
Operations - 2001-10-19 - Personnel - personnel.10192001.operations.doc

admin
Site Admin
Posts: 66249
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Having problems with the foreach function

Post by admin »

Mesh wrote:
admin wrote:
Looks simple. But: what is "the After". Can you give an example?

Using selecteditems has fixed this issue for me. I think the original problem is that I expected the foreach command to change the selection and focus to each token as it iterated through the loops. Because it doesn't, I had to do it manually with the selecteditems command.


But to explain the overall purpose of what I'm doing, I can take files like this:

budget.01.14.95.accounting.xls
2008.4.7.accounting.expenditures.xls
operations.10192001.personnel.doc
operations budget-3-13-12.doc


And turn them into this:

Accounting - 1995-01-14 - Budget - budget.01.14.95.accounting.xls
Accounting - 2008-04-07 - Expenditures - 2008.4.7.accounting.expenditures.xls
Operations - 2012-03-13 - Budget - operations budget-3-13-12.doc
Operations - 2001-10-19 - Personnel - personnel.10192001.operations.doc
Looks like this is doable with a RegExp Rename one-liner, so need for scripting at all. But don't ask me how, I'm no RegExp man... :)

tiago
Posts: 589
Joined: 14 Feb 2011 21:41

Re: Having problems with the foreach function

Post by tiago »

"I'm not. Why do you think I am?"
Well, if you were not assigning a token for the script to work with, the script was just working over the whole bunch and you were having issues because of that, simple.
But it seems you got it now, right?
Power-hungry user!!!

Mesh
Posts: 956
Joined: 24 Mar 2008 21:22

Re: Having problems with the foreach function

Post by Mesh »

admin wrote:
Looks like this is doable with a RegExp Rename one-liner, so need for scripting at all. But don't ask me how, I'm no RegExp man... :)

I am, and it's definitely not a one liner. :)

I had a working script previously, but it required Jacky's looping script. I was trying to port it to the foreach implementation so it wouldn't have any external dependencies, when I ran into this snafu. :)

Mesh
Posts: 956
Joined: 24 Mar 2008 21:22

Re: Having problems with the foreach function

Post by Mesh »

tiago wrote:
Well, if you were not assigning a token for the script to work with, the script was just working over the whole bunch and you were having issues because of that, simple.
But it seems you got it now, right?
Yes, I understand what was going on now. Thank you for pointing me in the right direction.

Post Reply