Author Topic: Extract text & save to file?  (Read 19149 times)

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Extract text & save to file?
« on: November 27, 2016, 17:24:52 »
I wondered if I could make a script to search for files containing a string & output the found lines to a text file or clipboard.

I can see that the search & filter functions will find the file content but is it possible to grab the found lines & output them?

SaveStringToFile could be used to output the result but defining the str + parameters & deciding what to output is potentially a bit more complex. 
If I wanted to find any line that included a word or phrase, & output that whole line, can I do it or should I be looking at more complex tools?
e.g.
I want any line with 'cat'
I have a file with this content
Code: [Select]
the frog sat on the mat
the cat sat on the mat
the dog sat on the mat
the fox sat on the mat
the result output should be :- the cat sat on the mat
I assume I can use LF or CRLF to define lines or punctuation such as a full-stop to flag 'whole' lines.

I also like these options:-
  • save string(s) as list
  • save string list including filename (or name & path)
  • save string(s) from multiple files as list
  • Output list to clipboard
Hope that makes sense.
This may be too much for a MC script, but I'd like to play around with writing it if it is possible  :D

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4489
    • View Profile
    • Multi Commander
Re: Extract text & save to file?
« Reply #1 on: November 27, 2016, 21:17:33 »
You can probably in theory script it. But it will not work because when you start a FileSearch from script using CustomCommand MC.FileSearch it will just fire of the command that will start the search and then continue. It will not wait for the search to complete.  (CustomCommand are fire and forget commands..)

If you can do it by script + manual action :)
After you got the search result.. just select all and and do "Copy Path To Clipboard"  (Ctrl+P or Ctrl+Shift+P)

Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Extract text & save to file?
« Reply #2 on: November 28, 2016, 12:36:58 »
OK Thanks Mathias.
I will have to look at how to break up the operation - maybe that would be the best way to get the 'features' I want.
A few smaller scripts that I can run to get certain results could work.

In any case, I cannot live without <ctrl+p> now!  :D

I did find something which appears to do the job I want but is more complex/heavy than I need- it is an interesting bit of coding tho
https://www.binarymark.com/products/batchregex

Maybe I should try Powershell
This would appear to be the right basis for what I want
Code: [Select]
Param (
[Parameter(Mandatory=$true)]
   [string] $FullyQualifiedPath
)

#Note: Make sure the following path is outside those being searched!
$OutputFile = "G:\Test\Results\BegEndResults.txt"  #Adjust as needed

$Pat1 = [regex] '(Begin)*(End)'  #Adjust tags between () as appropriate

Remove-Item "$OutputFile"

Get-ChildItem -Path "$FullyQualifiedPath" |

Get-Content  | Select-String -Pattern $Pat1 -AllMatches >> "$OutputFile"
« Last Edit: November 28, 2016, 12:55:00 by Ulfhednar »

Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4489
    • View Profile
    • Multi Commander
Re: Extract text & save to file?
« Reply #3 on: November 28, 2016, 13:02:06 »
Well you can do your own search with MultiScript..  You can enumerate all files with FindFiles(...)  but it will not scan recursively and if you need to match file content you need to do that you self in script too.  But it might be do able..


Ulfhednar

  • Contributor
  • VIP Member
  • *****
  • Posts: 514
    • View Profile
Re: Extract text & save to file?
« Reply #4 on: November 29, 2016, 16:00:07 »
So far I have been using the GUI with the search & filter functions & using <ctrl+p> to get my paths when needed; I can use MDV to review the files themselves.

Thanks for the advice Mathias  8)
FindFiles will give me an array of files I can then run a regex on. 
I should be able to pass that result to the SaveStringToFile function.  That certainly has to be at least as quick as what I'm doing via the GUI. 
The trick will be to move it along a few steps to get the 'finished product'.  I will experiment & see if I can produce something that completes some of the steps more quickly than I can do it via the GUI.  Then I can start to look at the other aspects.

Quite a few possibilities are present but require a bit more code than powershell by the look of it :)


For those who want this kind of functionality *now!* I've found a nice freeware util that does it & more  -
http://prgrep.com/?lng=en  PRGrep