Jump to content

Read value from text file


Recommended Posts

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.

Link to comment
Share on other sites

  • 5 years later...
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

Link to comment
Share on other sites

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

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

Link to comment
Share on other sites

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

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

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

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.

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

Link to comment
Share on other sites

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

:)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

;$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")

 

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