Solved

how can i capture value from FINDSTR

Posted on 2008-06-13
6
4,138 Views
Last Modified: 2008-08-07
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
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%

Open in new window

0
Comment
Question by:kunchesm
  • 5
6 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 21778487
in general you can do this:

for /f %%V in ('/I /C:"folder"  %sd%\fname.log') do set MYVAR=%%V

@echo %MYVAR%
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 21778510
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



0
 
LVL 33

Expert Comment

by:knightEknight
ID: 21778540
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
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 33

Expert Comment

by:knightEknight
ID: 21778692
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.
0
 

Author Comment

by:kunchesm
ID: 21790919
hi Ralph Brown

thank you so much....it worked.....am new to batch script.....thanks for all your suggestions and time.....
0
 
LVL 33

Accepted Solution

by:
knightEknight earned 50 total points
ID: 21793303
I'm glad it worked for you.  :)


  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
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now