get a value from a file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Keegan
Posts: 1
Joined: 07 Dec 2014 13:26

get a value from a file

#1 Post by Keegan » 07 Dec 2014 13:34

Hi all,
sorry if this has been asked before.
I am trying to get a value from a txt file and then do something if it is correct:

The file is like this:
Scanned result

Size : 1000 gB
Damaged Blocks : 0.0 %
Elapsed Time : 0:56

What I want to do is check if damaged blocks are 0,0% and continue, but if it is not 0,0% then I wan to do something else.

This is what I have so far:
findstr /B /E "0,0%" "c:\test.log" >nul
IF NOT ERRORLEVEL 1 goto next
goto fail

But it is not working.

Anyone?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: get a value from a file

#2 Post by Squashman » 07 Dec 2014 15:05

I see a few issues.

Your file examples shows a space in between the 0,0 and %.
Your code does not have the space.
You are using the /B switch with the findstr command. That means it has to find the matching expression at the beginning of the line and match the end of the line because you are using /E. So the only thing that can be on the line is: 0,0 %

Secondly you have to know that the percent symbol is a special character used for variable expansion in batch files.

In order to find a percent symbol, you need to use two percent symbols.

Code: Select all

findstr /E "0,0 %%" "c:\test.log" >nul


But I wouldn't even do it that way.

Code: Select all

FOR /F "tokens=2 delims=:" %%G in ('type c:\test.log ^|find /i "Damaged Blocks"') do set dblocks=%%~G
IF /I "%dblocks%"==" 0,0 %%" goto next

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: get a value from a file

#3 Post by Compo » 07 Dec 2014 16:49

Using the text directly from your stated 'Scanned result':

Code: Select all

@Echo Off
Find "Damaged Blocks : 0.0 "<C:\test.log>Nul &&(GoTo Next)
Rem 'fail code here'
Echo( Damaged Blocks Found
Exit/B
:Next
Rem 'continue code here'
Echo( No Damaged Blocks Found

Post Reply