Page 1 of 1
Rename selected files by extension
Posted: 21 Oct 2021 08:40
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!!
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";
}
}
Re: Rename selected files by extension
Posted: 21 Oct 2021 08:44
by highend
Then read the help file entry for rename?
Re: Rename selected files by extension
Posted: 21 Oct 2021 09:44
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.
Re: Rename selected files by extension
Posted: 21 Oct 2021 09:56
by admin
Then have a look at renameitem.
Re: Rename selected files by extension
Posted: 21 Oct 2021 09:58
by highend
That requires reading as well, Don^^ I know, totally crazy...
Re: Rename selected files by extension
Posted: 22 Oct 2021 01:01
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.
Re: Rename selected files by extension
Posted: 22 Oct 2021 01:07
by jupe
You want rename to only work on certain files, but have you given the rename function a filename to process?
Re: Rename selected files by extension
Posted: 22 Oct 2021 01:17
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.
Re: Rename selected files by extension
Posted: 22 Oct 2021 01:22
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
to
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.
Re: Rename selected files by extension
Posted: 22 Oct 2021 01:23
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.
Re: Rename selected files by extension
Posted: 22 Oct 2021 04:23
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.
Re: Rename selected files by extension
Posted: 22 Oct 2021 04:26
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

Re: Rename selected files by extension
Posted: 22 Oct 2021 04:56
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. 
Re: Rename selected files by extension
Posted: 22 Oct 2021 06:04
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
Re: Rename selected files by extension
Posted: 22 Oct 2021 21:02
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.
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!