Andy
Andy

Reputation: 89

Batch: How to find and get file name

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

Upvotes: 1

Views: 1258

Answers (2)

Magoo
Magoo

Reputation: 80211

@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.

Upvotes: 3

npocmaka
npocmaka

Reputation: 57332

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?

Upvotes: 2

Related Questions