Page 1 of 1
File to Folder name Selection
Posted: 17 Jul 2013 02:39
by CookieMonster
I'm looking for a script that selects the folder related to the file. For example if a file is labelled;
AcmePaper.<ext> and there is a folder called AcmePaper-Files the names are the same. When I select the file it selects the equal folder of the same name.
Re: File to Folder name Selection
Posted: 17 Jul 2013 08:40
by highend
When I select the file it selects the equal folder of the same name.
Not possible without clicking on a button / using a keyboard shortcut...
Apart from that, a script could look like this:
Code: Select all
end get("CountSelected") > 1, "There is already more than one file selected, aborting!";
$baseName = getpathcomponent(, "base");
foreach ($item, report("{name}|"), "|") {
if $item LikeI $baseName* {
selectitems "$item", , 0, "a";
break;
}
}
Re: File to Folder name Selection
Posted: 17 Jul 2013 17:17
by CookieMonster
highend wrote:When I select the file it selects the equal folder of the same name.
Not possible without clicking on a button / using a keyboard shortcut...
Apart from that, a script could look like this:
Code: Select all
end get("CountSelected") > 1, "There is already more than one file selected, aborting!";
$baseName = getpathcomponent(, "base");
foreach ($item, report("{name}|"), "|") {
if $item LikeI $baseName* {
selectitems "$item", , 0, "a";
break;
}
}
It works exactly how I want, thank you. It only works when I select one file, If I select multiple files to match up with, it doesn't work. Can this functionality be added ?
Re: File to Folder name Selection
Posted: 17 Jul 2013 18:02
by highend
Code: Select all
$list = report("{name}|");
foreach ($file, get("SelectedItemsNames", "|"), "|") {
$baseName = getpathcomponent($file, "base");
foreach ($item, $list, "|") {
if $item LikeI $baseName* {
selectitems "$item", , 0, "a";
break;
}
}
}
Re: File to Folder name Selection
Posted: 17 Jul 2013 18:18
by CookieMonster
The more I get aquated with this program, the more I love it.
Thank you for the help and the script.
Re: File to Folder name Selection
Posted: 17 Jul 2013 20:13
by TheQwerty
Here's an alternate approach because I prefer avoiding loops if possible:
Code: Select all
"Select Matching File Directories"
// Get the number of items selected.
$selCount = Get('CountSelected');
End $selCount < 1, 'At least one item must be selected.';
// This way will use the base name of anything that is selected.
$query = Report('{Basename}-Files|', 1);
// Alternately the following will only use the base name of selected *files*.
// The FormatList is required since we cannot put a literal '|' in {Dir ...}.
// $query = Report('{Dir |{Basename}-Files|}|',1);
// $query = FormatList($query, 'dents', '|');
// Add matching directories to the selection.
SelFilter($query, 'd', 'Name', 1);
// Display a status bar message so we know how many folders were selected.
$selCount = Get('CountSelected') - $selCount;
$y = $selCount == 1 ? 'y' : 'ies';
Status "Selected $selCount director$y.",, 'ready';
Verbosity not required.
Which if you don't want all the fluff can be condensed into a single liner:
Code: Select all
SelFilter(Report('{Basename}-Files|', 1), 'd', 'Name', 1);
Brevity not required.