Sign in to follow this  
Followers 0
KingYoshi

Save Text from file to variable

14 posts in this topic

Is there a way that I can either save text from an external text file to a variable within Autoit. Or insert text from a specific line of a text file, and have Autoit write it out?

I understand how this would be done if I just wrote the text in the text file.

The Goal:

#1 A person edits a text file in any way they want.

#2 Autoit reads the text on a specific line in the textfile and sets this to a variable

#3 Autoit then sends the text held in the variable through Send("{Enter} $Variable {Enter}")

I want someone to be able to edit an external text file, and change what that specific line of text says, therefore changing what text Autoit outputs.

Thank you for any and all help/suggestions.

Share this post


Link to post
Share on other sites



KingYoshi,

FileReadLine sounds like the function you need. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Was looking at that function, however it seems to read the entire file. Is there a way to pinpoint a specific line of text within the file?

Or could I have t look for a specific symbol such as ~ and then read all text inbetweeen two ~ ~?

Share this post


Link to post
Share on other sites

Do you have a Help file anywhere? Read about that function, in particular how to use it, and then read about the _StringBetween function, for the 'all the text between the two ~~' thing.


- Bruce /*somdcomputerguy */  If you change the way you look at things, the things you look at change.

Share this post


Link to post
Share on other sites

KingYoshi,

it seems to read the entire file

Which bit of:

FileReadLine 

Read in a line of text from a previously opened text file.

FileReadLine ( "filehandle/filename" [, line] )

Parameters

line [optional] The line number to read.

gave you the idea it read the entire file? :)

Are you sure you read the correct page in the Help file. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Woops. I feel stupid. I completely missed the parameter!!!

Thank you for your help.

Share this post


Link to post
Share on other sites

KingYoshi,

It happens! :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

I used the function and it works great.

However the variable is not being outputted through Send("{Enter}/1" & $Msg1 & "{Enter}",1)

Only Enter /1 Enter is being outputted.

Source Code:

; Press Esc to terminate script
; Press Pause to pause the script
; Random Sendkeydelay FileReadLine  FileOpen

Global $UnPaused, $Keypause, $Delay, $file, $Msg1
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")



$file = FileOpen("message.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

; Read in lines of text until the EOF is reached
While 1
        $Msg1 = FileReadLine($file)
    If @error = -1 Then ExitLoop
Wend
FileClose($file)



While 1
    Sleep(100)
    ToolTip("Paused",0,0)
WEnd

Func TogglePause()
    $UnPaused = NOT $UnPaused
    While $UnPaused
        ToolTip("Sending...",0,0)
        $Keypause = Random(1, 5, 1)
        AutoItSetOption("SendKeyDelay", $Keypause)
        $Keypause = Random(1, 5, 1)
        AutoItSetOption("SendKeyDownDelay", $Keypause)
        Send("{Enter}/1" & $Msg1 & "{Enter}",1)
        $Delay = Random(5000, 6000, 1)
        Sleep($Delay)
        
        ;Insert copies HERE of above 5 lines for more messages
    WEnd
EndFunc

Func Terminate()
    Exit 0
EndFunc

Share this post


Link to post
Share on other sites

I really find this weird, since I am getting no syntax errors when running it..

There has to be some sort of mistake in my code...

Share this post


Link to post
Share on other sites

KingYoshi,

There has to be some sort of mistake in my code...

Sure is! ;)

You are reading all the lines in one go until the end of the file - so at the end $Msg1 is empty as there are no lines left. :)

You need to read and Send each line in turn. Try this - look for the <<<<<<<<< line:

; Press Esc to terminate script
; Press Pause to pause the script
; Random Sendkeydelay FileReadLine  FileOpen

Global $UnPaused, $Keypause, $Delay, $file, $Msg1
HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")

$file = FileOpen("message.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

While 1
    Sleep(100)
    ToolTip("Paused",0,0)
WEnd

Func TogglePause()
    $UnPaused = NOT $UnPaused
    While $UnPaused
        ToolTip("Sending...",0,0)
        $Keypause = Random(1, 5, 1)
        AutoItSetOption("SendKeyDelay", $Keypause)
        $Keypause = Random(1, 5, 1)
        AutoItSetOption("SendKeyDownDelay", $Keypause)
        ; Read the next line of text ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        $Msg1 = FileReadLine($file)
        If @error Then
            ; EOF so close the file and exit
            FileClose($file)
            ExitLoop
        EndIf
        Send("{Enter}/1" & $Msg1 & "{Enter}",1)
        $Delay = Random(5000, 6000, 1)
        Sleep($Delay)
        ;Insert copies HERE of above 5 lines for more messages
    WEnd
EndFunc

Func Terminate()
    Exit
EndFunc

Better? ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thank you for the help with that and the notes. :)

Am a bit rusty at this, so I have to get used to programming again.

Also thank you for the notes they really helped!

Share this post


Link to post
Share on other sites

KingYoshi,

My pleasure. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Thank you for the help with that and the notes. :)

Am a bit rusty at this, so I have to get used to programming again.

Also thank you for the notes they really helped!

Just tried your full version of the code, and it didn't work at all.

After reading through it I thought it would, but when I added the corrections, it now doesn't output anything.

It now can't seem to find the file!? Nothing had changed besides the code, so I am not sure why this is happening.

Disregard the above.. It seems that somehow another file with the same name had been created, and was causing problems.. I am not sure how this is, as 2 files with the same name should not be able to exist within the same folder... (It was probably a different file type)

Edited by KingYoshi

Share this post


Link to post
Share on other sites

KingYoshi,

It worked for me when I tested it. ;)

when I added the corrections

So what does it look like now? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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