[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

how can i capture value from FINDSTR

Posted on 2008-06-13
6
Medium Priority
?
4,529 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

 
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 150 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

650 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