Rename selected files by extension

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
kiwichick
Posts: 321
Joined: 08 Aug 2012 04:14

Rename selected files by extension

Post by kiwichick »

I want to rename files by adding ".en" to end of the basename of files with the extension "srt". I have the following code and the rename works but it renames all selected files regardless of their extension and I can't figure out how to fix it. Loops and if/else always trip me up!! :veryconfused: :lol:

Code: Select all

foreach($item, <get selecteditemspathnames>, <crlf>, , "No files selected.") {
	if (gpc($item, "ext") == "srt") {
		rename , "*.en";
		}
	else {
		msg "This is not an srt file";
		}	
	}

highend
Posts: 11075
Joined: 06 Feb 2011 00:33

Re: Rename selected files by extension

Post by highend »

Then read the help file entry for rename?
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

kiwichick
Posts: 321
Joined: 08 Aug 2012 04:14

Re: Rename selected files by extension

Post by kiwichick »

highend wrote: 21 Oct 2021 08:44 Then read the help file entry for rename?
Thank highend, but that's far too cryptic. What exactly are you referring to in the rename entry? There's a lot there.

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

Re: Rename selected files by extension

Post by admin »

Then have a look at renameitem.

highend
Posts: 11075
Joined: 06 Feb 2011 00:33

Re: Rename selected files by extension

Post by highend »

That requires reading as well, Don^^ I know, totally crazy...
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

kiwichick
Posts: 321
Joined: 08 Aug 2012 04:14

Re: Rename selected files by extension

Post by kiwichick »

highend wrote: 21 Oct 2021 09:58 That requires reading as well, Don^^ I know, totally crazy...
I don't appreciate you just assuming I haven't read that section. I HAVE! And I don't understand what part of it you are referring to. I can't see what applies to my code. Just because I'm not an expert like you doesn't mean I haven't tried to get it working myself.

jupe
Posts: 1722
Joined: 20 Oct 2017 21:14
Location: Win10 21H2 120dpi

Re: Rename selected files by extension

Post by jupe »

You want rename to only work on certain files, but have you given the rename function a filename to process?

kiwichick
Posts: 321
Joined: 08 Aug 2012 04:14

Re: Rename selected files by extension

Post by kiwichick »

jupe wrote: 22 Oct 2021 01:07 You want rename to only work on certain files, but have you given the rename function a filename to process?
Well I thought I had. I thought <get selecteditemspathnames> got all the filenames and then each $item that had the extension srt would be "given" to the rename function. Apparently that's not right cos it doesn't work but I'm really struggling trying to fix it when I don't really know which part of my code isn't working.

kiwichick
Posts: 321
Joined: 08 Aug 2012 04:14

Re: Rename selected files by extension

Post by kiwichick »

admin wrote: 21 Oct 2021 09:56 Then have a look at renameitem.
Thanks Don, I had a look at that section and I thought changing

Code: Select all

rename , "*.en";
to

Code: Select all

renameitem("*.en",,1);
would help but it didn't. It's hard to know what to look for when I don't understand what part of my code isn't working.

jupe
Posts: 1722
Joined: 20 Oct 2017 21:14
Location: Win10 21H2 120dpi

Re: Rename selected files by extension

Post by jupe »

I thought I gave you a clue to what part of your code isn't working, by indicating you weren't supplying rename with a filename, anyway below is a modded script, which provides a filename to rename, because without it rename processes the whole selection, which is also mentioned in the documentation you read.

Code: Select all

	foreach($item, <get selecteditemspathnames>, <crlf>, , "No files selected.") {
	if (gpc($item, "ext") == "srt") {
		rename , "*.en", , $item;
		}
	else {
		msg "This is not an srt file";
		}	
	}
This could also be done without a foreach, but I'll leave it as is.

kiwichick
Posts: 321
Joined: 08 Aug 2012 04:14

Re: Rename selected files by extension

Post by kiwichick »

jupe wrote: 22 Oct 2021 01:23 I thought I gave you a clue to what part of your code isn't working, by indicating you weren't supplying rename with a filename, anyway below is a modded script, which provides a filename to rename, because without it rename processes the whole selection, which is also mentioned in the documentation you read.

Code: Select all

	foreach($item, <get selecteditemspathnames>, <crlf>, , "No files selected.") {
	if (gpc($item, "ext") == "srt") {
		rename , "*.en", , $item;
		}
	else {
		msg "This is not an srt file";
		}	
	}
This could also be done without a foreach, but I'll leave it as is.
Oh my gosh, thank you so much! I feel a bit of an idiot now, it was such an easy fix. But it just goes to show how we can all read things differently. I knew what you were getting at but I couldn't see how it related to what I was doing wrong. Your patience and explanation are very much appreciated.

kiwichick
Posts: 321
Joined: 08 Aug 2012 04:14

Re: Rename selected files by extension

Post by kiwichick »

jupe wrote: 22 Oct 2021 01:23 This could also be done without a foreach, but I'll leave it as is.
I tried it without the foreach loop and it didn't work :)

jupe
Posts: 1722
Joined: 20 Oct 2017 21:14
Location: Win10 21H2 120dpi

Re: Rename selected files by extension

Post by jupe »

By "without a foreach", I meant something like this:

rename , '*.en', , formatlist(<selitems |>, 'f', , '*.srt');

what I didn't mean >>> just remove the foreach without any other changes. :veryconfused:

highend
Posts: 11075
Joined: 06 Feb 2011 00:33

Re: Rename selected files by extension

Post by highend »

I don't appreciate you just assuming I haven't read that section. I HAVE! And I don't understand what part of it you are referring to. I can't see what applies to my code
itemlist CRLF- or |-separated list of items (full path) to rename. If empty then current list selections are renamed. The separator may be surrounded by any number of blanks.
If empty then current list selections are renamed
One of my scripts helped you out? Please donate via Paypal or paypal_donate (at) stdmail (dot) de

kiwichick
Posts: 321
Joined: 08 Aug 2012 04:14

Re: Rename selected files by extension

Post by kiwichick »

jupe wrote: 22 Oct 2021 04:56 By "without a foreach", I meant something like this:

rename , '*.en', , formatlist(<selitems |>, 'f', , '*.srt');

what I didn't mean >>> just remove the foreach without any other changes. :veryconfused:
Oh geez, my brain must be taking a coding break. I didn't even consider that. Gona go give myself a jolly good talking to!

Post Reply