1

I'm trying to find files which contains a string and print file names with and without extension.

Very important, files are located sub-folders of main Folder, I know only path of main Folder.

This returns 'full path' and extension of files containing word string:

findstr /s /m "string" c:main Folder\*.txt >list.txt

Desired output: only file name

Cheers, Andy

Andy
  • 79
  • 1
  • 7

2 Answers2

3
@ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN ('findstr /s /m /i "string" "c:main Folder\*.txt" ') DO (
 ECHO full    %%a
 ECHO name    %%~na
 ECHO nameext %%~nxa
)

GOTO :EOF

I added /i for case-insensitivity.

Choose the filename version you want and redirect at will.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Cheers Gents, both case are very useful. – Andy Feb 23 '16 at 14:04
  • @Andy - Be wary of the FINDSTR /S option - it is bugged. It might fail to find some files that match '"*.txt"' if there exist files named something like '"abc.txt2"'. See the section titled *"BUG - Short 8.3 filenames can break the /D and /S options"* at http://stackoverflow.com/a/8844873/1012053 for more info. – dbenham Feb 24 '16 at 02:23
  • @Magoo - Thanks, I will take in count this big. – Andy Feb 25 '16 at 16:08
2
for /r "c:\main Folder" %%# in (*.txt) do @(find "string" "%%f#" >nul 2>&1 && echo %%~n#)>>list.txt

May be FOR /R is what you need?

npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • @ npocmaka How I can output this to a text file? – Andy Feb 23 '16 at 13:28
  • @ npocmaka Sorry, maybe I miss something, but isn't creating even list.txt; Is it searching in sub-folders of 'main folder'?On other side, how I specify in *.txt files to search? – Andy Feb 23 '16 at 13:36