Sign in to follow this  
Followers 0
WillTing

How to copy a string from a text file

5 posts in this topic

I'm creating an application restore process where it needs to copy an ID from a text file that has a lot of other variables. 

I was looking into FileReadToArray or FileReadLine however it looks to me that they copy the whole line instead of just the values I want. 

The text file looks something like this: 

 

<appSettings>
 <add key="ServerId" value="d438a121-i91f-4f11-4a80-a682-f6b8ea40c348" />
 <add key="LablelId" value="72617f67-5629-b158-a6c630546ad0" />
</appSettings>
 
 
I need to create an variable that will equal to the ServerID, or just d438a121-i91f-4f11-4a80-a682-f6b8ea40c348 without the quotes or any other values.
 
From there I want to call that variable to input into a text box. ( I can figure this part out on my own)
 
So I'm really looking help with how I can copy the value between the quotes on a specific line within the text file. 
Seems easy enough in my mind, but just started AutoIT and have not been able to find any thing that can help me with this. Thanks.

Share this post


Link to post
Share on other sites



#include <Array.au3>

$sString = '<add key="ServerId" value="d438a121-i91f-4f11-4a80-a682-f6b8ea40c348" />'
$aSplit = StringSplit($sString, '"')
    _ArrayDisplay($aSplit)


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

WillTing,

local $str = _
    '  <appSettings>' & @crlf & _
    '   <add key="ServerId" value="d438a121-i91f-4f11-4a80-a682-f6b8ea40c348" />' & @crlf & _
    '   <add key="LablelId" value="72617f67-5629-b158-a6c630546ad0" />' & @crlf & _
    '  </appSettings>'

local $ServerId = stringregexpreplace($str,'(?is).*serverid" value="([^"]+)".*','\1')

ConsoleWrite($ServerId & @CRLF)

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

Thanks for the response guys. I think this will really help me get to the next phase.

Here's how I'm going to use the array function. Because the Server and Label ID's are not constant values and they are different every time I launch the application I will have to use the "FileReadLine" function first to pull this line out. Then I'm hoping I can use the results from the FileReadLine to insert it into the Array function to perform this step. I'm going to try this out today and I will post my results. 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

WillTing,

I need to create an variable that will equal to the ServerID

 

Try post #3.  It creates variable $ServerID.

 

Not sure what you mean by this:

Here's how I'm going to use the array function

 

Regarding "FileReadLine":

  1. Read the file to a variable (see Help File)
  2. Run code in topic #2 using the variable that you read the file to in place of the variable "$str"

Ther is no need to read the file line by line.

kylomas

edit: spelling

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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