FrancescoDiMuro

_FileWriteToLine

15 posts in this topic

Good morning everyone.

I'm new in this big community and I just started using AutoIt. It's a very powerful tool, especially when you neeed to build something that you really need, without a unfriendly editor or something else. I want to say that I'm Italian, so if I do some mistakes, I please you to excuse me. Now, let's talk about my problem with this function. The project I'm working on is very simple. I made a little GUI from which I retrieve 2 texts from 2 input ( of text obviously ), and send them to a file. And here is the problem. I'm using the _FileWriteToLine, but it doesn't write anyting. I open the file in OVERWRITE Mode, so, because I want to store the data always in the same 2 lines. How can I fix it? Thanks to everyone can help me. Have a good day. - Francesco


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

@FrancescoDiMuro welcome to the forum. Please post your code so we can see just what you are doing. It makes it much easier to assist if we don't have to guess at your code ;)

 


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

Share this post


Link to post
Share on other sites

FrancescoDiMuro,

Welcome to the AutoIt forums.

I see a big problem with what you have described - opening the file in OVERWRITE mode will delete the current content and, at present, _FileWriteToLine only writes to existing lines. So it looks as if you are trying to write to a non-existent file and so the lines do not exist. But this works:

#include <File.au3>
#include <MsgBoxConstants.au3>

$sFile = @ScriptDir & "\test.txt"

; Create an empty file with 2 lines
FileWrite($sFile, @CRLF & @CRLF & @CRLF)

; Write to the lines
_FileWriteToLine($sFile, 1, "Line 1", True)
_FileWriteToLine($sFile, 2, "Line 2", True)
; And her they are
$sContent = FileRead($sFile)
MsgBox($MB_SYSTEMMODAL, "First", $sContent)

; And again
_FileWriteToLine($sFile, 1, "New 1", True)
_FileWriteToLine($sFile, 2, "New 2", True)
$sContent = FileRead($sFile)
MsgBox($MB_SYSTEMMODAL, "Second", $sContent)

Please ask if you have any questions.

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
14 minutes ago, JLogan3o13 said:

@FrancescoDiMuro welcome to the forum. Please post your code so we can see just what you are doing. It makes it much easier to assist if we don't have to guess at your code ;)

 

 

10 minutes ago, Melba23 said:

FrancescoDiMuro,

Welcome to the AutoIt forums.

I see a big problem with what you have described - opening the file in OVERWRITE mode will delete the current content and, at present, _FileWriteToLine only writes to existing lines. So it looks as if you are trying to write to a non-existent file and so the lines do not exist. But this works:

#include <File.au3>
#include <MsgBoxConstants.au3>

$sFile = @ScriptDir & "\test.txt"

; Create an empty file with 2 lines
FileWrite($sFile, @CRLF & @CRLF & @CRLF)

; Write to the lines
_FileWriteToLine($sFile, 1, "Line 1", True)
_FileWriteToLine($sFile, 2, "Line 2", True)
; And her they are
$sContent = FileRead($sFile)
MsgBox($MB_SYSTEMMODAL, "First", $sContent)

; And again
_FileWriteToLine($sFile, 1, "New 1", True)
_FileWriteToLine($sFile, 2, "New 2", True)
$sContent = FileRead($sFile)
MsgBox($MB_SYSTEMMODAL, "Second", $sContent)

Please ask if you have any questions.

M23

I'm trying to rewrite the code for having more clarity. As I finished, I will post it, so you can tell me if it is correct or it can be improved. Thanks M23 to your answer. In the Help there's no specific about the use of _FileWriteToLine. Thanks a lot. See you in minutes! :)


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

FrancescoDiMuro,

Quote

In the Help there's no specific about the use of _FileWriteToLine

Really? What about this: https://www.autoitscript.com/autoit3/docs/libfunctions/_FileWriteToLine.htm

M23

P.S. When you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - we know what we wrote and it just pads the thread unnecessarily.

 

 


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

Ok M23, sorry for the bad reply. Here is the code... And it still don't work...

#include <File.au3>
#include <FileConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>

; Creazione del Form

$Form = GUICreate("MULTISCREEN",300,130)
Local $Label1 = GUICtrlCreateLabel("Percorso:",10,10,280,20)
Local $Input1 = GUICtrlCreateInput("Incolla il percorso del file che dev'essere lanciato",10,30,280,20)
Local $Label2 = GUICtrlCreateLabel("Istanze:",10,55,280,20)
Local $Input2 = GUICtrlCreateInput("",10,75,40,20)
Local $Label3 = GUICtrlCreateLabel("Scrivi il numero di volte che il programma dev'essere lanciato",52.5,72.5,280,30)
Local $Button1 = GUICtrlCreateButton("Salva",250,100,40,20)
Local $cFile    ; Config File ( configurazione.txt )
Local $rInput1  ; Path del file da aprire ( prova.txt )
Local $rInput2  ; Istanze del file da aprire ( prova.txt )

; Storicizza la posizione ed il nome del file ( configurazione.txt )
Local $cFilePath = "C:\Users\Portatile 60\Documents\Gestione Multischermi\configurazione.txt" ; Config File Path

GUISetState (@SW_SHOW) ; Serve ASSOLUTAMENTE per far mostrare la GUI
While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Button1
            $rInput1 = GUICtrlRead($Input1)
            $rInput2 = GUICtrlRead($Input2)
            ; Apertura del primo file ( configurazione.txt )
            $cFile = FileOpen($cFilePath, 2)
            If $cFile = -1 Then
                MsgBox($MB_ICONERROR,"ERRORE!","Errore durante l'apertura del file.",5)
                Exit
            EndIf
            FileWrite($cFile, @CRLF & @CRLF & @CRLF)
            _FileWriteToLine($cFile, 1, $rInput1, True)
            _FileWriteToLine($cFile, 2, $rInput2, True)
            FileClose($cFile)
            Exit
    EndSwitch
WEnd

 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

By the way, if I just want to read always from the first 2 rows, I could simply do something like this: 

FileWrite($cFile, "Line 1" & @CRLF)
FileWrite($cFile, "Line 2" & @CRLF)

Opening the $cFile in OVERWRITE mode... Am I right? Thanks


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

FrancescoDiMuro,

It works if you code it correctly:

#include <GUIConstantsEx.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>

; Storicizza la posizione ed il nome del file ( configurazione.txt )
Local $sFilePath = @ScriptDir & "\configurazione.txt" ; Config File Path
; If the file does not exist then create it
If Not FileExists($sFilePath) Then
    FileWrite($sFilePath, @CRLF & @CRLF & @CRLF)
EndIf

; Creazione del Form

$Form = GUICreate("MULTISCREEN",300,130)
Local $Label1 = GUICtrlCreateLabel("Percorso:",10,10,280,20)
Local $Input1 = GUICtrlCreateInput("Incolla il percorso del file che dev'essere lanciato",10,30,280,20)
Local $Label2 = GUICtrlCreateLabel("Istanze:",10,55,280,20)
Local $Input2 = GUICtrlCreateInput("",10,75,40,20)
Local $Label3 = GUICtrlCreateLabel("Scrivi il numero di volte che il programma dev'essere lanciato",52.5,72.5,280,30)
Local $Button1 = GUICtrlCreateButton("Salva",250,100,40,20)

Local $rInput1  ; Path del file da aprire ( prova.txt )
Local $rInput2  ; Istanze del file da aprire ( prova.txt )

GUISetState (@SW_SHOW) ; Serve ASSOLUTAMENTE per far mostrare la GUI

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            ExitLoop
        Case $Button1
            $rInput1 = GUICtrlRead($Input1)
            $rInput2 = GUICtrlRead($Input2)

            ; This is what deletes the file each time <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
            ; Apertura del primo file ( configurazione.txt )
            ;$cFile = FileOpen($sFilePath, 2)
            ;If $cFile = -1 Then
            ;    MsgBox($MB_ICONERROR,"ERRORE!","Errore durante l'apertura del file.",5)
            ;   Exit
            ;EndIf

            _FileWriteToLine($sFilePath, 1, $rInput1, True)
            _FileWriteToLine($sFilePath, 2, $rInput2, True)

            ;FileClose($cFile)

            $sContent = FileRead($sFilePath)
            MsgBox($MB_SYSTEMMODAL, "Content", $sContent)
    EndSwitch
WEnd

All clear 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

It is being clear, but so, in which cases FileOpen is used? For example, if I have a file in which I store some things, I can open it in Write mode, so, it keeps adding things at the end of the file. But if I have to do something like I posted, the better way is to "recreate" the file if it doesn't exist, and If it does exist, just keep going with the script... Am I right? Thanks M23 for your instructive answers :)


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

I'm defenitely not understanding why in that way it works... Sorry! 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

FrancescoDiMuro,

Quote

in which cases FileOpen is used?

Many AutoIt file functions (such as FileWrite) offer the choice of either a file path (a string) or file handle (the return from FileOpen) to identify the file. If you intend to use the function intensively it is always best to use the handle as if given a path AutoIt opens and closes the file on each call which can slow down execution considerably.

However, _FileWriteToLine only accepts a file path and so there is no point in opening the file first - and particularly not in OVERWRITE mode as that deletes the current file which, as I explained above, means that there are no lines to change.

If you want to save several "things" in a file, you might want to look at ini files. AutoIt has several Ini* functions which allow you to read and write items very easily without having to worry about creating or opening the file itself - the Help file (which should always be your first port of call) explains how to use them.

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 would like to retrieve the name of a file by a C:\Users\Portatile 60\Documents\prova.txt, and using it... How can I do it? Thanks to all. I still waiting your response M23. 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

Maybe I managed... Thanks all for the answers.


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

Share this post


Link to post
Share on other sites

FrancescoDiMuro,

I have already explained twice and I am not sure how I can make it clearer, but I will try.

If you open a file in OVERWRITE mode you destroy the existing file, which means that you are left with an empty file. _FileWriteLine needs an existing line or it fails - if the file is empty there are by definition no lines so it always fails. Besides, that function does not take a file handle (the return value from FileOpen) as a parameter, but only a file name (a literal string), so there is no point in opening the file in any case.

The script I posted checks if the file already exists and, if not, creates a new file with 2 empty lines - not an empty file as it contains the @CRLF EOLs thus creating the lines. Now _FileWriteToLine will work as it has suitable lines within the file on which to operate - and as it opens and closes the file automatically behind the scenes, there is no need for you to do so.

Nothing new there - I can only hope that it is clearer the third time. Now I am off to a concert and will not be able to reply until tomorrow.

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 your 2nd reply M23. Yeah, now it's clearer. Thank you for have dedicated some of your time with me. Have a good time at the concert! Bye! :D

 


Click here to see my signature:

Spoiler

I will always thank you for the time you spent for me.
I'm here to ask, and from your response, I'd like to learn.
By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

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