Jump to content
Sign in to follow this  
stixaw

While loop to read lines in file

Recommended Posts

stixaw

HELP with While LOOP Please

I have a csv file with any number of rows in it.

I can read a row if I specify the row and then split the string to create a command line based on the data in that row and thus execute it.

However I don't want to do a for because each time I add a row I would need to modify the script.

example .csv file

data1,data2,data3,data4

data1,data3,data3,data4

code I have so far:

#include <GUIConstants.au3>

#include <string.au3>

$file = FileOpen("test.csv", 0)

If $file = -1 Then

MsgBox(0, "error", "File doesn't exist or can't be read")

Exit

EndIf

$string = (FileReadLine($file, 1))

$input = StringSplit($string, ",", 1)

$value1 = $input[1]

$value2 = $input[2]

$value3 = $input[3]

$value4 = $input[4]

$Commandline = "Test.exe " & "-dll "& $value1 & " -name " & $value2 & " -xml " & $value3 & " -in " & $value4

I am basically automating a Test Cycle with this process.

I think I need to do something like this :

Func Command()

while 1

for line in $file

$string = (FileReadLine($file, ())

$input = StringSplit($string, ",", 1)

$value1 = $input[1]

$value2 = $input[2]

$value3 = $input[3]

$value4 = $input[4]

$Commandline = "TestHarness.exe " & "-dll "& $value1 & " -name " & $value2 & " -xml " & $value3 & " -in " & $value4

Wend

Help is greatly appreciated

Edited by stixaw

Share this post


Link to post
Share on other sites
Authenticity

Dim $sTxt = FileRead("test.csv")
If $sTxt = "" Then Exit

Dim $avLines = StringSplit($sTxt, @CRLF, 1)
Dim $sValue1, $sValue2, $sValue3, $sValue4, $aInput

For $i = 1 To $avLines[0]
    $aInput = StringSplit($avLines[$i], ",")
    $sValue1 = $aInput[1]
    $sValue2 = $aInput[2]
    $sValue3 = $aInput[3]
    $sValue4 = $aInput[4]

    $Commandline = "Test.exe " & "-dll "& $value1 & " -name " & $value2 & " -xml " & $value3 & " -in " & $value4
    ; ...
Next

Edited by Authenticity

Share this post


Link to post
Share on other sites
enaiman

Have a look at _FileReadToArray (included in File.au3)

That will read the whole file, split the content for @CRLF and return an array having each element = 1 line.

Have a play with, I'm sure you will like it.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
stixaw

Ok I have to change something as now

#include <GUIConstants.au3>

#include <string.au3>

#include <file.au3>

Dim $dllPath = "C:\Agent\"

Dim $Harness = "C:\Test.exe"

Dim $fDirPath = "C:\Agent\THFILES\"

Dim $stext = FileRead("test.csv")

If $stext = "" Then

MsgBox(0, "error", "File doesn't exist or can't be read")

Exit

EndIf

Dim $avLines = StringSplit($stext, @CRLF, 1)

Dim $sValue1, $sValue2, $sValue3, $sValue4, $aInput

For $i = 1 To $avLines[0]

$aInput = StringSplit($avLines[$i], ",")

$sValue1 = $aInput[1]

$sValue2 = $aInput[2]

$sValue3 = $aInput[3]

$sValue4 = $aInput[4]

$Commandline = "test.exe" & "-dll "& $dllPath&$sValue1 & " -name " & $sValue2 & " -xml " & $fDirPath&$sValue3 & " -in " & $fDirPath&$sValue4

Next

Which works great.. now the question is how do I execute the commandline once its formed?

I think I want Shellexecutewait but its not working..

Thanks again for any advice and help

Share this post


Link to post
Share on other sites
Authenticity

If "test.exe" is not in the working directory you'll need to specify the full path to the executable. If the path contains blanks (spaces) then you'll need to double quote it such as in "C:\Program Files\Something V2\V2.exe". Using AutoIt strings manipulation it's better to quote the entire string using single quote ' so double quotes doesn't need to be doubled. Example:

$Commandline = '"C:\Test\Pa th\test.exe" -dll ' & $dllPath & $sValue1 & ' -name ' & $sValue2 & ' -xml ' & _
      $fDirPath & $sValue3 & ' -in ' & $fDirPath&$sValue4

Run($Commandline)

Share this post


Link to post
Share on other sites
stixaw

Ok now that I have that all going one last piece of the puzzle.

How do I pipe the output from the .exe Commandline to a log? Normally you would use > log not sure if I can do that now..

once I have this all figured out will post it so others if they want to reuse the code can :D

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.