Jump to content

While loop to read lines in file


Recommended Posts

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
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...