Sign in to follow this  
Followers 0
Xulong

Trying to return a failed or passed value (0, 1) after file read located the text I am looking for is presented or not

4 posts in this topic

So I am trying to return a value (0 or 1) for failed or passed after file read located a specific things I am looking for is there or not. Below is part of my script and is related to the file read function (currently I used the msgbox, which I want it return a value and exit the function and the returned value will be used by Cruise Control).

So if the "         0" is presented in the file I am reading, then return a value of 0, otherwise return a 1.

And if the result is 0 then return that 0 value, which this value will be used to check against in our cruise control success exit code, i.e., our success exit code is 1, the value returned by autoIT after file read is 0, then the build will be failed. 

Hope this is clear. and thanks for your help.

$file = FileOpen($verifile, 0)
$read = FileRead($file)
If @error = -1 Then
   _FileWriteLog($SQLAutomationLog, "error reading sqlresults file")
   Exit
Else
   _FileWriteLog($SQLAutomationLog, $read)
   If StringRegExp($read, "          0") Then
      MsgBox(0, "Failed", "There is failed SQL test")
   Else
      MsgBox(0, "Passed", "All SQL tests are passed")
   EndIf
EndIf
FileClose($file)

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I just tested your script, it's working for me.

In attach the text file I've used to test this.

If I got your question right, I've used $check variable to return the result

#include <file.au3>

Func SQL_Test_Check()

   Local $verifile="test.txt"
   Local $SQLAutomationLog
   Local $check

   $file = FileOpen($verifile, 0)
   $read = FileRead($file)

   If @error = -1 Then
      _FileWriteLog($SQLAutomationLog, "error reading sqlresults file")
      Exit
   Else
      _FileWriteLog($SQLAutomationLog, $read)
      $check = StringRegExp($read, "          0")
      If $check Then
         MsgBox(0, "Failed", "There is failed SQL test")
      Else
         MsgBox(0, "Passed", "All SQL tests are passed")
      EndIf
   EndIf

   FileClose($file)

   Return $check

EndFunc

MsgBox(64,"The return value","The return value from SQL_Test_Check function is: " & SQL_Test_Check())

 

test.txt

Edited by r3dbullo88
Added the return value.

Share this post


Link to post
Share on other sites

Thanks for your help r3dbullo88. That's great, I can get a return value now.

But the value returned here is 1 because the script works fine but I want 0 returned if I find a specific string in the test file (i.e., here is "        0"), and not about the script being ran is failed or pass. (I recorded 0 or 1 for test fail and pass while executing those sql script). As I need a value returned by AutoIT which I can use in the next step to fail a build process in Cruise Control.

Below is my current script with your function, return, end function.

#include <File.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

SQL_Test_Check()

Func SQL_Test_Check()
; File path for msi
Local $filepath = "C:\sqlbatch.bat"
Local $verifile = "C:\Temp\sqlresults.txt"
Local $check

;Create a write log file
Local $SQLAutomationLog = FileOpen (@ScriptDir & "\Temp\SQLAutomationLog", 1)

; check msi is there or not
If FileExists ($filepath) Then
   _FileWriteLog($SQLAutomationLog, "sqlbatch has been found")
Else
   _FileWriteLog($SQLAutomationLog, "sqlbatch has not been found, please ensure you have sqlbatch under your testing environment")
EndIf

; run bat file
Local $batch = Run ("C:\sqlbatch.bat")
ProcessWait ($batch)
Sleep (20000)
_FileWriteLog($SQLAutomationLog, "sqlbatch.bat file found and starting")

; check sqlresults file is there or not
If FileExists ($verifile) Then
   _FileWriteLog($SQLAutomationLog, "sqlresults file has been found")
Else
   _FileWriteLog($SQLAutomationLog, "sqlresults file has not been found, please ensure the sqlbatch.bat has been run correctly")
EndIf

; red the sqlresults file
$file = FileOpen($verifile, 0)
$read = FileRead($file)
If @error = -1 Then
   _FileWriteLog($SQLAutomationLog, "error reading sqlresults file")
   Exit
Else
   _FileWriteLog($SQLAutomationLog, $read)
   $check = StringRegExp($read, "          0")
   If $check Then
      _FileWriteLog($SQLAutomationLog, "Failed, there is failed SQL test")
   Else
      _FileWriteLog($SQLAutomationLog, "Passed, all SQL tests are passed")
   EndIf
EndIf

FileClose($file)

Return $check

EndFunc

;debug use, check the returned value
;MsgBox(64,"The return value","The return value from SQL_Test_Check function is: " & SQL_Test_Check())

; delete the sqlresults log file
;FileDelete ("C:\Temp\sqlresults.txt")

 

Share this post


Link to post
Share on other sites

Hi r3dbullo88,

I figured out, actually, I was too focused on 0 being failed and 1 being success, alternatively, I can change the value I used in Cruise Control to take 1 as failed and 0 as success. 

Thanks for your help.

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  
Followers 0