How to search for files and only show first match?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
zBernie
Posts: 179
Joined: 08 Dec 2016 17:20

How to search for files and only show first match?

Post by zBernie »

Is it possible to search through many files and only show the first match? For example, I have photos which begin with movie_firsts. So given the files below, would it be possible to return only movie_firsts_oneA and movie_firsts_oneB?


movie_firsts_oneA
movie_firsts_twoA
movie_firsts_threeA
movie_firsts_oneB
movie_firsts_twoB
movie_firsts_threeB

jupe
Posts: 2794
Joined: 20 Oct 2017 21:14
Location: Win10 22H2 120dpi

Re: How to search for files and only show first match?

Post by jupe »

Not sure I understand exactly what you want to do, because if you search for just "movie_firsts_one" it should only find those 2 you say you want to find, but anyway you can limit the returns to 1 hit just add

/limit=n Limit the number of search results.

after the search string e.g
movie_firsts_one /limit=1

you can also use this if it helps:

/limitperdir=n Limit the number of search results per directory.

you can paste the following into the XY address bar for more information if you are interested:

Code: Select all

::help "idh_quicknamesearch.htm";

zBernie
Posts: 179
Joined: 08 Dec 2016 17:20

Re: How to search for files and only show first match?

Post by zBernie »

jupe wrote:Not sure I understand exactly what you want to do, because if you search for just "movie_firsts_one" it should only find those 2 you say you want to find, but anyway you can limit the returns to 1 hit just add

/limit=n Limit the number of search results.

after the search string e.g
movie_firsts_one /limit=1

you can also use this if it helps:

/limitperdir=n Limit the number of search results per directory.

you can paste the following into the XY address bar for more information if you are interested:

Code: Select all

::help "idh_quicknamesearch.htm";

Thank you very much, this ALMOST does what I want. But I forgot, some directories will have several files that begin with 'movie_firsts", which would not be displayed when limiting output.

I wrote the python script below, and in conjunction with the piped linux commands, does what I want. I was trying to emulate this behavior in XYplorer. Can you think of any ways to emulate the sed, sort ,and uniq commands below, in XYplorer?

MovieFirsts.py -f -d /mnt/ds418 | sed 's/...$//' | sort | uniq

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

Re: How to search for files and only show first match?

Post by highend »

And MovieFirsts.py contains... what?

Give a real world example with enough input data and the expected output (and expect XY to show a paperfolder afterwards)...
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 179
Joined: 08 Dec 2016 17:20

Re: How to search for files and only show first match?

Post by zBernie »

highend wrote:And MovieFirsts.py contains... what?

Give a real world example with enough input data and the expected output (and expect XY to show a paperfolder afterwards)...

Okay, here is an example using files prefixed with "bernie3_first". These are "firsts" for my kid. If I search for bernie3_first in the 2009 directory using XYplorer, I get the list shown below. Then running my python against the 2009 directory, reduces it to a single match for each bernie3_first found, for files only. Essentially I'm trying to reduce the number of "bernie3_first" matches to a single match of that pattern, to increase readability.

XYplorer:
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Birthday
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Eagles_Game
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Halloween
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Birthday\bernie3_first_birthday-01.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Birthday\bernie3_first_birthday-02.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Birthday\bernie3_first_birthday-03.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Birthday\bernie3_first_birthday-04.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Birthday\bernie3_first_birthday-05.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Birthday\bernie3_first_birthday-06.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Birthday\bernie3_first_birthday-07.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\bernie3_first_day_clapping.jpg
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\bernie3_first_day_clapping.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Eagles_Game\bernie3_first_eagles_game_falls_down.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Halloween\bernie3_first_halloween-01.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Halloween\bernie3_first_halloween-02.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\Bernie3_First_Halloween\bernie3_first_halloween-03.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\bernie3_first_plays_toys.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\bernie3_first_smile-041909.jpg
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\bernie3_first_smile-041909.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\bernie3_first_typing-070509.mov
Z:\Family_Media\2009\Bernie3\Bernie3_First_Year_Home-03162009\bernie3_first_typing-070509.txt

My python script without parsing. Removes extensions:

Bernie3Firsts.py -f -d /mnt/ds418/Barton_Family_Media/Family_Media/2009
bernie3_first_typing-070509
bernie3_first_day_clapping
bernie3_first_typing-070509
bernie3_first_smile-041909
bernie3_first_day_clapping
bernie3_first_plays_toys
bernie3_first_smile-041909
bernie3_first_eagles_game_falls_down
bernie3_first_birthday-02
bernie3_first_birthday-01
bernie3_first_birthday-05
bernie3_first_birthday-03
bernie3_first_birthday-04
bernie3_first_birthday-06
bernie3_first_birthday-07
bernie3_first_halloween-02
bernie3_first_halloween-01
bernie3_first_halloween-03


My python script with sed, sort, and uniq:

Bernie3Firsts.py -f -d /mnt/ds418/Barton_Family_Media/Family_Media/2009 | sed 's/...$//' | sort | uniq
bernie3_first_birthday
bernie3_first_day_clapp
bernie3_first_eagles_game_falls_d
bernie3_first_halloween
bernie3_first_plays_t
bernie3_first_smile-041
bernie3_first_typing-070

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

Re: How to search for files and only show first match?

Post by highend »

First you need to tweak your XYplorer.ini file (https://www.xyplorer.com/faq-topic.php?id=tweak):
PFAllowZombies=1

Then you can use a script (start it when you are inside the search path, e.g. "Z:\Family_Media\2009"):

Code: Select all

    $pattern = input("Enter the part of the file name to search for", , "bernie3_first");
    if !($pattern) { status "No search term entered, aborted!", "8B4513", "stop"; end true; }

    // Find search term (files only)
    $matches = quicksearch("$pattern /f", <curpath>);
    // Remove file extensions
    $matches = regexreplace($matches, "(\.[^.]+)(?:\r?\n|$)", <crlf>);
    // Remove the last 3 characters
    $matches = regexreplace($matches, "(...)(?:\r?\n|$)", <crlf>);
    // Sort and remove duplicates
    $matches = formatlist($matches, "sed", <crlf>);
    // Create and show a paperfolder
    paperfolder("search", $matches, , "nl");
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 179
Joined: 08 Dec 2016 17:20

Re: How to search for files and only show first match?

Post by zBernie »

highend wrote:First you need to tweak your XYplorer.ini file (https://www.xyplorer.com/faq-topic.php?id=tweak):
PFAllowZombies=1

Then you can use a script (start it when you are inside the search path, e.g. "Z:\Family_Media\2009"):

Code: Select all

    $pattern = input("Enter the part of the file name to search for", , "bernie3_first");
    if !($pattern) { status "No search term entered, aborted!", "8B4513", "stop"; end true; }

    // Find search term (files only)
    $matches = quicksearch("$pattern /f", <curpath>);
    // Remove file extensions
    $matches = regexreplace($matches, "(\.[^.]+)(?:\r?\n|$)", <crlf>);
    // Remove the last 3 characters
    $matches = regexreplace($matches, "(...)(?:\r?\n|$)", <crlf>);
    // Sort and remove duplicates
    $matches = formatlist($matches, "sed", <crlf>);
    // Create and show a paperfolder
    paperfolder("search", $matches, , "nl");

Thank you very much, that precisely duplicates the functionality I showed you using the python script and linux commands! It's also good to know how to perform this type of functionality in an XYplorer script. Oh, and I made a donation, have a drink on me!

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

Re: How to search for files and only show first match?

Post by highend »

No problem and thanks for the donation :beer:
One of my scripts helped you out? Please donate via Paypal

zBernie
Posts: 179
Joined: 08 Dec 2016 17:20

Re: How to search for files and only show first match?

Post by zBernie »

highend wrote:No problem and thanks for the donation :beer:
You are welcome.

FYI, I noticed that some of the files contained trailing hyphens and or digits, so I added the last two "$matches =" lines (Not your fault). Works exactly as desired now!

$pattern = input("Enter the part of the file name to search for", , "bernie3_first | bernie3_second");
if !($pattern) { status "No search term entered, aborted!", "8B4513", "stop"; end true; }
// Find search term (files only). Add /f after pattern to search files not directories.
$matches = quicksearch("$pattern /f", <curpath>);
// Remove file extensions
$matches = regexreplace($matches, "(\.[^.]+)(?:\r?\n|$)", <crlf>);
// Remove the last 3 characters
//$matches = regexreplace($matches, "(...)(?:\r?\n|$)", <crlf>);
// Remove last n digits
$matches = regexreplace($matches, "(\d*)(?:\r?\n|$)", <crlf>);
// Remove trailing hyphen
$matches = regexreplace($matches, "(-)(?:\r?\n|$)", <crlf>);
// Sort and remove duplicates
$matches = formatlist($matches, "sed", <crlf>);
// Create and show a paperfolder
paperfolder("search", $matches, , "nl");

Post Reply