Rename script

Discuss and share scripts and script files...
gialandra
Posts: 28
Joined: 03 Feb 2019 14:12

Rename script

Post by gialandra »

Hi, I am a novice and I have a folder with 2000-2500 file and I want rename them with this method:

1. select only the files which name begin with the zero digit (01218.png,054889.png,06566.png and so on)
2. sort them by name
3. rename them with the zero digit as first character name and then a 4digits number sequence (00001.png, 000002.png and so on)

Then I have to make the same thing with file which name begin with the one digit and then with two digit

Is it possible?

gialandra
Posts: 28
Joined: 03 Feb 2019 14:12

Re: Rename script

Post by gialandra »

I'm serching for... probably is enough a RegExp rename using this: (.{1})(.+)(\..+) > $1$3 (keep first character) plus <#0000> (number sequence) but I can't find the right syntax to joint this espressions. Who can joint them?

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

Re: Rename script

Post by highend »

There is no count something up in a batch regex rename.

Something like this should work:

Code: Select all

    $files = listfolder(, , 1+4, <crlf>);
    $i = 0;
    while ($i < 10) {
        $selection = regexmatches($files, "^$i.*?(?=\r?\n|$)", <crlf>);
        if ($selection) {
            $j = 1;
            foreach($item, $selection, <crlf>, "e") {
                $number = format($j, "0000");
                renameitem($i . $number, $item, 1);
                $j++;
            }
        }
        $i++;
    }
One of my scripts helped you out? Please donate via Paypal

gialandra
Posts: 28
Joined: 03 Feb 2019 14:12

Re: Rename script

Post by gialandra »

Wow, it work perfectly.

Is it possible add the launch of a batch file (C:\Users\MN\Downloads\4L\Elab\1Stitching.cmd) in your code?

Thank you for your help!

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

Re: Rename script

Post by highend »

Look at run or runret() in the script command section of the help file
One of my scripts helped you out? Please donate via Paypal

gialandra
Posts: 28
Joined: 03 Feb 2019 14:12

Re: Rename script

Post by gialandra »

I found the run help and I add the line: run "C:\Users\MN\Downloads\4L\Elab\1Stitching.cmd"; to the end of your code but now seems that the engine script sees two script... but it is not so important, I can lauch manually

Thank you again!

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

Re: Rename script

Post by highend »

but now seems that the engine script sees two script...
And that looks like?

Sounds like a missing indentation for that new line...
One of my scripts helped you out? Please donate via Paypal

gialandra
Posts: 28
Joined: 03 Feb 2019 14:12

Re: Rename script

Post by gialandra »

This is my proof (I'm not a programmer):

Code: Select all

    $files = listfolder(, , 1+4, <crlf>);
    $i = 0;
    while ($i < 10) {
        $selection = regexmatches($files, "^$i.*?(?=\r?\n|$)", <crlf>);
        if ($selection) {
            $j = 1;
            foreach($item, $selection, <crlf>, "e") {
                $number = format($j, "0000");
                renameitem($i . $number, $item, 1);
                $j++;
            }
        }
        $i++;
   }
run "C:\Users\MN\Downloads\4L\Elab\1Stitching.cmd";
Last edited by gialandra on 04 Feb 2019 00:20, edited 1 time in total.

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

Re: Rename script

Post by highend »

Last line in my last posting...
One of my scripts helped you out? Please donate via Paypal

gialandra
Posts: 28
Joined: 03 Feb 2019 14:12

Re: Rename script

Post by gialandra »

You are right, indent was the problem. Now it works :)

Last thing: is it possible lock the script work to a fixed folder? I want to run the script from any folder and it should to work always on a fixed folder
Last edited by gialandra on 04 Feb 2019 10:15, edited 1 time in total.

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

Re: Rename script

Post by highend »

Look up the listfolder() command and its very first argument.
One of my scripts helped you out? Please donate via Paypal

gialandra
Posts: 28
Joined: 03 Feb 2019 14:12

Re: Rename script

Post by gialandra »

From what I understand, but I'm walking in darkness, to reduce the work of the script to only one folder, I have tried to put the path of the folder in your code, but it does not work and i have not found other advice on how to put this path:

$files = listfolder(<C:\Users\5314410\Downloads\CANC\>,, 1+4, <crlf>);

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

Re: Rename script

Post by highend »

Just put it in single or double quotes.

Code: Select all

$files = listfolder("C:\Users\5314410\Downloads\CANC\",, 1+4, <crlf>);
One of my scripts helped you out? Please donate via Paypal

gialandra
Posts: 28
Joined: 03 Feb 2019 14:12

Re: Rename script

Post by gialandra »

Thanks. Now the syntax is OK, but the result is not what I expect (look at the screenshot): if I launch the script from "C:\Users\MN\Downloads\2\" folder, it doesn't work on files in "C:\Users\MN\Downloads\1\" folder
Attachments
2019-02-04_161839.png
2019-02-04_161839.png (18.03 KiB) Viewed 3761 times

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

Re: Rename script

Post by highend »

Yeah, you'd need to add the path for the renameitem() command as well. You should do it like this:

Add a line at the top:

Code: Select all

$folder = "C:\Users\MN\Downloads\1\";
Make sure the path ends with a backslash!

Change the $files line to this afterwards:

Code: Select all

$files = listfolder($folder, , 1+4, <crlf>);
Change the renamitem() line to this:

Code: Select all

renameitem($i . $number, $folder . $item, 1);
One of my scripts helped you out? Please donate via Paypal

Post Reply