I'm new to batch programming please help me
I want to search some keywords from hi.txt file
hi.txt file has 2 keywords like
hello
raju
when I use this command findstr /g:c:\\hi.txt *.* output is in below format
Copy (3) of New Text Document.txt:helloCopy (4) of New Text Document.txt:rajuss
txt:rajuFINDSTR: Cannot open \r\n
BUT I want output of FIVE lines for each string like
--------START------------- 1
Search string is:hello 2
File path is :filepath\filename 3
Line number is :line number 4
--------END------------- 5
Findstr output format
Moderator: DosItHelp
Re: Findstr output format
Code: Select all
@echo off
for /f "tokens=1,2 delims=:" %%a in ('findstr /g:c:\hi.txt *.*') do (
for /f "tokens=1 delims=:" %%c in ('findstr /n /c:"%%b" "%%a"') do (
echo --------START-------------
echo Search string is:%%b
echo File path is :%%~dpa%%a
echo Line number is :line number %%c
echo --------END-------------
)
)
pause
Re: Findstr output format
@foxidrive
Thank you for your reply.
But it worked only for once and stopped working from second time.
I dont know why. The script written by you takes input string from hi.txt and searches for that string in all the files in a specific folder and prints it in the specified output format.
But it worked only Once
Thank you for your reply.
But it worked only for once and stopped working from second time.
I dont know why. The script written by you takes input string from hi.txt and searches for that string in all the files in a specific folder and prints it in the specified output format.
But it worked only Once

Re: Findstr output format
Try this with debugging steps in. It will echo two lines and a pause to the screen for every file that matches. If it doesn't echo to the screen then there are no files in the current folder that match the search terms.
Code: Select all
@echo off
for /f "tokens=1,2 delims=:" %%a in ('findstr /g:c:\hi.txt *.*') do (
echo. "%%b"
echo. "%%a"
pause
for /f "tokens=1 delims=:" %%c in ('findstr /n /c:"%%b" "%%a"') do (
echo --------START-------------
echo Search string is:%%b
echo File path is :%%~dpa%%a
echo Line number is :line number %%c
echo --------END-------------
)
)
pause
Re: Findstr output format
@foxidrive
Thank you for replying.
checked with it and its not working
Thank you for replying.
checked with it and its not working
Re: Findstr output format
Change the search terms. try a simple search for 'a' without the quotes.
Re: Findstr output format
@foxidrive
Thank you for replying
but for some reason the script is not working.
Thank you for replying

but for some reason the script is not working.
Re: Findstr output format
Try this batch file. backup c:\hi.txt first.
Is the problem that the search string which was matched is not printed, but what is printed is the matched text?
I've changed this to show the search string but it will take longer, depending on the number of files.
Is the problem that the search string which was matched is not printed, but what is printed is the matched text?
I've changed this to show the search string but it will take longer, depending on the number of files.
Code: Select all
@echo off
echo a>c:\hi.txt
(
echo abc zzz
echo def
echo ghi
)>test1.txt
(
echo 123
echo def
echo ghi
)>test2.txt
(
echo 123
echo aaa bbb
echo ghi
)>test3.txt
for /f "delims=" %%z in ('type "c:\hi.txt"') do (
for /f "tokens=1,2,* delims=:" %%a in ('findstr /n /c:"%%z" *.*') do (
echo --------START-----------------
echo Search string is: %%z
echo Search string which is matched: %%c
echo File path is : %%~fa
echo Line number is : %%b
echo --------END-------------------
))
pause