Page 1 of 1

Rename script - strange result

Posted: 17 Dec 2016 22:25
by chincherpa
Hi @all,

I wrote a script to rename files, like:

IMG_20160102 120203.jpg -> 2016-01-02 12-02-03.jpg
VID_20160103 120304.mp4 -> 2016-01-03 12-03-04.mp4
Screenshot_20160104 120405.png -> 2016-01-04 12-04-05.png

The problem is, my script wants to give all files the same name.
See:
Image

after clicking "Rename", next windows pops up and all files will be renamed with the next 'newname' from the next file in the list.

What is going on here? :cry:

this is my script:

Code: Select all

foreach($item, <get SelectedItemsPathNames |>) {
    $base   = gpc($item, "base");
    if ($base Like "VID_*")||($base Like "IMG_*") {
        echo "VID, IMG: ".$base;
        $jahr = substr($base, 4, 4);
        $monat = substr($base, 8, 2);
        $tag = substr($base, 10, 2);
        $stunde = substr($base, 13, 2);
        $minute = substr($base, 15, 2);
        $sekunde = substr($base, 17, 2);
    }
    elseif ($base Like "Screenshot_*") {
        echo "Screenshot: ".$base;
        $jahr = substr($base, 11, 4);
        $monat = substr($base, 15, 2);
        $tag = substr($base, 17, 2);
        $stunde = substr($base, 20, 2);
        $minute = substr($base, 22, 2);
        $sekunde = substr($base, 24, 2);
  }
    else {
      echo "Startet nicht mit: VID_ oder IMG_ oder Screenshot_";
    break;
  }
  $newname = $jahr."-".$monat."-".$tag." ".$stunde."-".$minute."-".$sekunde;
    if (regexmatches(substr($newname, 0, 19), "[0-9-/s]{19}") {
        rename, $newname, p;
    }
}
The echo's are for debugging.

Re: Rename script - strange result

Posted: 17 Dec 2016 23:02
by highend

Code: Select all

rename, $newname, p;
Look at the docs and you should immediately see why this doesn't work as expected.

Imho it doesn't make much sense to use a preview for each loop either so I'd use renameitem() instead...

Re: Rename script - strange result

Posted: 17 Dec 2016 23:13
by highend
My script would look like this:

Code: Select all

    foreach($item, <get SelectedItemsNames |>) {
        if !(regexmatches($item, "^(img|vid|screenshot)_")) { continue; }
        $dateOnly = regexreplace($item, "^[a-z]+_");
        $newDate  = regexreplace($dateOnly, "(\d{4})(\d{2})(\d{2}) (\d{2})(\d{2})(\d{2})", "$1-$2-$3 $4-$5-$6");
        renameitem($newDate, $item, 4);
    }

Re: Rename script - strange result

Posted: 17 Dec 2016 23:27
by chincherpa
Didn't see it immediatly, but I saw it.

I'm learning...

Thank you!!

Re: Rename script - strange result

Posted: 11 Dec 2017 12:42
by chincherpa
I don't know why, but it seems not to work anymore.
But why?

Tested with filename: VID_20170102_121314.mp4
Result is: 20170102_121314.mp4
Expected result is: 2017-01-02 12-13-14.mp4

Does somebody know why?

And, is there any documentation out there, where this part of the code is explained?

Code: Select all

"(\d{4})(\d{2})(\d{2}) (\d{2})(\d{2})(\d{2})", "$1-$2-$3 $4-$5-$6")
Well, I have an idea what it means, but is there any documentation?

Re: Rename script - strange result

Posted: 11 Dec 2017 12:48
by highend
Isn't it obvious?

Old file name format:
IMG_20160102 120203.jpg
New file name format:
VID_20170102_121314.mp4
And, is there any documentation out there, where this part of the code is explained?
Any tutorial about regex expressions should explain it...

Code: Select all

"(\d{4})(\d{2})(\d{2}) (\d{2})(\d{2})(\d{2})", "$1-$2-$3 $4-$5-$6")
4 digits
2 digits
2 digits
A SPACE
2 digits
2 digits
2 digits

Reformatted into a date with "-" delimiters

So to catch the old and the new format, e.g. use:

Code: Select all

(\d{4})(\d{2})(\d{2})(?:[ ]|_)?(\d{2})(\d{2})(\d{2})

Re: Rename script - strange result

Posted: 11 Dec 2017 12:57
by chincherpa
Thank you!
I was afraid it might be my fault!

Re: Rename script - strange result

Posted: 11 Dec 2017 12:59
by highend
It is.

You changed the file format without adapting the regex to capture that change :maf: