How to search a phrase in all text files of a folder for lines having the words foo and bar
and print the text between these words, on the Windows command line - Noob vs. ProAI Noob Answer:
"use the command line tool "for" and "findstr":
for /r %i in (*.txt) do @for /f "tokens=1-3 delims=:" %a in ('findstr /r "foo.*bar" %i ^| findstr /b ":"') do
@echo %i:%c
... who shall remember this?? and it just produces:
syntax error at unexpected word '%i' !
Alternatively, you can use the "powershell" command, the basic syntax is:
Get-ChildItem -Path "C:\path\to\folder" -Filter *.txt -Recurse -Force | Select-String -Pattern "foo.*bar" -Context 0,1 | % { $_.Context.PostContext[0] }
... produces no output. Should that be a starting point for hour-long trial and error?
Great solution! ... if you have an alien neighbour who can translate it for humans.
![](/howto/pic/alien-neighbour-001.jpg)
But if he can't, you're rather lost, and need the
Pro answer:
Forget the above junk, download the free open source SFK for any system and type in one line:
sfk xfind -pure mydir "/foo*bar/[part2]\n/"
Done. Example output:
:file mydir\log1.txt
and the
:file mydir\log2.txt
with some
Secret command discovered!
How to list all files having a word in their filename
Big mystery uncovered!
How to list the newest files in a folder