Search code examples
filebatch-filetokencutbetween

Read a .txt file using batch and extract a string between ()


I have a .LOG file whitch contains:

2015-01-20 17:00:59 (15.6 MB/s) - `c:/temp/teste_velocidade.tar.gz' saved [253311/253311]

I'd like to extract the content between "(" ")" into a variable.

Ex.:

$number=15.6
$unit=MB

How can I do This?

I'm seaching for a solution for it but batch is not familiar to me.


Solution

  • Like this :

    @echo off
    
    for /f "tokens=3,4 Delims=() " %%a in (test.log) do (
      set "$Number=%%a" 
      set "$Unit=%%b"
    )
    
    echo Number : [%$Number%]
    echo Unit   : [%$Unit%]
    

    I Used here a file named test.log for the test