Jump to content

Convert StringBetween value so that it can then be used with FileWrite


Recommended Posts

Morning,

I am working on a multi-layered AutoIt solution to automate the installation of several different pieces of healthcare software.  One of them uses a jboss vault and I need to capture specific output from some batch files to be put in the standalone.xml file.  This output is always between the strings in my code below and is 3-4 lines long.  Here is what I have done so far.

1.  Run the batch file and send output to a text file

2.  Read from the text file using StringBetween to grab the section I need.

Now I need to take that section represented in an Array, and convert it to a string so that I can then insert it into the standalone.xml file.  See some code snippets below and if there is any more information you need please let me know.  Thanks!

P.S. - I have messed with _ArrayToString but can't seem to get it to output anything.

 

Func Read()
    Local $sFileName = 'C:\drive\log.txt'
    Local $hFileOpen = FileOpen($sFileName, $FO_READ)
    Local $sFileRead = FileRead($hFileOpen)
    Local $aFileRead = _StringBetween($sFileRead, "<vault>", "</vault>")
        If @error Then Exit MsgBox(16, "Error", "String not found")
    _ArrayDisplay($aFileRead, "Vault")
    FileClose($hFileOpen)
EndFunc


 

Link to comment
Share on other sites

I wrote this in about 5 min as a response to a different thread months ago.  Not sure if it will help you or if it even works.

 

Func ArrayToString($array)     
Local $temp     
for $x=1 to UBound($array)-1         
$temp&=$array[$x] & " "    
Next    
Return $temp     
EndFunc

 

Link to comment
Share on other sites

6 hours ago, Nine said:

Can you show the _ArrayDisplay result.  And give us an example of the XML file you want to create from the array.

Sure thing.  So the value in Row 0 Col 0 is the correct value when displaying the array from my original snippet.  I need to take this value and convert it into a string so that it can be inserted into a particular spot in the xml file.  My plan is to just use a FileRead similar to the code I have below to insert this line into the xml.  The XML generated by the initial install will always have the same values in it so while its clunky it doesn't have to be particularly dynamic to get the job done.  If there is a way to do a StringReplace between two strings though that would work even better.  Thanks!

$File= "C:\drive\autologin.reg"
$FileContent=FileRead($File,FileGetSize($File))
$Find= "CHANGEME"
$Replace= $sAcct
$FileContent=StringReplace($FileContent,$Find,$Replace)
FileDelete($File)
FileWrite($File,$FileContent)



image.png.86d796cf587aa8d5cef3cb0585b51008.png

Link to comment
Share on other sites

1 hour ago, Zedna said:

I use this wrapper (to return string instead of array):

Func StringBetween($sString, $sStart, $sEnd)
    $sReturn = _StringBetween($sString, $sStart, $sEnd) ;, -1, 1)
    If @error Then Return ''
    Return $sReturn[0]
EndFunc

 

Amazing work. That's exactly what I needed to solve my problem.  See my snippet below for the final solution I used and just verified as working.  Thanks!

 

Local $sFileName = 'C:\drive\jboss.txt'
Local $hFileOpen = FileOpen($sFileName, $FO_READ)
Local $sFileRead = FileRead($hFileOpen)
$sStart = "<vault>"
$sEnd = "</vault>"
$jboss1 = StringBetween($sFileRead, $sStart, $sEnd)

Local $sFileName = 'L:\jboss-eap-6.4-TC2POE\standalone\configuration\standalone.xml'
Local $hFileOpen = FileOpen($sFileName, $FO_READ)
Local $sFileRead = FileRead($hFileOpen)
$sStart = "<vault>"
$sEnd = "</vault>"
$jboss2 = StringBetween($sFileRead, $sStart, $sEnd)

_ReplaceStringInFile($sFileName, $jboss2, $jboss1)

 

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...