Computer Hope

Microsoft => Microsoft DOS => Topic started by: sandeep02 on September 15, 2006, 06:13:01 PM

Title: How to get value in variable from FINDSTR function
Post by: sandeep02 on September 15, 2006, 06:13:01 PM
Hi,

Can anyone please help me out in this, this task is most important for me, Please accept my thanks in advance and help me resolving this issue.

Sandy

Please see the code below, i am trying to search for a particular string in a specific file using FINDSTR function, after that i have to execute a conditional task, mean if conditions is true than task will be executed other wise not.

PLEASE NOTE BELOW CODE IS NOT WORKING, IT IS NOT GETTING VALUE RETURNED BY FINDSTR.
===================================================================

set StatusValue =  FINDSTR StatusValue "Import finished" c:\test2.txt

echo %StatusValue%

if not '%StatusValue%' == 'Import finished'(
      echo Test successfull
      goto END      
       )      ELSE (
      goto FileNotFound
      goto END
)
Title: Re: How to get value in variable from FINDSTR func
Post by: Sidewinder on September 15, 2006, 06:42:12 PM
This must be one of those puzzles that if you stare at long enough you'll go blind. I can't for the life of me see where FINDSTR is actually executed.

First up: set StatusValue = this statement is declaring a variable named [highlight]StatusValue [/highlight]; note the trailing space. This will never equate to %StatusValue%

Second up: the FINDSTR needs a search string and a filename to search; switches are optional.  You are using too many arguments.

Third up: single quotes and double quotes while both very useful, are not interchangable.

If you are trying to see if the string "Import Finished" is found in c:\test2.txt something like this may work:

Code: [Select]
@echo off
findstr /i "Import finished" c:\test2.txt
if errorlevel 1 echo Test Unsuccessful & goto end
if errorlevel 0 echo Test Successful & goto end
:end

Hope this helps.  8-)
Title: Re: How to get value in variable from FINDSTR func
Post by: sandeep02 on September 15, 2006, 06:49:29 PM
Thanks a lot it resolved my problem, You are a life saver.  :)