Skeletor Posted June 10, 2021 Share Posted June 10, 2021 (edited) Right everyone, I'm back.. but lost again... I got this example code from M32.. but I want it to append to a specific line in the text... I'm doing something wrong..any help will really .. er.. help... expandcollapse popup#include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <File.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> ; Create file in same folder as script $sFileName = @ScriptDir &"\Test.txt" ; Open file - deleting any existing content $hFilehandle = FileOpen($sFileName, $FO_OVERWRITE) ; Prove it exists If FileExists($sFileName) Then MsgBox($MB_SYSTEMMODAL, "File", "Exists") Else MsgBox($MB_SYSTEMMODAL, "File", "Does not exist") EndIf ; Write a line FileWrite($hFilehandle, "This is line 1" & @CRLF) FileWrite($hFilehandle, "This is line 2") ; Read it MsgBox($MB_SYSTEMMODAL, "File Content", FileRead($sFileName)) ; Append a line _FileWriteToLine($hFilehandle, 1, " and I have appended to line 1.", False) ; read it MsgBox($MB_SYSTEMMODAL, "File Content", FileRead($sFileName)) ; Close the file handle FileClose($hFilehandle) ; Tidy up by deleting the file FileDelete($sFileName) Edited June 10, 2021 by Skeletor Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI Link to comment Share on other sites More sharing options...
Solution Danyfirex Posted June 10, 2021 Solution Share Posted June 10, 2021 Hello It seems to be It append at the beginning of the line. I just modify the code to append at the end. expandcollapse popup#include <FileConstants.au3> #include <MsgBoxConstants.au3> #include <File.au3> #include <MsgBoxConstants.au3> #include <WinAPIFiles.au3> ; Create file in same folder as script $sFileName = @ScriptDir &"\Test.txt" ; Open file - deleting any existing content $hFilehandle = FileOpen($sFileName, $FO_OVERWRITE) ; Prove it exists If FileExists($sFileName) Then MsgBox($MB_SYSTEMMODAL, "File", "Exists") Else MsgBox($MB_SYSTEMMODAL, "File", "Does not exist") EndIf ; Write a line FileWrite($hFilehandle, "This is line 1" & @CRLF) FileWrite($hFilehandle, "This is line 2") FileClose($hFilehandle) ; Read it MsgBox($MB_SYSTEMMODAL, "File Content", FileRead($sFileName)) ; Append a line _FileWriteToLine2($sFileName, 1, " and I have appended to line 1.",False) ; read it MsgBox($MB_SYSTEMMODAL, "File Content", FileRead($sFileName)) Func _FileWriteToLine2($sFilePath, $iLine, $sText, $bOverWrite = False, $bFill = False) If $bOverWrite = Default Then $bOverWrite = False If $bFill = Default Then $bFill = False If Not FileExists($sFilePath) Then Return SetError(2, 0, 0) If $iLine <= 0 Then Return SetError(4, 0, 0) If Not (IsBool($bOverWrite) Or $bOverWrite = 0 Or $bOverWrite = 1) Then Return SetError(5, 0, 0) If Not IsString($sText) Then $sText = String($sText) If $sText = "" Then Return SetError(6, 0, 0) EndIf If Not IsBool($bFill) Then Return SetError(7, 0, 0) ; Read current file into array Local $aArray = FileReadToArray($sFilePath) ; Create empty array if empty file If @error Then Local $aArray[0] Local $iUBound = UBound($aArray) - 1 ; If Fill option set If $bFill Then ; If required resize array to allow line to be written If $iUBound < $iLine Then ReDim $aArray[$iLine] $iUBound = $iLine - 1 EndIf Else If ($iUBound + 1) < $iLine Then Return SetError(1, 0, 0) EndIf ; Write specific line - array is 0-based so reduce by 1 - and either replace or insert $aArray[$iLine - 1] = ($bOverWrite ? $sText : $aArray[$iLine - 1] & $sText) ; Concatenate array elements Local $sData = "" For $i = 0 To $iUBound $sData &= $aArray[$i] & @CRLF Next $sData = StringTrimRight($sData, StringLen(@CRLF)) ; Required to strip trailing EOL ; Write data to file Local $hFileOpen = FileOpen($sFilePath, FileGetEncoding($sFilePath) + $FO_OVERWRITE) If $hFileOpen = -1 Then Return SetError(3, 0, 0) FileWrite($hFileOpen, $sData) FileClose($hFileOpen) Return 1 EndFunc ;==>_FileWriteToLine PD: _FileWriteToLine need a file path. not file handle. Skeletor 1 Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
Developers Jos Posted June 10, 2021 Developers Share Posted June 10, 2021 Where in the helpfile does it state you can use a FileHandle of an already opened file for _FileWriteToLine()? Just use the original filename! Jos Skeletor 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Luke94 Posted June 10, 2021 Share Posted June 10, 2021 If you change this line: Quote _FileWriteToLine($hFilehandle, 1, " and I have appended to line 1.", False) To: Quote _FileWriteToLine($sFileName, 1, " and I have appended to line 1.", False) It works as intended, inserting the line. However I'm guessing you want to append text to the end of the text already on that line? Skeletor 1 Link to comment Share on other sites More sharing options...
Skeletor Posted June 10, 2021 Author Share Posted June 10, 2021 2 minutes ago, Jos said: Where in the helpfile does it state you can use a FileHandle of an already opened file for _FileWriteToLine()? Just use the original filename! Jos Haha, as mentioned, copied from MelbaM32... haha... Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI Link to comment Share on other sites More sharing options...
Skeletor Posted June 10, 2021 Author Share Posted June 10, 2021 @Danyfirex Thanks for that solution. I see you created a function to take care of this.. is this not native to AutoIt? Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 10, 2021 Moderators Share Posted June 10, 2021 Skeletor, Quote as mentioned, copied from MelbaM32 You copied the main part of the script from this thread: https://www.autoitscript.com/forum/topic/179511-write-or-append-to-txt-file/?do=findComment&comment=1288191 But the _FileWriteToLine line is all your own - so do not try to blame me! M23 Skeletor 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Skeletor Posted June 10, 2021 Author Share Posted June 10, 2021 (edited) HAHA, you still the same after many years Melba.. haha.... Edited June 10, 2021 by Skeletor Kind RegardsSkeletor "Coffee: my defense against going postal." Microsoft Office Splash Screen | Basic Notepad Program (Beginner) | Transparent Splash Screen | Full Screen UI Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now