Script to find missing file pairs

Discuss and share scripts and script files...
Post Reply
zer0
Posts: 2673
Joined: 19 Jan 2009 20:11

Script to find missing file pairs

Post by zer0 »

I don't often need a script, but when I do...I come here :lol:

I am after a script that would loop through all files in a given location (including child and grandchild folders) and pick out files that have at least one match from a given list of strings. For all such matches, I want it to show me a list of only those -- just base names will do -- which do not have a corresponding file of a specific file type. For example, for any file that has "dog" in its file name (such as "this is is my dog.jpg), I only want to see those files which do not have a corresponding text file (for that JPEG, it would be exactly "this is my dog.txt").

My flow for how such a script would work is this:

1. Get a list of all file names from the given location (assume current, unless specified otherwise) and save in a variable.
2. Loop through the complete list and search for matches of specific words. For every match, store that name in another variable.
3. Loop through the refined list and find if the corresponding file type exists. If it does not, store that name in the 3rd variable.
4. Show the final list derived from what is created in the 3rd variable.

Is there any better way to do this? If it is a piece of cake, can anyone provide skeleton code for me to tweak? Thank you in advance :tup:
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

highend
Posts: 13309
Joined: 06 Feb 2011 00:33

Re: Script to find missing file pairs

Post by highend »

$find = "|"-separated list of matching words. Plain english names here? No need to escape this list

What is different to your approach:
- Does not need a loop over all existing files but only one for the ones that match your word list
-> Will save you thousands of (slow!) loops depending on how many files you have in your (sub)folder(s)
- Filters out matches in the recycle.bin

Edit: Optimized the foreach loop a bit more

Code: Select all

    $find   = "check|cat";
    $dstExt = ".txt";

    $files      = formatlist(quicksearch("/f"), "f", "<crlf>", "!recycle.bin");
    $matches    = formatlist(regexmatches($files, "^.*\\.*?($find).*?(?=$)", "<crlf>"), "f", "<crlf>", "!*$dstExt", "f");
    $posMatches = formatlist($files, "f", "<crlf>", "*$dstExt", "f");

    $list = "";
    foreach($match, $matches, "<crlf>") {
        if !(formatlist($posMatches, "f", "<crlf>", getpathcomponent($match, "base") . $dstExt, "f")) {
            $list = $list . getpathcomponent($match, "file") . "<crlf>";
        }
    }
    text "These files do not have an equivalent $dstExt file:<crlf 2>$list";

One of my scripts helped you out? Please donate via Paypal

highend
Posts: 13309
Joined: 06 Feb 2011 00:33

Re: Script to find missing file pairs

Post by highend »

A slightly different one using only formatlist() instead of regexmatches().
A bit easier to read but the setup for $find requires a different format than in the first version.

Code: Select all

    $find   = "*check*|*cat*";
    $dstExt = ".txt";

    $files      = formatlist(quicksearch("/f", , "|"), "f", , "!recycle.bin");
    $matches    = formatlist(formatlist($files, "f", , "$find", "f"), "f", , "!*$dstExt");
    $posMatches = formatlist($files, "f", , "*$dstExt", "f");

    $list = "";
    foreach($match, $matches) {
        if !(formatlist($posMatches, "f", , getpathcomponent($match, "base") . $dstExt, "f")) {
            $list = $list . getpathcomponent($match, "file") . "<crlf>";
        }
    }
    text "These files do not have an equivalent $dstExt file:<crlf 2>$list";
One of my scripts helped you out? Please donate via Paypal

zer0
Posts: 2673
Joined: 19 Jan 2009 20:11

Re: Script to find missing file pairs

Post by zer0 »

My apologies for not thanking you sooner :tup: The bad news: it created a lot of work for me since I found quite a few missing file pairs :lol:
Reporting a bug? Have a wish? Got a question? Use search - View roadmap - FAQs: Forum + XY site
Windows 7/10
Always using the latest stable two-decimal build

Post Reply