Page 1 of 2

Rename only part of a file on certain conditions

Posted: 08 Apr 2022 13:12
by chumbo
Hi,
I'm eager to learn scripting as I've only just started my journey in coding and am at an early stage of learning Python.
However, seeing little XY scripting snippets here and there tells me I should be able to already do a few basic scripts on my own!

But what I'm trying to do now is maybe above my abilities for now....
I'd like to be able to rename files and folders by removing all the dots found between words and not between numbers.
So a file with this name:

Code: Select all

this.is.a.file.v1.2.3.78.txt
Would be renamed to:

Code: Select all

this is a file v1.2.3.78.txt
I'm just not clear on how I would apply the conditionals to avoid renaming between the integers.
Thx in advance! :tup:

Re: Rename only part of a file on certain conditions

Posted: 08 Apr 2022 14:38
by highend
E.g. by looping over the (base) string via a while loop.

If the current char is a dot and the next char doesn't match a regular expression [0-9], set the current char to a space...

Or much faster and cleaner, a regexreplace() with a negative lookahead^^

Re: Rename only part of a file on certain conditions

Posted: 08 Apr 2022 14:53
by chumbo
Right, thx. I appreciate what you're doing, nudging me in the right direction so I can figure it out myself but I'm afraid I'm really not there yet.
I'm very much a beginner and would have to learn all the syntax of of the scripting language (does it have a name?) and I have no clue whatsoever about Regular Expressions so I'm not even sure that if I spent the whole day and more, if I could even manage to write it.
So I'd really appreciate it if it's not too complicated and long, if someone could just post the script and I'll then be able to learn a lot from that. Just by dissecting it and understanding how it works.
Thx!

Re: Rename only part of a file on certain conditions

Posted: 08 Apr 2022 14:59
by highend
You need to learn to apply logic to things...

Code: Select all

    $base    = gpc(<curitem>, "base");
    $newBase = "";
    while ($pos++ < strlen($base)) {
        $char = gettoken($base, $pos, "");
        $next = gettoken($base, $pos + 1, "");
        if ($char == "." && !regexmatches($next, "[0-9]")) { $char = " "; }
        $newBase .= $char;
    }
    $newName = $newBase . "." . gpc(<curitem>, "ext");
    text $newName;
and this is the pro code:

Code: Select all

    text regexreplace(gpc(<curitem>, "base"), "\.(?![0-9])", " ");

Re: Rename only part of a file on certain conditions

Posted: 08 Apr 2022 15:39
by chumbo
Thx, but...it doesn't seem to be working? Pls tell me if I'm doing something wrong here....
- I selected the files I wanted to run the script on
- I copy/pasted your first code (non-pro ;)) in the 'Run Script..." window and clicked OK...nothing happened?
You need to learn to apply logic to things...
Believe me, it has nothing to do with logic at this point for me. I look at the script you wrote and it doesn't make much sense to me. I just don't understand the language and syntax well enough for now. I never could have come up with that, no way!

So, does XY's scripting language have a name? Just wondering if there are any IDEs out there that implement it so I could start trying things out in a more 'friendly' environment.

Re: Rename only part of a file on certain conditions

Posted: 08 Apr 2022 15:52
by highend
I'm just not clear on how I would apply the conditionals to avoid renaming between the integers
I was only showing how building a new (base) name can be done. The script is neither written for multiple files / folders nor does it contain any rename command at all^^
OK...nothing happened?
I highly doubt that, it outputs {something} via text
So, does XY's scripting language have a name?
Maybe: XY's scripting language^^
Just wondering if there are any IDEs out
A text editor of your choice. There are syntax highlighter schemes available for iirc AkelPad and Notepad++ somewhere on the forum

Re: Rename only part of a file on certain conditions

Posted: 08 Apr 2022 16:06
by chumbo
I was only showing how building a new (base) name can be done. The script is neither written for multiple files / folders nor does it contain any rename command at all^^
Oh? Ok, well, if some kind soul could write it, if not too much trouble. Thx.

Re: Rename only part of a file on certain conditions

Posted: 23 Apr 2022 02:02
by kiwichick
chumbo wrote: 08 Apr 2022 16:06
I was only showing how building a new (base) name can be done. The script is neither written for multiple files / folders nor does it contain any rename command at all^^
Oh? Ok, well, if some kind soul could write it, if not too much trouble. Thx.
I'm so glad I found this. I'd like some help with it, too. I can get highend's script working to rename a single file but to rename multiple files I'm guessing the code will have to go in a loop and I can't, for the life of me, figure it out :veryconfused: Loops are the bane of my existence :lol: I always have trouble with them. Hopefully someone will give us a hand.

Re: Rename only part of a file on certain conditions

Posted: 23 Apr 2022 20:28
by Norn
rename
View the Help file.
text $newName; -> rename l, $newName;

Code: Select all

    $base    = gpc(<curitem>, "base"); //this.is.a.file.v1.2.3.78.txt -> this.is.a.file.v1.2.3.78
    $newBase = "";
    while ($pos++ < strlen($base)) { //Loop
        $char = gettoken($base, $pos, ""); //this.i.txt -> if $char = "." ↓
        $next = gettoken($base, $pos + 1, ""); //this.i.txt -> $next = "i"
        if ($char == "." && !regexmatches($next, "[0-9]")) { $char = " "; } //this.i.2.txt -> if $char = "." $next = "i"    if $next != 0-9 $char = " "
        $newBase .= $char; //↓
		text $newBase;
    }
    $newName = $newBase . "." . gpc(<curitem>, "ext"); //new basename + ext = new filename
    rename l, $newName; //Rename  //View the Help file

Code: Select all

foreach($Item, get("selecteditemsnames"), "<crlf>") {
    text $Item;                      
}

Re: Rename only part of a file on certain conditions

Posted: 24 Apr 2022 01:56
by kiwichick
Norn wrote: 23 Apr 2022 20:28 rename
View the Help file.
text $newName; -> rename l, $newName;

Code: Select all

    $base    = gpc(<curitem>, "base"); //this.is.a.file.v1.2.3.78.txt -> this.is.a.file.v1.2.3.78
    $newBase = "";
    while ($pos++ < strlen($base)) { //Loop
        $char = gettoken($base, $pos, ""); //this.i.txt -> if $char = "." ↓
        $next = gettoken($base, $pos + 1, ""); //this.i.txt -> $next = "i"
        if ($char == "." && !regexmatches($next, "[0-9]")) { $char = " "; } //this.i.2.txt -> if $char = "." $next = "i"    if $next != 0-9 $char = " "
        $newBase .= $char; //↓
		text $newBase;
    }
    $newName = $newBase . "." . gpc(<curitem>, "ext"); //new basename + ext = new filename
    rename l, $newName; //Rename  //View the Help file
Yes, I got that working just as you've done here by replacing text with rename. But it only works for one file....

Code: Select all

foreach($Item, get("selecteditemsnames"), "<crlf>") {
    text $Item;                      
}
... and I can't for the life of me figure out how to incorporate it with the loop. I just can't seem to get my head around loops and what parts go in the loop or what parts need to be changed because they're going in the loop. Been at it for over an hour now and I still can't get it.

Re: Rename only part of a file on certain conditions

Posted: 24 Apr 2022 03:55
by RalphM
Use that foreach as stated and put all of highend's code between the curly brackets (instead of "text...").
Then change <curitem> in that part to $Item and it should work.

Re: Rename only part of a file on certain conditions

Posted: 24 Apr 2022 09:44
by kiwichick
RalphM wrote: 24 Apr 2022 03:55 Use that foreach as stated and put all of highend's code between the curly brackets (instead of "text...").
Then change <curitem> in that part to $Item and it should work.
Thanks, Yes well that's what I thought, too and I had tried that before I posted. I replaced highend's [text $newName] with norn's [rename l, $newName;] and changed [<curitem>] to [$item] but it didn't work. Screenshots show (1) the code I used, (2) the files before rename and (3) the files after rename.

1. code.png
2. before rename.png
3. after rename.png

I thought get("selecteditemsnames")] might need to be changed to [get("selecteditemspathnames")] so I tried that too but got the same result.

Re: Rename only part of a file on certain conditions

Posted: 24 Apr 2022 09:50
by highend
Variable names are case-sensitive^^

Re: Rename only part of a file on certain conditions

Posted: 24 Apr 2022 10:01
by kiwichick
highend wrote: 24 Apr 2022 09:50 Variable names are case-sensitive^^
Yes you are correct, I forgot about that even though changing the variable case still doesn't make it work.

result after changing case:
2022-04-24_195856.png

Re: Rename only part of a file on certain conditions

Posted: 24 Apr 2022 10:10
by highend
Ofc it doesn't work.

Do you think that feeding the full list of selected items as source into EACH LOOP does make any sense? No, it doesn't.

Use renameitem() with the correct variables^^