Page 1 of 2

Search and Replace Text in Files

Posted: 31 Mar 2017 17:24
by iryx
Is this possible with anything built into the application?

Re: Search and Replace Text in Files

Posted: 31 Mar 2017 17:42
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;


Re: Search and Replace Text in Files

Posted: 19 Jul 2022 12:26
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!

Re: Search and Replace Text in Files

Posted: 19 Jul 2022 12:33
by highend
And how should those results been shown exactly?

Re: Search and Replace Text in Files

Posted: 19 Jul 2022 12:36
by napleswebguy
Same results popup as your current script?

Re: Search and Replace Text in Files

Posted: 19 Jul 2022 12:38
by napleswebguy
Or show the results in a new tab?

Re: Search and Replace Text in Files

Posted: 19 Jul 2022 13:19
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()...

Re: Search and Replace Text in Files

Posted: 19 Jul 2022 13:41
by napleswebguy
Perfect! Thank you!

Re: Search and Replace Text in Files

Posted: 19 Jul 2022 16:08
by highend
No problem and thanks for the donation :tup:

Re: Search and Replace Text in Files

Posted: 23 Jul 2022 12:38
by napleswebguy
You are welcome!

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

Re: Search and Replace Text in Files

Posted: 23 Jul 2022 12:52
by highend
Inside files in a .zip file? Not without finding a CLI tool that can do that. XY itself can't...

Re: Search and Replace Text in Files

Posted: 23 Jul 2022 12:55
by napleswebguy
Okay thanks!

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

Re: Search and Replace Text in Files

Posted: 23 Jul 2022 13:09
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...

Re: Search and Replace Text in Files

Posted: 23 Jul 2022 13:11
by napleswebguy
Thanks, I'll check it out!

Have a great weekend!

Re: Search and Replace Text in Files

Posted: 29 Jul 2022 08:16
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?