Page 1 of 1
Need help on script to synchronize filenames
Posted: 29 Aug 2015 22:47
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
Re: Need help on script to synchronize filenames
Posted: 30 Aug 2015 05:37
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...
Re: Need help on script to synchronize filenames
Posted: 30 Aug 2015 06:08
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.
Re: Need help on script to synchronize filenames
Posted: 30 Aug 2015 06:32
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
Re: Need help on script to synchronize filenames
Posted: 30 Aug 2015 13:46
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
Re: Need help on script to synchronize filenames
Posted: 31 Aug 2015 19:06
by Papoulka
Thank you highend. That will let me proceed with my project now. Enjoy the

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.
Re: Need help on script to synchronize filenames
Posted: 31 Aug 2015 22:14
by highend
Enjoy the

in your Paypal acct
Thanks!

Re: Need help on script to synchronize filenames
Posted: 03 Sep 2015 14:35
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!

Re: Need help on script to synchronize filenames
Posted: 03 Sep 2015 16:23
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.
Re: Need help on script to synchronize filenames
Posted: 04 Sep 2015 11:02
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^^
Re: Need help on script to synchronize filenames
Posted: 04 Sep 2015 17:21
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".