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
Solved

how can i capture value from FINDSTR

Posted on 2008-06-13
6
4,205 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Office & Windows Activation with Email 3 58
Build VS2015 project in bat command 3 93
how to split this type of line? 5 98
How can i echo a ! to end of a file? 8 23
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

860 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