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?
get a value from a file
Moderator: DosItHelp
Re: get a value from a file
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.
But I wouldn't even do it that way.
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
Re: get a value from a file
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