Jump to content
autoitxp

Read value from text file

Recommended Posts

autoitxp

Hi

my text.txt file got data like this i just want to pick or get value of ID how to do that need ur help

name= xxxx

subject = xxxx

email = xxxx

ID = 23123

number = xxxxxx

Share this post


Link to post
Share on other sites
JohnOne

There are a number of ways to carry out such a task

You should look at these functions in the helpfile to spawn some ideas

_FileCountLines

FileRead

FileReadLine

StringSplit

StringInStr

_FileReadToArray


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
sahsanu

Maybe this could help you to start with your script:

$result = StringRegExp(FileRead("text.txt"),"ID\s=\s(.*)",1)
If IsArray($result) Then
    MsgBox(0,"","ID is " & $result[0])
Else
    MsgBox(0,"","I didn't find the ID")
EndIf
Edited by sahsanu

Share this post


Link to post
Share on other sites
antonioj84
On 26/04/2011 at 3:13 AM, wakillon said:

A bit like sahsanu solution ! ;)

 

$sFile = @DesktopDir & "\mytext.txt"
$sText = FileRead ( $sFile )
$aHold = StringRegExp ( $sText, '(?s)(?i)ID = (.*?)\r', 3 ) 
FileClose ( $sFile )
ConsoleWrite ( "$aHold : " & Number ( $aHold[0] ) & @Crlf )
 

this example does not work

Share this post


Link to post
Share on other sites
wakillon
44 minutes ago, antonioj84 said:

this example does not work

$sFile = @DesktopDir & "\mytext.txt"
$sText = FileRead ( $sFile )
$aHold = StringRegExp ( $sText, '(?s)(?i)ID = (.*?)\r', 3 )
ConsoleWrite ( "$aHold : " & Number ( $aHold[0] ) & @Crlf )

Script return well the Id value, $aHold : 23123 


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
antonioj84
4 minutes ago, wakillon said:
$sFile = @DesktopDir & "\mytext.txt"
$sText = FileRead ( $sFile )
$aHold = StringRegExp ( $sText, '(?s)(?i)ID = (.*?)\r', 3 )
ConsoleWrite ( "$aHold : " & Number ( $aHold[0] ) & @Crlf )

Script return well the Id value, $aHold : 23123 

 
 
 
it returns value 0
However, the code below works.

$result = StringRegExp(FileRead("getEMVRids.txt"),"ID\s=\s(.*)",1)
If IsArray($result) Then
    MsgBox(0,"","ID is " & $result[0])
Else
    MsgBox(0,"","I didn't find the ID")
EndIf










my text file

emvRids.rids[x].addContactlessData = 1
emvRids.rids[x].rid = A000000025
etype = 4
emvRids.rids[x].addContactlessData = 1
emvRids.rids[x].rid = A000000065
etype = 5
emvRids.rids[x].addContactlessData = 0
emvRids.rids[x].rid = A000000152
etype = 6
emvRids.rids[x].addContactlessData = 0

ID = 23123
$result = StringRegExp(FileRead("text.txt"),"ID\s=\s(.*)",1)
If IsArray($result) Then
    MsgBox(0,"","ID is " & $result[0])
Else
    MsgBox(0,"","I didn't find the ID")
EndIf
Edited by antonioj84

Share this post


Link to post
Share on other sites
wakillon

Try to use the example with "text.txt" instead of "mytext.txt"


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
antonioj84
5 minutes ago, wakillon said:

Try to use the example with "text.txt" instead of "mytext.txt"

no change

readfileCapture.JPG

Share this post


Link to post
Share on other sites
antonioj84

it did return 1, i am using the latest autoit beta  with windows 10

Share this post


Link to post
Share on other sites
wakillon
35 minutes ago, antonioj84 said:

it did return 1, i am using the latest autoit beta  with windows 10

For me with current release or latest beta with Win 7 or Win 8.1 it return well 23123

text.txt

Edited by wakillon

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
careca
22 hours ago, wakillon said:

Try to use the example with "text.txt" instead of "mytext.txt"

Exactly, and it worked here.

screenshot - 31122016-1441.jpg


Spoiler

Paster - Main function is to paste text, but has more functions. (No longer mantained, switched to String Trigger)

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Firefox Profile Backup - Backup/restore previously saved profile.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Share this post


Link to post
Share on other sites
wakillon

Thanks Careca

i really don't know why antonioj84 can't get it to work.

Are you using Win 10 ?


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
mikell

Using the txt file provided by antonioj84 :

emvRids.rids[x].addContactlessData = 1
emvRids.rids[x].rid = A000000025
etype = 4
emvRids.rids[x].addContactlessData = 1
emvRids.rids[x].rid = A000000065
etype = 5
emvRids.rids[x].addContactlessData = 0
emvRids.rids[x].rid = A000000152
etype = 6
emvRids.rids[x].addContactlessData = 0

ID = 23123

There are many reasons for this regex to fail. One among others :

$result = StringRegExp(FileRead("text.txt"), '(?s)(?i)ID = (.*?)\r', 3 )

MsgBox(0,"","$result[0] = " & $result[0] & @crlf & "Number($result[0]) = " & Number($result[0]))

:)

Share this post


Link to post
Share on other sites
wakillon

The better way to ask for help is to show the text on which he try the script

and not to edit his post (for insert his text) after i have received the notification...


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites
mikell

Right   :)

Share this post


Link to post
Share on other sites
antonioj84
On 31/12/2016 at 11:05 AM, mikell said:

Using the txt file provided by antonioj84 :

emvRids.rids[x].addContactlessData = 1
emvRids.rids[x].rid = A000000025
etype = 4
emvRids.rids[x].addContactlessData = 1
emvRids.rids[x].rid = A000000065
etype = 5
emvRids.rids[x].addContactlessData = 0
emvRids.rids[x].rid = A000000152
etype = 6
emvRids.rids[x].addContactlessData = 0

ID = 23123

There are many reasons for this regex to fail. One among others :

$result = StringRegExp(FileRead("text.txt"), '(?s)(?i)ID = (.*?)\r', 3 )

MsgBox(0,"","$result[0] = " & $result[0] & @crlf & "Number($result[0]) = " & Number($result[0]))

:)

 

here is the file

mytext.txt

Share this post


Link to post
Share on other sites
mikell
;$result = StringRegExp(FileRead("text.txt"),"ID\s=\s(.*)",1)

#Include <Array.au3>

$result = StringRegExp(FileRead("text.txt"), '(?s)(?i)ID = (.*?)\r', 3 )
_ArrayDisplay($result, "1")

; now remove the useless options. then it works BUT it will fail if the text contains no trailing newline sequence because of the final \r 

$result = StringRegExp(FileRead("text.txt"), 'ID = (.*?)\r', 3 )
_ArrayDisplay($result, "2")

; if we remove the \r then we get an empty string - the lazy ? makes (.*?) able to match an empty string

$result = StringRegExp(FileRead("text.txt"), 'ID = (.*?)', 3 )
_ArrayDisplay($result, "3")

; here it is

$result = StringRegExp(FileRead("text.txt"), 'ID = (.*)', 3 )
_ArrayDisplay($result, "4")

 

Share this post


Link to post
Share on other sites
antonioj84

IT works thank you all and Mikell

Capture2file.JPG

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

×