kunchesm
asked on
how can i capture value from FINDSTR
Hi all,
i my batch script i need to capture the result of FINDSTR function into a variable ,so that i can use that value for further processing in the same script
in the below given code (in code snippet) result of findstr /I /C:"folder" %sd%\fname.log should be captured into a variable
how can i do that,please suggest
many thanks in advance
i my batch script i need to capture the result of FINDSTR function into a variable ,so that i can use that value for further processing in the same script
in the below given code (in code snippet) result of findstr /I /C:"folder" %sd%\fname.log should be captured into a variable
how can i do that,please suggest
many thanks in advance
cls
@ECHO OFF
set sd=d:
set balu1 =1
findstr /I /C:"folder" %sd%\fname.log > %balu1%
if not errorlevel 1 goto :ABORT
:ABORT
echo %balu1%
OOPS! I left out the findstr command. Correcting:
for /f %%V in ('findstr /I /C:"folder" %sd%\fname.log') do set MYVAR=%%V
...but in you case you need to also redirect the output to a file, so do both:
findstr /I /C:"folder" %sd%\fname.log > %balu1%
for /f %%V in ('findstr /I /C:"folder" %sd%\fname.log') do set MYVAR=%%V
if not errorlevel 1 goto :ABORT
for /f %%V in ('findstr /I /C:"folder" %sd%\fname.log') do set MYVAR=%%V
...but in you case you need to also redirect the output to a file, so do both:
findstr /I /C:"folder" %sd%\fname.log > %balu1%
for /f %%V in ('findstr /I /C:"folder" %sd%\fname.log') do set MYVAR=%%V
if not errorlevel 1 goto :ABORT
hmm, I don't thing the order makes a difference here, but just in I'm swapping them:
for /f %%V in ('findstr /I /C:"folder" %sd%\fname.log') do set MYVAR=%%V
findstr /I /C:"folder" %sd%\fname.log > %balu1%
if not errorlevel 1 goto :ABORT
for /f %%V in ('findstr /I /C:"folder" %sd%\fname.log') do set MYVAR=%%V
findstr /I /C:"folder" %sd%\fname.log > %balu1%
if not errorlevel 1 goto :ABORT
also, I'm assuming from your question that the string you are trying to find only occurs once in the file. If you need to do something with MYVAR for multiple occurrances in the file then this will changes things considerably. Let me know.
ASKER
hi Ralph Brown
thank you so much....it worked.....am new to batch script.....thanks for all your suggestions and time.....
thank you so much....it worked.....am new to batch script.....thanks for all your suggestions and time.....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
for /f %%V in ('/I /C:"folder" %sd%\fname.log') do set MYVAR=%%V
@echo %MYVAR%