Jump to content

Save Text from file to variable


Recommended Posts

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.

Link to comment
Share on other sites

  • Moderators

KingYoshi,

FileReadLine sounds like the function you need. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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 ~ ~?

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

KingYoshi,

It happens! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
Share on other sites

  • Moderators

KingYoshi,

My pleasure. :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

Link to comment
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!

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

  • Moderators

KingYoshi,

It worked for me when I tested it. ;)

when I added the corrections

So what does it look like now? :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see 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

 

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