File to Folder name Selection

Discuss and share scripts and script files...
Post Reply
CookieMonster

File to Folder name Selection

Post 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.

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

Re: File to Folder name Selection

Post 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;
		}
	}
One of my scripts helped you out? Please donate via Paypal

CookieMonster

Re: File to Folder name Selection

Post 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 ?

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

Re: File to Folder name Selection

Post 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;
			}
		}
	}
One of my scripts helped you out? Please donate via Paypal

CookieMonster

Re: File to Folder name Selection

Post by CookieMonster »

The more I get aquated with this program, the more I love it.

Thank you for the help and the script.

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: File to Folder name Selection

Post 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.

Post Reply