Need help on script to synchronize filenames

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Need help on script to synchronize filenames

Post by Papoulka »

I have folders open in both panes. They both have the same files - all .txt - but in the P1 folder some names have been corrected. I want to make the P2 filenames mirror those in P1. I thought it would be straightforward but after much trying I need some help.

Here's one of several methods I've tried:

Code: Select all

focus "P1";
   $newnames = listfolder(, , 1+4); //A.txt|B.txt|E.txt
   focus "P2";
   $oldnames = listfolder(, , 1+4); //A.txt|C.txt|D.txt
   rename b, $newnames, p, $oldnames;
This fails because the "rename b" command does not recognize CR or | in the $newnames string. It tries to rename each target file with the entire $newname string. The only relevant example in the Help refers to <clipboard>. But if I first copy the new filenames to the clipboard and then use "rename b, <clipboard>", the same thing happens.

EDIT: I think now that "rename batch" is not supposed to work as I had hoped above(!).... So that leaves me wondering how best to do this job. I tried the various Synch commands and options earlier without success. The only thing I can think of is parsing and stepping through $newnames and $oldnames in synch, and using renameitem on one file at a time.

Thanks for any guidance

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

Re: Need help on script to synchronize filenames

Post by highend »

Imho the only reliable option is to compare the file hashes and rename accordingly. And that does only work if all files are really identical apart from their file name. How else should XY know which file was renamed to what.

E.g.:
C.txt could now be B.txt OR E.txt
and the same goes for D.txt

The only other option would be an access to XY's action log but even if you save it (savesettings = 128), you'd need to parse binary data...
One of my scripts helped you out? Please donate via Paypal

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Re: Need help on script to synchronize filenames

Post by Papoulka »

I should have mentioned: in my only use cases I know for certain that (a) the number of files in both panes are the same and (b) they are in correct order in both panes. I only want to take all the filenames in Pane 1 and apply them to the files in Pane 2.

Under these conditions I could safely proceed manually by copying the P1 filenames to the clipboard, then moving to P2 and using File | Rename Special | Edit Item Names... delete what's there, paste in the new, and apply them. But that can't be automated (I spent a long time trying to) and I have hundreds of folders to process. So I was trying to find another way to script it.

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

Re: Need help on script to synchronize filenames

Post by highend »

Ofc that can be automated. Albeit not with XY alone but you need e.g. AHK / AutoIt. The xys script copies all new names into the clipboard, the .ahk script uses all these names and automates the dialog window...

Not that hard...

Code: Select all

 sel "a";
 copytext "<get SelectedItemsNames <crlf>>";
 focus "PI";
 sel "a";
 run """D:\Tools\AutoHotkey\@Scripts\@Test\test1.exe""", "<curpath>";
 #147;
D:\Tools\AutoHotkey\@Scripts\@Test\test1.exe:

Code: Select all

#NoEnv
#SingleInstance, Force
#NoTrayIcon

WinWaitActive, ahk_exe XYplorer.exe, , 5
{
	SendInput ^a
	SendInput ^v
	Sleep, 50
	SetControlDelay -1
	ControlClick, OK, A
	Sleep, 50
	SendInput {Enter}
}
ExitApp
One of my scripts helped you out? Please donate via Paypal

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

Re: Need help on script to synchronize filenames

Post by RalphM »

I don't know whether this could be scripted but the easiest manual way would be to:
Select all the files in pane 1
Files / Rename Special / Edit Item Names...
Ctrl+A, Ctrl+C
Cancel
Go to pane 2
Select all the files there
Files / Rename Special / Edit Item Names...
Ctrl+A, Ctrl+V
OK
Ralph :)
(OS: W11 25H2 Home x64 - XY: Current x64 beta - Office 2024 64-bit - Display: 1920x1080 @ 125%)

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Re: Need help on script to synchronize filenames

Post by Papoulka »

Thank you highend. That will let me proceed with my project now. Enjoy the :beer: in your Paypal acct.

RalphM - your suggestion is in fact what highend coded. Command #147 is "File | Rename Special | Edit Item Names", and the AutoHotkey script pastes in the data via Ctl-V. Unfortunately that approach can't be automated within XY (which is what I meant by my remark). I may yet try a "step through the strings" script for future, more general use.

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

Re: Need help on script to synchronize filenames

Post by highend »

Enjoy the :beer: in your Paypal acct
Thanks! :whistle: :ghost:
One of my scripts helped you out? Please donate via Paypal

bdeshi
Posts: 4256
Joined: 12 Mar 2014 17:27
Location: Asteroid B-612
Contact:

Re: Need help on script to synchronize filenames

Post by bdeshi »

here's a possible xys only solution:

Code: Select all

//rename inactive pane files to match active pane file names.
//assuming same sort order and no extra items in either pane.
  $listA = listpane('a', , 1+4);
  $listI = listpane('i', , 1+4);
  $pathA = get('path','a').'\';
  $pathI = get('path','i').'\';

  $c = 0; $tmp = $pathI;
  while exists($tmp.$c) {$c++;}
  $tmp = new($tmp.$c, 'dir');
  setting 'UseCustomCopy', 0; setting 'BackgroundFileOps', 0;
  $c = 1;

  foreach ($itm, $listI, '|') {
    moveas gettoken($listA, $c, '|'), $tmp, $pathI.$itm;
    $c++;
  }
  moveto gpc($tmp,'path'), listfolder($tmp);
  if (listfolder($tmp) == '') { delete 0, 0, $tmp; }
pretty late to the party, but I found this at the bottom of my unread list and had nothing better to do! :ghost:
Icon Names | Onyx | Undocumented Commands | xypcre
[ this user is asleep ]

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Re: Need help on script to synchronize filenames

Post by Papoulka »

Thank you Sammay. Your script works and is instructive to go through.

My first thoughts were to form a list of the Active pane filenames and apply them one at a time to the Inactive pane files via RenameItem or similar. That's more direct and possibly faster than "moving" the files. But it relies on me knowing that none of the existing Active filenames would collide with an existing Inactive filename. Your method avoids that possibility and so is more robust. On a test with an external drive and thirty 50MB files it takes about 2 sec to rename the Inactive files. That's fast enough, even when doing many folders one after another.

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

Re: Need help on script to synchronize filenames

Post by highend »

If listfolder would work as well...

Code: Select all

    $filesA = listfolder("<curpath>", , 1+4);
    rename "b", '*_<datem yyyymmdd_hhnnss>', , listpane("i", , 1);
    $filesI = listfolder(get("Path", "i"), , 1);
    while ($i++ < gettoken($filesA, "count", "|")) {
        renameitem(gettoken($filesA, $i, "|"), gettoken($filesI, $i, "|"));
    }
I needed a break from YAUM^^
One of my scripts helped you out? Please donate via Paypal

Papoulka
Posts: 455
Joined: 13 Jul 2013 23:41

Re: Need help on script to synchronize filenames

Post by Papoulka »

Thanks, highend. That's even faster (in one test), cleaner on the screen, and also avoids collisions. I'll switch to this.

BTW - typo in your new tag line (which somehow changed everywhere...?!): "or" should be "to".

Post Reply