I have a sample.txt file with around 1000 strings and i have a main folder folder "adapt" which contains many files (files are also present inside many sub folders and file types are .xml, .cpp, .h and few more).

I need a batch script which will takes input as the sample.txt file (which is present in the same directory where batch script is) and either asks user to select the folder/ lets user modify the path of the folder in the code (folder is also present in the same directory as of batch file), and inside the folder&sub folders in all the file types, it has to search every string in the sample.txt file and if found write "found" in result.txt file/ if not found write "not found" in result.txt file.

Here are few strings in the text file





I found a script that prompts user for a string and gives the matches in another text file but i am having trouble in updating it. Here is what i found link.

The code I am trying to modify is below.

@echo off
set OUTPUT_FILE="output.txt"
set /p "var1=Enter the String to Find: "

pushd %~p0
type NUL > %OUTPUT _FILE%.tmp
for /f "delims=" %%a in ('dir /B /S *.txt') do (
for /f "tokens=3 delims=:" %%c in ('find /i /c "%var1%" "%%a"') do (
    for /f "tokens=*" %%f in ('find /i "%var1%" "%%a"') do if %%c neq 0 echo         %%f
) >> "%OUTPUT_FILE%".tmp
move %OUTPUT_FILE%.tmp %OUTPUT_FILE% >nul 2>&1


Any help with the script please.

  • So you've got a lot of folders, files, extensions and keywords. But no code of your tries nor a sample of expected output. – LotPings Nov 12 '16 at 12:04
  • Please note that superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?. – DavidPostill Nov 12 '16 at 13:23
  • I tried modifiying the code provided in the above link, but it dint work out, still facing issues with that. I dint want to put an not working code, expected output is "string found/not found" for every string I am passing from the text file for searching in the folders @LotPings – S6633d Nov 12 '16 at 14:24
  • I am trying to modify the code provided in the above link and I had mentioned my expected output, do you want me to write down not working code i am trying to modify @DavidPostill – S6633d Nov 12 '16 at 14:26

You can check my reply on this Stack Overflow question - I've pasted it below as well:

REM This Script is written and modified by (c) Hackoo 2016 on 13/11/2016
Title Scan a folder and Search a string in multi-files by (c) Hackoo 2016
mode con cols=75 lines=2
Call :init
Call :Browse4Folder "Choose source folder to scan" "c:\scripts"
Set "ROOT=%Location%"
::Does string have a trailing back slash ? if yes, so, we remove it !
IF %ROOT:~-1%==\ SET ROOT=%ROOT:~0,-1%
SET "EXT=xml cpp hpp"
SET "Count=0"
Set "NewFolder2Copy=%userprofile%\Desktop\CopyFiles"
Set "LogFile=%~dp0%~n0_Log.txt"
SETLOCAL enabledelayedexpansion
REM Iterates throw the files on this current folder and its subfolders.
REM And Populate the array with existent files in this folder and its subfolders
For /F %%i in ('Type "sample.txt"') do (
Set "Word2Search=%%i"
    For %%a in (%EXT%) Do ( 
        Call :Scanning "!Word2Search!" "*.%%a" 
        Call :PS_Sub 'information' 10 '"Scanning now for """!Word2Search!""" on """*.%%a""" . . . "' "'Please wait. . . Scan is in progress on all """*.%%a""" . . .'" 'info' 5
            FOR /f "delims=" %%f IN ('dir /b /s "%ROOT%\*.%%a"') DO (
                ( find /I "!Word2Search!" "%%f" >nul 2>&1 ) && (
                SET /a "Count+=1"
                set "list[!Count!]=%%~nxf"
                set "listpath[!Count!]=%%~dpFf"
                ) || (
            ( Call :Scanning "!Word2Search!" "%%~nxf" )
cls & color 0B
echo wscript.echo Len("%ROOT%"^) + 20 >"%tmp%\length.vbs"
for /f %%a in ('Cscript /nologo "%tmp%\length.vbs"') do ( set "cols=%%a")
If %cols% LSS 50 set /a cols=%cols% + 24
rem If %cols% LSS 50 set /a cols=%cols% + 15
set /a lines=%Count% + 17
Mode con cols=%cols% lines=%lines%
Call :color 0A " ------------------------------------------------" 1
ECHO   Folder : "%ROOT%"
Call :color 0A " ------------------------------------------------" 1
rem Display array elements 
for /L %%i in (1,1,%Count%) do (
    set "msg=[%%i] - !list[%%i]!"
    echo !msg!
ECHO Total of [%EXT%] files(s) found
Call :color 0D "Type the number of file that you want to explore" 1
Call :color 0C "To save results into a LogFile just type 'LOG'" 1
Call :color 0A "To copy all files found just type 'Copy'" 1
set /p "Input="
For /L %%i in (1,1,%Count%) Do (
    If "%INPUT%" EQU "%%i" (
        Call :Explorer "!listpath[%%i]!"
    IF /I "%INPUT%"=="Log" (
        Call :Save_Results
    IF /I "%INPUT%"=="Copy" (
        Call :CopyFiles
If Exist "%LogFile%" Del "%LogFile%"
rem Display array elements and save results into the LogFile
    Echo   ------------------------------------------------
    ECHO   Folder : "%ROOT%"
    echo   ------------------------------------------------

for /L %%i in (1,1,%Count%) do (
    set "msg=[%%i] - !list[%%i]!"
    echo !msg! -- "!listpath[%%i]!" >> "%LogFile%"

    ECHO Total of [%EXT%] files(s^) : %Count% file(s^) found
)>> "%LogFile%"
Start "" "%LogFile%"
:Scanning <Word> <file>
mode con cols=75 lines=3
Cls & Color 0A
echo         Scanning for the string "%~1" on "%~2" ...
goto :eof
:Explorer <file>
explorer.exe /e,/select,"%~1"
Goto :EOF
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
    <nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
    echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
exit /b
set Location=
set vbs="%temp%\_.vbs"
set cmd="%temp%\_.cmd"
for %%f in (%vbs% %cmd%) do if exist %%f del %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
    echo set shell=WScript.CreateObject("Shell.Application"^) 
    echo set f=shell.BrowseForFolder(0,"%~1",0,"%~2"^) 
    echo if typename(f^)="Nothing" Then  
    echo wscript.echo "set Location=Dialog Cancelled" 
    echo WScript.Quit(1^)
    echo end if 
    echo set fs=f.Items(^):set fi=fs.Item(^) 
    echo p=fi.Path:wscript.echo "set Location=" ^& p
cscript //nologo %vbs% > %cmd%
for /f "delims=" %%a in (%cmd%) do %%a
for %%f in (%vbs% %cmd%) do if exist %%f del /f /q %%f
for %%g in ("vbs cmd") do if defined %%g set %%g=
goto :eof
:PS_Sub $notifyicon $time $title $text $icon $Timeout
PowerShell  ^
  [reflection.assembly]::loadwithpartialname('System.Windows.Forms') ^| Out-Null; ^
 [reflection.assembly]::loadwithpartialname('System.Drawing') ^| Out-Null; ^
 $notify = new-object system.windows.forms.notifyicon; ^
  $notify.icon = [System.Drawing.SystemIcons]::%1; ^
  $notify.visible = $true; ^
  $notify.showballoontip(%2,%3,%4,%5); ^
  Start-Sleep -s %6; ^
%End PowerShell%
exit /B
:MakeCopy <Source> <Target>
If Not Exist "%~2\" MD "%~2\"
Copy /Y "%~1" "%~2\"
goto :eof
mode con cols=80 lines=20
for /L %%i in (1,1,%Count%) do (
    echo Copying "!list[%%i]!" "%NewFolder2Copy%\"
    Call :MakeCopy  "!listpath[%%i]!" "%NewFolder2Copy%">nul 2>&1 
Call :Explorer "%NewFolder2Copy%\"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.