Page 1 of 1
Script to jump to another folder of similar name
Posted: 20 Apr 2016 20:31
by roboso
Suppose I have two folders c:/blah/dirAa/bleh/ and c:/blah/dirBb/bleh. Would anyone know how to write a script that, in case I have a Dual Pane view and the first folder open on the front pane, open the second folder on the other pane?
So,
1. Grab the string that names the current folder in the front pane.
2. Replace, e.g., 'Aa' by 'Bb'.
3. Open the folder defined by the new name in the other pane.
Thanks in advance for any help.
Re: Script to jump to another folder of similar name
Posted: 20 Apr 2016 21:14
by admin
Just a note to whoever comes up with a script. This would make a nice use case for the still under cover "DUAL" prefix introduced in v12.50.0005 - 2013-06-19 14:21:
Re: Script to jump to another folder of similar name
Posted: 20 Apr 2016 22:31
by highend
Is the part that is different (the complete folder name) always at the same position?
c:/blah/dirAa/bleh/
-> Second part from the right
Give some real world examples for the original and the similar name...
Re: Script to jump to another folder of similar name
Posted: 20 Apr 2016 23:20
by roboso
Yes, suppose the part that is different starts always at the same position, so you can say that (in pseudo-code)
dir1 = commonstring1 + diffstr1 + commonstring2
dir2 = commonstring1 + diffstr2 + commonstring2
The example I gave is pretty close to a real example:
dir1 = c:/blah/dirAa/bleh/
dir2 = c:/blah/dirBb/bleh/
with
commonstring1 = "c:/blah/dir"
commonstring2 = "/bleh/"
diffstr1 = 'Aa'
diffstr2 = 'Bb'
Re: Script to jump to another folder of similar name
Posted: 21 Apr 2016 00:07
by roboso
Just one more point. It would be nice if the special case of commonstring1 being empty were supported. This is the case in which you have root+tree in the two folders, with the same tree but different roots. It could be used to go the same tree in a different disk drive or volume, for instance.
Re: Script to jump to another folder of similar name
Posted: 21 Apr 2016 08:28
by highend
Ok, two more questions:
1. Does there only ever exist one matching folder for diffstr1?
E.g.:
dirAa
dirBb
or is it possible that there exist more folders with possible matches in the same folder like
dirCc, etc.?
2. Does the diffstr1 always start with the common part "dir" (all lower letters)?
Re: Script to jump to another folder of similar name
Posted: 21 Apr 2016 15:59
by roboso
1. Maybe not. I would expect that the script would require me to input the "to be replaced" (diffstr1, e.g., "Aa") and "replacement" (diffstr2, e.g., "Bb") parts of the path, so that, if there is a "dirCc", it would be ignored. Of course, I would have to make sure the part to be replaced is unique in the path, so I couldn't have "c:\dirAa\folderAa", unless I wanted to replace both "Aa" segments.
2. Yes for my current applications. Looking at the full path as a single string with a part (or subset) to be replaced by another, I was hoping that it wouldn't matter in the script if the part starts at a sub-directory or not. If it matters, I could always type the common part in the sub-directory name. For instance, I would make diffstr1 = "dirAa" and diffstr2 = "dirBb".
Re: Script to jump to another folder of similar name
Posted: 21 Apr 2016 21:32
by highend
Try this...
It should open the correct folder if it exists on the same drive (common basepath) and if it's the only one. Otherwise it will ask which to open.
If no folder is found on the same drive it asks to search all other drives. Again, it opens automatically if there is only one matching, otherwise it'll ask...
Code: Select all
$prefix = "dir";
$drives = get("drives");
$curDrive = gpc(<curpath>, "drive") . ":\";
$subPath = gpc(<curpath>, "component", -1);
$diffPath = gpc(<curpath>, "component", -2);
$commonPath = gpc(<curpath>, "component", 2);
$allDrives = formatlist($drives, "f", , "!$curDrive");
// Return similar folders for the current drive first
$matchingFolders = FindSimilarFolders($curDrive, $commonPath, $diffPath, $subPath, $prefix);
if ($matchingFolders) { ChooseAndOpen($matchingFolders); }
else {
// Try it for other drives...
$message = "No similar folders found on the current drive,<br>search on all drives instead?";
if (confirm($message, , , 4)) { $matchingFolders = FindSimilarFolders($allDrives, $commonPath, $diffPath, $subPath, $prefix); }
if ($matchingFolders) { ChooseAndOpen($matchingFolders); }
}
end 1==1;
Function FindSimilarFolders($drives, $commonStr, $diffStr, $subStr, $prefix) {
$matches = "";
foreach($drive, $drives, , "e") {
if (exists("$drive$commonStr") != 2) { continue; }
$folders = quicksearch("/nd", "$drive$commonStr");
$simFolders = formatlist(regexmatches($folders, "^.*\\$prefix[A-Z].*?(?=$)", , 1), "f", , "!*$diffStr*");
foreach($folder, $simFolders, , "e") {
if (exists("$folder\$subStr") == 2) { $matches = $matches . "$folder\$subStr|"; }
}
}
return trim($matches, "|", "R");
}
Function ChooseAndOpen($folders) {
$count = gettoken($folders, "count", "|");
if ($count == 1) { $folder = gettoken($folders, 1, "|"); }
elseif ($count > 1) {
$folder = inputselect("Choose the folder to open...", $folders, , 1+32+64);
}
else { status "No similar folder found, aborted!", "8B4513", "stop"; end 1==1; }
focus "PI";
tab("new", $folder);
}