Search code examples
windowsbatch-filecmdfindstr

Is it possible to search string in file and display filename as variable in batch


Im trying to search for files as a batch kinda like when your in a folder and you can type what your looking for and it will display names of files with that sting within that file but I want to use it as a variable in a batch

Kinda like

 @echo off
:Start  
Title Details Search

set /P "result1=Number1" 

for /f %%i in ('findstr /i "%result%" "C:\Users\%USERNAME%\Desktop\Folder\*"') do @echo %result1% found in %%~nxi

set /P "result2=Number2: " 

for /f %%i in ('findstr /i "%result2%" "C:\Users\%USERNAME%\Desktop\Folder\*"') do @echo %result2% found in %%~nxi

Pause

It wont allow me to do more than one, where the one below works great with a special thanks to Gerhard Barnard but its only one

@echo off
:Start  
Title Details Search

set /P "result=what would you want to search today?: " 

for /f %%i in ('findstr /i "%result%" "C:\Users\%USERNAME%\Desktop\Folder\*"') do @echo %result% found in %%~nxi
Pause

The outcome im trying to get is something like

Bob 543-434-4324 Tom 234-253-2463

Where the name is the filename and the number is what I typed it. So if I type it in the number it will display next to the filename it found Note- These are not real numbers I just typed random numbers


Solution

  • So as per your change in question, searching for multiple numbers. You need to add your text seperated by a whitespace in your search. i.e 09122 123441 881992

    @echo off
    :Start  
    Title Details Search
    
    set /P "result=what would you want to search today?: "
    for %%a in (%result%) do (
        for /f %%i in ('findstr /i "%%a" "C:\Users\%USERNAME%\Desktop\Folder\*.rtf"') do @echo %%~nxi %%a
    )