Search and Replace Text in Files

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
iryx
Posts: 26
Joined: 29 Jan 2007 16:51

Search and Replace Text in Files

Post by iryx »

Is this possible with anything built into the application?

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

Re: Search and Replace Text in Files

Post by highend »

Sure, via scripting, as long as these are real text files (no pdf, word, or anything else...)

E.g.:

Code: Select all

    $safe = <<<>>>
txt
xml
xys
nfo
<extend me if necessary!>
>>>;

    $search = input("Enter search term...", , , "m");
    end (!$search), "Empty search terms are not accepted!";
    $replace = input("Enter text to replace with...", , , "m");

    $notSafe = "";
    $replaced = "";
    $notReplaced = "";
    foreach($item, <get SelectedItemsPathNames |>, , "e") {
        $ext = gpc($item, "ext");
        if (!regexmatches($safe, "^$ext$")) {
            $notSafe = $notSafe . ((!$notSafe) ? "<crlf>Items with invalid file extension(s)<crlf>$item" : "<crlf>$item" );
            continue;
        }
        $oldContent = readfile($item);
        $newContent = replace($oldContent, $search, $replace);
        if ($oldContent != $newContent) {
            writefile($item, $newContent);
            $replaced = $replaced . ((!$replaced) ? "<crlf>Files with replacement(s)<crlf>$item" : "<crlf>$item" );
        } else {
            $notReplaced = $notReplaced . ((!$notReplaced) ? "<crlf>Files without replacement(s)<crlf>$item" : "<crlf>$item" );
        }
    }
    $result = $notSafe . <crlf> . $replaced . <crlf> . $notReplaced;
    text $result;

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

napleswebguy
Posts: 11
Joined: 08 Feb 2020 14:55

Re: Search and Replace Text in Files

Post by napleswebguy »

@highend, this is so handy!

Is it possible to modify your script to just FIND and show results of certain content within multiple text files?

Thanks!

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

Re: Search and Replace Text in Files

Post by highend »

And how should those results been shown exactly?
One of my scripts helped you out? Please donate via Paypal

napleswebguy
Posts: 11
Joined: 08 Feb 2020 14:55

Re: Search and Replace Text in Files

Post by napleswebguy »

Same results popup as your current script?

napleswebguy
Posts: 11
Joined: 08 Feb 2020 14:55

Re: Search and Replace Text in Files

Post by napleswebguy »

Or show the results in a new tab?

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

Re: Search and Replace Text in Files

Post by highend »

Code: Select all

    $safe = <<<>>>
txt
xml
xys
nfo
<extend me if necessary!>
>>>;

    $search = input("Enter search term...", , , "m");
    end (!$search), "Empty search terms are not accepted!";

    $notSafe  = "";
    $found    = "";
    $notFound = "";
    foreach($item, <get SelectedItemsPathNames |>, , "e") {
        $ext = gpc($item, "ext");
        if (!regexmatches($safe, "^$ext$")) {
            $notSafe = $notSafe . ((!$notSafe) ? "<crlf>Items with invalid file extension(s)<crlf>$item" : "<crlf>$item" );
            continue;
        }
        if (strpos(readfile($item), $search) != -1) {
            $found .= ((!$found) ? "<crlf>File(s) with matching search term<crlf>$item" : "<crlf>$item" );
        } else {
            $notFound .= ((!$notFound) ? "<crlf>File(s) without matching search term<crlf>$item" : "<crlf>$item" );
        }
    }
    $result = $notSafe . <crlf> . $found . <crlf> . $notFound;
    text $result;
if you want it in a new tab, lookup the commands tab() and paperfolder()...
One of my scripts helped you out? Please donate via Paypal

napleswebguy
Posts: 11
Joined: 08 Feb 2020 14:55

Re: Search and Replace Text in Files

Post by napleswebguy »

Perfect! Thank you!

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

Re: Search and Replace Text in Files

Post by highend »

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

napleswebguy
Posts: 11
Joined: 08 Feb 2020 14:55

Re: Search and Replace Text in Files

Post by napleswebguy »

You are welcome!

Question, is it possible to modify the script to search for words INSIDE a zip file?

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

Re: Search and Replace Text in Files

Post by highend »

Inside files in a .zip file? Not without finding a CLI tool that can do that. XY itself can't...
One of my scripts helped you out? Please donate via Paypal

napleswebguy
Posts: 11
Joined: 08 Feb 2020 14:55

Re: Search and Replace Text in Files

Post by napleswebguy »

Okay thanks!

Do you know of any other solutions for something like this?

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

Re: Search and Replace Text in Files

Post by highend »

Sure,

FileLocator Pro can do this (https://www.mythicsoft.com/filelocatorpro/).

Beware, paid software!

it's scriptable so you could even integrate it's search results for such cases...
One of my scripts helped you out? Please donate via Paypal

napleswebguy
Posts: 11
Joined: 08 Feb 2020 14:55

Re: Search and Replace Text in Files

Post by napleswebguy »

Thanks, I'll check it out!

Have a great weekend!

kiwichick
Posts: 648
Joined: 08 Aug 2012 04:14
Location: Windows 10 Pro 22H2, 150% scaling

Re: Search and Replace Text in Files

Post by kiwichick »

highend wrote: 31 Mar 2017 17:42 Sure, via scripting, as long as these are real text files (no pdf, word, or anything else...)
I've taken part of your script to do a simple search and replace on one xml file. This is the code:

Code: Select all

$config = "C:\configx.xml";

	$search = bookMarkMargin="hide";
	$replace = bookMarkMargin="show";
	$oldContent = readfile($config);
	$newContent = replace($oldContent, $search, $replace);
	writefile($config, $newContent);
This is just for one search and replace but I have four I need to do. Is it possible to include all four in the two variables I have for search and replace? And, if so, how would I go about doing it?
Windows 10 Pro 22H2

Post Reply