# DOS Keyword search



## Nitekreeper (May 7, 2009)

Writing something that will search for keywords in all the documents within the folder it is contained, as usual I have no clue how to do this. I'm Guessing i'll have to use the _FOR_ command, just dont know how.


```
:search
cls
echo -----------------
echo Type keyword to search for or 0 to return to main menu.
echo -----------------
set /p keyword=Keyword: 
if keyword==0 goto choice
cls
echo -----------------
echo Searching for %keyword%
echo -----------------
pause
goto tagresults

:tagresults
cls
for %keyword% IN %cd%/?.doc

echo -----------------
echo %results%
echo -----------------
```
I'm intending on making it echo each filename the keyword is contained as the numbers 1 - 9, and then use a set command to pick which file you want to open.

I've probably got the syntax all wrong, or completely the wrong idea, so any help is appreciated  thanks.


----------



## TheOutcaste (Mar 19, 2009)

You can use *Find* or *Findstr* to search the files, would need a For loop to capture the output.
*Find* looks for a character match regardless of position, *Findstr* can look at the beginning or end of a word, or both as well.
For example, if they enter *set* for a keyword, do you want it to match "set", "SetLocal", "reset" and "unsettled", or only "Set"?
*Find* will match all of the above. *Findstr* gives you these options as well:
You can match at beginning of a word (would find "SetLocal" and "Set", or match at end of word, which would find "reset" and "Set"?
Or match at beginning and end of a word, which would only match "Set".

Find outputs each file name, even if there isn't a match. Findstr outputs only file names that have a match, so it easier to process. Here's what the output for the same search looks like form both commands:

```
---------- GETLOGNAMES.CMD
SetLocal EnableDelayedExpansion
Set _Source=C:\Temp Dir\Logs
Set _OutputDir=C:\Temp Dir\Test
Set _CCC=%_OutputDir%\ccc.txt
Set _Ofile=%_OutputDir%\Output.txt
   Set _Temp=%%I                         E
   Set _Temp=!_Temp:~0,25!

[B]---------- MOUNTDRIVE.CMD[/B]

---------- MTEST.CMD
Set _DefUsr=C:\Users\Default
Set _MP=C:\~TO~MP
For /F "Tokens=2 Delims=," %%I In ('Wmic CDRom Get Drive /Format:CSV') Do Set _CDRom=%%I
Set _CDRom=Y:
Set _Resp=
Set /P _Resp=Press any key to try again. Press Enter to exit
Set _Count=0
Set /A _Count+=1

GetLogNames.cmd:SetLocal EnableDelayedExpansion
GetLogNames.cmd:Set _Source=C:\Temp Dir\Logs
GetLogNames.cmd:Set _OutputDir=C:\Temp Dir\Test
GetLogNames.cmd:Set _CCC=%_OutputDir%\ccc.txt
GetLogNames.cmd:Set _Ofile=%_OutputDir%\Output.txt
GetLogNames.cmd:   Set _Temp=%%I                         E
GetLogNames.cmd:   Set _Temp=!_Temp:~0,25!
mtest.cmd:Set _DefUsr=C:\Users\Default
mtest.cmd:Set _MP=C:\~TO~MP
mtest.cmd:For /F "Tokens=2 Delims=," %%I In ('Wmic CDRom Get Drive /Format:CSV') Do Set _CDRom=%%I
mtest.cmd:Set _CDRom=Y:
mtest.cmd:Set _Resp=
mtest.cmd:Set /P _Resp=Press any key to try again. Press Enter to exit
mtest.cmd:Set _Count=0
mtest.cmd:Set /A _Count+=1
```
When checking for a response, if the user just presses enter, the variable is not changed. If it has a previous value, that will be used. If it's not defined, it will still be not defined, so you need to clear it first, or define it, then put quotes or something around it, and check for them just pressing Enter if needed:

```
Set keyword=
set /p keyword=Keyword:
if "%keyword%"=="" goto badresponse
if "%keyword%"=="0" goto choice
```
This should get you started, it searches for the keyword as a whole word only. Just lists the file names it finds. It doesn't check to see if there are .doc files in the folder, so if there aren't, it will give an error and stop.


```
@Echo Off
SetLocal EnableDelayedExpansion
Set _Count=0
:: Clear variables
For /F "Tokens=1 Delims==" %%I In ('Set _FN 2^>Nul') Do @Set %%I=>Nul
cls
echo -----------------
echo Type keyword to search for or 0 to return to main menu.
Echo.Press Enter to exit the program
echo -----------------
Set keyword=
set /p keyword=Keyword:
if "%keyword%"=="" Goto :EOF
if "%keyword%"=="0" goto choice
cls
echo -----------------
echo Searching for %keyword%
echo -----------------

For /F "Tokens=1,2 Delims=:" %%I In ('Findstr /I /R "\<%keyword%\>" "*.doc"') Do Call :_Check "%%I" !_Count!
For /L %%I In (1,1,%_Count%) Do Echo _FN%%I is !_FN%%I!
Goto :EOF
::::::::::::::::::::::::::::::::::::::::::::::::::::::
::           Subroutines
::::::::::::::::::::::::::::::::::::::::::::::::::::::
:_Check
If NOT "%~1"=="!_FN%2!" Set /A _Count+=1&Set _FN!_Count!=%~1
Goto :EOF
:choice
:: Main Menu is here
```


----------

