Script to Change Name Order

Discuss and share scripts and script files...
Post Reply
Pdub
Posts: 3
Joined: 16 Jan 2013 06:28

Script to Change Name Order

Post by Pdub »

Not sure if anyone can help or point me in the right direction for this. I am trying to re-arrange the big art collection called "Oil Paintings of the Western World" in to alphabetical order by LAST name instead of first (which apparantly someone thought would be a good idea). Any way, what I want to know is if it is possible to highlght a directory name (say Bill Smith) and with a key command/script, change it to "Smith, Bill"?

j_c_hallgren
XY Blog Master
Posts: 5826
Joined: 02 Jan 2006 19:34
Location: So. Chatham MA/Clearwater FL
Contact:

Re: Script to Change Name Order

Post by j_c_hallgren »

Hi and welcome to the XY forums!

I'm not a XY scripter so can't help you with actual coding (sorry!) but having done that name flip task years ago in COBOL (don't have that code available anywhere -- wish I did at times as it was quite a challenge in a language not designed for that type of thing), I'd like to just clarify some of the requirements and/or data issues you might have:

1) Do all names just have first and last? That is...two words only? No middle names/initials, right?
2) Do any last names have two parts? Like "Van Dyke", "Von Collin", etc...also Oriental names gave me grief back then...
3) Do any names just have initials instead of first (and last) name? And thus punctuation, like "J. Hallgren" or "J. H."?
4) Do all names need to be flipped? I'm presuming that you want to do this only on one selected file and repeat manually as opposed to doing all items via looping because if you have any exceptions, then how would they be identified?
Still spending WAY TOO much time here! But it's such a pleasure helping XY be a treasure!
(XP on laptop with touchpad and thus NO mouse!) Using latest beta vers when possible.

Borut
Posts: 1472
Joined: 19 Oct 2010 19:29
Location: Win10 Pro 64b 22H2, @120DPI (125%)

Re: Script to Change Name Order

Post by Borut »

Hi Pdub,

well, I would do this with a help of an editor which supports macro recording and playback (notepad++ is one of these and it is free). Like this...

In XYplorer:
- select all directory names
- Rename Special > Edit Item Names...
- Select and copy all Names

In editor:
- Paste names
- Do one line with macro recording on
- Macro recording off
- Replay macro for all lines
- Select and copy all lines

In XYplorer:
- Paste all names
- OK > Check the file names once more > Rename now

In other words, maybe for this task it is not necessary to bother with scripts. BTW, everything j_c_hallgren wrote applies in this case too and may make additional manual editing necessary.
Win 10 Pro 64bit

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: Script to Change Name Order

Post by SkyFrontier »

Code: Select all

   $a = get(selecteditemspathnames);

   foreach($tk, $a, "<crlf>") {

// step;
   $base = formatlist($tk, v, "");
   $base = gettoken ($base, 1, "\");
   $nm1 = gettoken ($base, 1, " ");
   $nm1 = formatlist($nm1, v, "");
   $nm2 = gettoken ($base, 2, " ", , 2);
   $nm2 = formatlist($nm2, v, "");
// step;
   renameitem("$nm1, $nm2", $tk, , "_01");

                              }

   echo "All files renamed!<crlf>Have a nice day...";
*assuming you have *folders only* selected.
No checking on code, no error handling - sorry, I'm in a hurry lately.
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

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

Re: Script to Change Name Order

Post by highend »

The basic thing (swapping the last found "name") to the first place is trivial, but all those execptions...

E.g.:

Code: Select all

renameitem(regexreplace("<curbase>", "(.*)\s(.*)", "$2, $1"), "<curname>");
Takes everything up to (but not including) the last space in a filename and puts if after the last word (part), separated by a comma with a following space.

Ofc it doesn't care if it's a double surname, has a von / van or anything else as a title, etc.
But you could do the editing by hand if the result is not perfect...

Be careful not to have file / folder names where you have a space before your fileextension / after the last word *g*...
One of my scripts helped you out? Please donate via Paypal

Pdub
Posts: 3
Joined: 16 Jan 2013 06:28

Re: Script to Change Name Order

Post by Pdub »

Wow, thank you for the welcome to your User Group and thank you for all the brain power applied to my question!

to j_c_hallgren:
There are 1718 file-folders (I am not messing with the actual file names within the file-folders). Many have just a first and last name but many have 3 or 4 names. I am trying to knock down the bulk by just going after the 'easy' 2 name folders. The rest I am resigned to plough through. Names such as "John Van Dkye" I am for the sake of expediency, changing to "Dkye, John Van." I expect that I will have to do another pass through the group to fix this once I get each painter into the correct alpha category. Same goes for first or middle name initialls.

Borut
Thank you for the reply. I will give your suggestion a shot as it does sound more or less painless. I will let you know how I make out.

SkyFrontier
Wow - I will study and try this also.

highend
Again - you guys have got it goin' on! I will try this too.

Thanks all!

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

Re: Script to Change Name Order

Post by highend »

Ok, if you want to use my version for all selected files & folders, you have to edit it to:

Code: Select all

    foreach($file, <get SelectedItemsPathNames>, "<crlf>") {
        renameitem(regexreplace(getpathcomponent($file, "base"), "(.*)\s(.*)", "$2, $1"), getpathcomponent($file, "file"));
    }
One of my scripts helped you out? Please donate via Paypal

Pdub
Posts: 3
Joined: 16 Jan 2013 06:28

Re: Script to Change Name Order

Post by Pdub »

Hi Highend

I tried your latest version on the file names below and got the following results:

BEFORE
Anders Zorn (1860-1920)
Zamacois y Zabala (1842-1871)
Zandomeneghi (1841-1917)
Zao Wou-Ki
Zuber-Buhler (1822-1896)
Zuloaga y Zabaleta (1870-1945)

AFTER
(1860-1920), Anders Zorn
(1842-1871), Zamacois y Zabala
(1841-1917), Zandomeneghi
Wou-Ki, Zao
(1822-1896), Zuber-Buhler
(1870-1945), Zuloaga y Zabaleta

So it does work fine for my original question - how to rearrange a first and last name. I have successfully run the (1st version of your) script on several hundred names - THANK YOU!!!

The names with accompanying date ranges (lifespan of the named artists) don't work so well so I am plugging away through them one by one.

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

Re: Script to Change Name Order

Post by highend »

Try this:

Code: Select all

    foreach($file, <get SelectedItemsPathNames>, "<crlf>") {
        $dateToken = gettoken(getpathcomponent($file, "base"), -1, " ");
        if(regexmatches($dateToken, "\d{4}", "|") != "") {
            $tmpName = formatlist(replace(getpathcomponent($file, "base"), $dateToken, ""), "t");
            $newName = regexreplace(getpathcomponent($tmpName, "base"), "(.*)\s(.*)", "$2, $1") . " " . $dateToken;
            renameitem($newName, getpathcomponent($file, "file"));
        } else {
            renameitem(regexreplace(getpathcomponent($file, "base"), "(.*)\s(.*)", "$2, $1"), getpathcomponent($file, "file"));
        }
    }
If it finds 4 digits in a row in the last token (separated by spaces) of the basename, it will preserve this token.

You NEED at least Beta v11.90.0209 to use this script!
One of my scripts helped you out? Please donate via Paypal

Post Reply