Rename only part of a file on certain conditions

Discuss and share scripts and script files...
chumbo
Posts: 262
Joined: 04 Jan 2015 15:20

Rename only part of a file on certain conditions

Post 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:

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

Re: Rename only part of a file on certain conditions

Post 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^^
One of my scripts helped you out? Please donate via Paypal

chumbo
Posts: 262
Joined: 04 Jan 2015 15:20

Re: Rename only part of a file on certain conditions

Post 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!

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

Re: Rename only part of a file on certain conditions

Post 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])", " ");
One of my scripts helped you out? Please donate via Paypal

chumbo
Posts: 262
Joined: 04 Jan 2015 15:20

Re: Rename only part of a file on certain conditions

Post 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.

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

Re: Rename only part of a file on certain conditions

Post 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
One of my scripts helped you out? Please donate via Paypal

chumbo
Posts: 262
Joined: 04 Jan 2015 15:20

Re: Rename only part of a file on certain conditions

Post 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.

kiwichick
Posts: 648
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Rename only part of a file on certain conditions

Post 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.
Windows 10 Pro 22H2

Norn
Posts: 479
Joined: 24 Oct 2021 16:10

Re: Rename only part of a file on certain conditions

Post 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;                      
}
Windows 11 24H2 @100% 2560x1440

kiwichick
Posts: 648
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Rename only part of a file on certain conditions

Post 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.
Windows 10 Pro 22H2

RalphM
Posts: 2028
Joined: 27 Jan 2005 23:38
Location: Cairns, Australia

Re: Rename only part of a file on certain conditions

Post 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.
Ralph :)
(OS: W11 24H2 Home x64 - XY: Current x32 beta - Office 2024 32-bit - Display: 1920x1080 @ 125%)

kiwichick
Posts: 648
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Rename only part of a file on certain conditions

Post 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
1. code.png (12.68 KiB) Viewed 3697 times
2. before rename.png
2. before rename.png (3.42 KiB) Viewed 3697 times
3. after rename.png
3. after rename.png (3.71 KiB) Viewed 3697 times

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

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

Re: Rename only part of a file on certain conditions

Post by highend »

Variable names are case-sensitive^^
One of my scripts helped you out? Please donate via Paypal

kiwichick
Posts: 648
Joined: 08 Aug 2012 04:14
Location: Pahiatua, New Zealand

Re: Rename only part of a file on certain conditions

Post 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
2022-04-24_195856.png (2.95 KiB) Viewed 3696 times
Windows 10 Pro 22H2

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

Re: Rename only part of a file on certain conditions

Post 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^^
One of my scripts helped you out? Please donate via Paypal

Post Reply