Page 1 of 1

using batch script to read text file to capture 6 diff strgs

Posted: 08 Mar 2011 13:57
by pythagoras
Greetings all

I'm attempting to write a script which will read a text file, find 6 different strings/rows, and store those six strings into six different variables within the batch script. So far, my batch script will set the last known string into all six variables. May I have some trouble shooting assistance?

Thanks in advance!

Source file: testFile.txt

Code: Select all

   Speed :            111111111 Bytes/sec.
   Speed :            222222222 Bytes/sec.
   Speed :            333333333 Bytes/sec.
   Speed :            444444444 Bytes/sec.
   Speed :            555555555 Bytes/sec.   
   Speed :            666666666 Bytes/sec.   


Batch Script thus far

Code: Select all

@echo on
setlocal ENABLEDELAYEDEXPANSION


::Declare temp space for 6 pass numbers (3 trials, write, read)
set str_pass_1_write=0
set str_pass_1_read=0
set str_pass_2_write=0
set str_pass_2_read=0
set str_pass_3_write=0
set str_pass_3_read=0



:setNumbers
for /f "tokens=*" %%a in ('findstr "Bytes/sec." testFile.txt') do (
if %str_pass_1_write%==0 set str_pass_1_write=%%a
if %str_pass_1_read%==0 set str_pass_1_read=%%a
if %str_pass_2_write%==0 set str_pass_2_write=%%a
if %str_pass_2_read%==0 set str_pass_2_read=%%a
if %str_pass_3_write%==0 set str_pass_3_write=%%a
if %str_pass_3_read%==0 set str_pass_3_write=%%a
)
goto:setNumbers_End
:setNumbers_End
echo %str_pass_1_write%
echo %str_pass_1_read%
echo %str_pass_2_write%
echo %str_pass_2_read%
echo %str_pass_3_write%
echo %str_pass_3_read%



goto:EOF


:EOF

Re: using batch script to read text file to capture 6 diff s

Posted: 08 Mar 2011 14:13
by aGerman
Not sure if this will work:

Code: Select all

:setNumbers
for /f "tokens=*" %%a in ('findstr /l "Bytes/sec." testFile.txt') do (
if !str_pass_1_write!==0 (set str_pass_1_write=%%a) else (
if !str_pass_1_read!==0 (set str_pass_1_read=%%a) else (
if !str_pass_2_write!==0 (set str_pass_2_write=%%a) else (
if !str_pass_2_read!==0 (set str_pass_2_read=%%a) else (
if !str_pass_3_write!==0 (set str_pass_3_write=%%a) else (
if !str_pass_3_read!==0 (set str_pass_3_write=%%a))))))
)


Regards
aGerman

Re: using batch script to read text file to capture 6 diff s

Posted: 09 Mar 2011 14:29
by pythagoras
Hey, thanks aGerman, that works! Have a good day! :D