'Search a string from files inside folder and return the file path using command line windows

I'm using "FindStr" command to search for particular string like this :

findstr /s /n /i /p foo *

however, its showing me errors filestr : can not open "pathnames"

I refereed this link (http://ss64.com/nt/findstr.html) to get use of exact command.

My .txt files are located like this "\pr\DU\Extract\PO" then inside "PO" there are folders with dates like "F20160203","F20160204",etc... like this...

The .txt files are there inside these "F20160203","F20160204"... folders. Out of all these folders , in one folder has a .txt file which has string "foo". And, I need to get(return) the path of that folder which has file that contains string "foo".

for example: if this \pr\DU\Extract\PO\F20160203" has the .txt file and "foo" inside that text file; then it should return me path \pr\DU\Extract\PO\F20160203\test.txt" like this.

How can I achieve this? Any sample example would help.



Solution 1:[1]

Why do you use /n? It's to show the line number, but you don't need it. Instead you want to get the filename, which is /m. Get a for around it to get the full path:

for /f "tokens=*" %%i in ('findstr /s /i /p /m "Foo" *') do echo %%~fi

Solution 2:[2]

To accomplish your task, i prefer you use powershell with this command:

Get-ChildItem -recurse | findstr -pattern "foo" | group path | select name

You should change directory to PO folder for make the search faster. findstr is often used with pipeline to read containing of files, do not call it alone like that.

Hope this help.

Solution 3:[3]

Find can also search strings.

eg. find some string inside all text files in current directory:

cls & for %i in (*.txt) do find /i "search text" < "%i" && (echo : %i & echo -)

Tested in Win 10

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stephan
Solution 2 Dang Pham
Solution 3 Zimba