Jump to content

adding unique id to text file


 Share

Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

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 by Melba23
Fixed tags

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

Link to comment
Share on other sites

  • Moderators

grimmlock,

Fun little task that: :D

#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

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

×
×
  • Create New...