Search and Replace Text in Files
Search and Replace Text in Files
Is this possible with anything built into the application?
Re: Search and Replace Text in Files
Sure, via scripting, as long as these are real text files (no pdf, word, or anything else...)
E.g.:
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
@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!
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
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
Same results popup as your current script?
-
napleswebguy
- Posts: 11
- Joined: 08 Feb 2020 14:55
Re: Search and Replace Text in Files
Or show the results in a new tab?
Re: Search and Replace Text in Files
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;
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
Perfect! Thank you!
Re: Search and Replace Text in Files
No problem and thanks for the donation 
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
You are welcome!
Question, is it possible to modify the script to search for words INSIDE a zip file?
Question, is it possible to modify the script to search for words INSIDE a zip file?
Re: Search and Replace Text in Files
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
Okay thanks!
Do you know of any other solutions for something like this?
Do you know of any other solutions for something like this?
Re: Search and Replace Text in Files
Sure,
Beware, paid software!
it's scriptable so you could even integrate it's search results for such cases...
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
Thanks, I'll check it out!
Have a great weekend!
Have a great weekend!
Re: Search and Replace Text in Files
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);Windows 10 Pro 22H2
XYplorer Beta Club