Sign in to follow this  
Followers 0
dav360

FileWriteLine

3 posts in this topic

Hello Everyone,

I have a simple script that I am working on for the office. I have a license key manager for Microsoft Office 2003 and 2007. I have a combo box that when you select either office 2003 or 2007, it updates the listbox. The listbox contains all the license keys for either Office 2003 or 2007, depending on which you select in the combobox. The license keys are read from a text file. There are 2 text files, one named Office2003.txt and another named Office2007.txt. I also have an input box where you can input a license key. When you press "add key" the program writes the key to the text file. There is a problem however. When I manually go into the text file to remove a test key, restart the license key manager, and try to add a key, the program sometimes writes the key to the same line as the last line in the text file, putting 2 keys on the same line. I was wondering if there was a way to fix this issue. Also, I am trying to expand the license key manager to all licensed software we have. Is there a way to script the combo box so that it looks for all the text files in its current directory and adds an for each of the names, and, once this is done, the listbox would pull the keys from the text file and write a key if you use the add key option?

Thanks for all your help

#include <GuiConstants.au3>
#include <GUIConstantsEx.au3>
#include <ComboConstants.au3>
#Include <GuiComboBox.au3>
#include <File.au3>



$GUI_Handle = GuiCreate("License Key Manager", 400, 400,(@DesktopWidth-400)/2, (@DesktopHeight-400)/2)

$Label_1 = GuiCtrlCreateLabel("Choose Program", 50, 10, 230, 30)
GUICtrlSetFont($Label_1,20,"bold",1)
GUISetState()
$List_2 = GuiCtrlCreateList("", 20, 100, 350, 123)
$Combo_3 = GuiCtrlCreateCombo("", 20, 50, 350, 21,$CBS_DROPDOWNLIST)
GUICtrlSetData(-1,"Office 2003|Office 2007")
GUISetState() 
$Input_4 = GuiCtrlCreateInput("", 20, 350, 300, 20)
$Label_5 = GuiCtrlCreateLabel("Add Key", 30, 300, 310, 30)
GUICtrlSetFont($Label_5,20,"bold",1)
GUISetState()
$Button_6 = GuiCtrlCreateButton("Add", 320, 350, 70, 20)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $Combo_3
        If GUICtrlRead($Combo_3) = "Office 2003" Then
            GUICtrlSetData($List_2, "")
            GUISetState() 
            $file03 =FileOpen("Office2003.txt",0)
            $lineCount03 = _FileCountLines("Office2003.txt")
            $lineNum =1
            Local $line[$lineCount03 + 1]
            For $lineNum =1 To $lineCount03
                $line[$lineNum] =(FileReadLine($file03,$lineNum))
                GUICtrlSetData($List_2, $line[$lineNum])
                GUISetState() 
            Next
            
        ElseIf GUICtrlRead($Combo_3) = "Office 2007" Then
            GUICtrlSetData($List_2, "")
            GUISetState() 
            $file07 =FileOpen("Office2007.txt",0)
            $lineCount07 = _FileCountLines("Office2007.txt")
            $lineNum =1
            Local $line[$lineCount07 + 1]
            For $lineNum =1 To $lineCount07
                $line[$lineNum] =(FileReadLine($file07,$lineNum))
                GUICtrlSetData($List_2, $line[$lineNum])
                GUISetState()
            Next
        EndIf
    Case $msg = $Button_6
        If GUICtrlRead($Combo_3) = "Office 2003" Then
            FileClose($file03)
            $file03= FileOpen("Office2003.txt",1)
            $addKey =GUICtrlRead($Input_4)
            FileWriteLine($file03,$addKey)
            FileClose($file03)
            GUICtrlSetData($Input_4,"")
            GUISetState() 
            
            GUICtrlSetData($List_2, "")
            GUISetState() 
            $file03 =FileOpen("Office2003.txt",0)
            $lineCount03 = _FileCountLines("Office2003.txt")
            $lineNum =1
            Local $line[$lineCount03 + 1]
            For $lineNum =1 To $lineCount03
                $line[$lineNum] =(FileReadLine($file03,$lineNum))
                GUICtrlSetData($List_2, $line[$lineNum])
                GUISetState() 
            Next
            
        ElseIf GUICtrlRead($Combo_3) = "Office 2007" Then
            FileClose($file07)
            $file07= FileOpen("Office2007.txt",1)
            $addKey =GUICtrlRead($Input_4)
            FileWriteLine($file07,$addKey)
            FileClose($file07)
            GUICtrlSetData($Input_4,"")
            GUISetState()
            
            GUICtrlSetData($List_2, "")
            GUISetState() 
            $file07 =FileOpen("Office2007.txt",0)
            $lineCount07 = _FileCountLines("Office2007.txt")
            $lineNum =1
            Local $line[$lineCount07 + 1]
            For $lineNum =1 To $lineCount07
                $line[$lineNum] =(FileReadLine($file07,$lineNum))
                GUICtrlSetData($List_2, $line[$lineNum])
                GUISetState() 
            Next

        EndIf
    EndSelect
WEnd

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi,

i have had the same problem with adding values to a textfile and delete some test items manually

.

If you delete items in a text file, always be sure, that after the last item is a CarriageReturn. Then Filewrite will write

as you want. If you have no CR, the 1.st FileWrite will add the string straight after the last item and not in next line.

;-))

Stefan

P.S: I'm not pretty sure, because it was some time ago. You might try it the other way round -> no CR after last item.

Edited by 99ojo

Share this post


Link to post
Share on other sites

Thanks for your help. I will give it a try

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
Sign in to follow this  
Followers 0