grimmlock Posted March 26, 2013 Posted March 26, 2013 So I am using this code: Local $file = FileOpen("test.txt", 1) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf FileWrite($file, "Line1") FileWrite($file, "Still Line1" & @CRLF) FileWrite($file, "Line2") FileClose($file) However I would like to add a unique number to the beginning, so that the result would be something like: test.txt id 1: line id 2: line 2 id 3: line 3 etc Any ideas? Thanks in advance Grimm Thanks Grimm
Moderators Melba23 Posted March 26, 2013 Moderators Posted March 26, 2013 (edited) grimmlock, How about this - just call the function at the beginning of each line: Local $file = FileOpen("test.txt", 1) ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf FileWrite($file, _LineID() & "Line1") FileWrite($file, "Still Line1" & @CRLF) FileWrite($file, _LineID() & "Line2") FileClose($file) Func _LineID() Static $iCount = 0 $iCount += 1 Return "id" & StringFormat("%04i", $iCount) & ": " EndFunc Any use? M23 Edited March 26, 2013 by Melba23 Fixed tags 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
grimmlock Posted March 26, 2013 Author Posted March 26, 2013 Thank you Melba23, Great! Ok so I close the app and reopen it, then run it. It puts the unique id however it starts at 1 again. Is there a way to have the function look at the previous id and then add 1 to it? Meaning I open the app and create a line 1 with id001. Then I close the app and reopen it and add something else, right now it adds id001 again but can it be setup to see that it already has a id001 so it makes it id002, and so on? Thanks Grimm Thanks Grimm
Moderators Melba23 Posted March 26, 2013 Moderators Posted March 26, 2013 grimmlock, Fun little task that: expandcollapse popup#include <File.au3> Local $file = FileOpen("test.txt", 1) Local $aLines, $iCount = 0 ; Check if file opened for writing OK If $file = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf ; Read the file _FileReadToArray("Test.txt", $aLines) If Not @error Then ; if the file exists look for the ID starting on the last line For $i = $aLines[0] To 1 Step -1 If StringLeft($aLines[$i], 2) == "id" Then ; get the ID number $iCount = Int(StringRegExpReplace($aLines[$i], "id(\d*):.*", "$1")) ExitLoop EndIf Next ; Check we found an ID If $i = 0 Then MsgBox(0, "Error", "No line id found") Exit EndIf EndIf FileWrite($file, _LineID() & "LineA") FileWrite($file, "Still LineA" & @CRLF) FileWrite($file, _LineID() & "LineB" & @CRLF) FileClose($file) Func _LineID() $iCount += 1 Return "id" & StringFormat("%04i", $iCount) & ": " EndFunc All clear? M23 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
grimmlock Posted March 26, 2013 Author Posted March 26, 2013 Melba you are the best!!!! Thanks! Grimm Thanks Grimm
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