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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pythagoras
Posts: 2
Joined: 08 Mar 2011 13:34

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

#1 Post by pythagoras » 08 Mar 2011 13:57

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

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

#2 Post by aGerman » 08 Mar 2011 14:13

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

pythagoras
Posts: 2
Joined: 08 Mar 2011 13:34

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

#3 Post by pythagoras » 09 Mar 2011 14:29

Hey, thanks aGerman, that works! Have a good day! :D

Post Reply