Jump to content

Delete Name From Txt File


step
 Share

Recommended Posts

In a nutshell i want to input a name which will then (if found) be removed from a txt file

But i cant get this to work!

So far i have.....

Func DeleteStaffMember()

$DelUser = InputBox("CSTS - Delete User", "Enter Name To Delete.", "")

If $DelUser = "" Then

Return

Endif

If Not _FileReadToArray(UserList.txt,$DELemployee) Then

MsgBox(4096,"Error", " Error reading Employee List File to Array error:" & @error)

Exit

EndIf

For $count = 1 To $DELemployee[0]

If $DelUser = $DELemployee[$count ] then

_ArrayDelete( $DELemployee,$count)

;HOW can i exit the for loop here!!??

EndIf

Next

_ArrayDisplay( $DELemployee, "Removed entry " )

;I need to write the new array to UserList.txt here!!

endFunc

Link to comment
Share on other sites

Ahh... simple:

Func DeleteStaffMember()

   $DelUser = InputBox("CSTS - Delete User", "Enter Name To Delete.", "")

   If $DelUser = "" Then Return

   If Not _FileReadToArray("UserList.txt",$DELemployee) Then
      MsgBox(4096,"Error", " Error reading Employee List File to Array error:" & @error)
      Exit
   EndIf


   For $count = 1 To $DELemployee[0]
      If $DelUser = $DELemployee[$count ] then
         _ArrayDelete( $DELemployee,$count)
         ExitLoop 1
      EndIf
   Next
   _ArrayDisplay( $DELemployee, "Removed entry " )
   _FileWriteFromArray("UserList.txt",$DELemployee, 1)
endFunc

Read the documentation!!!

#)

EDIT: forgot to mention, requires beta, for the UDF _FileWriteFromArray().

Edited by nfwu
Link to comment
Share on other sites

  • Moderators

@step

Is that all you have in that file, is employee names? Is each name on a seperate line?

Here's my attempt at it... will still need beta for StringRegExp(), but you won't need to put everything in an array and should be much faster if the file is large:

Func DeleteStaffMember()
    Local $DelUser = InputBox("CSTS - Delete User", "Enter Name To Delete.", "")
    If Not $DelUser <> "" Then Return 0
    
    Local $FilePathToRead = 'File Path To What You Want To read'
    Local $DELemployee = FileRead($FilePathToRead, FileGetSize($FilePathToRead))
    Local $File_Open = FileOpen($FilePathToRead, 2)
    FileWrite($FilePathToRead, StringRegExpReplace($DELemployee, '(?i:' & $DelUser & ')', ''))
    FileClose($File_Open)
EndFunc
It's tested and does work... Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Boredom overtook me, and I thought I'd write how I would approach this if removing an employee from a file... replace the @DesktopDir & '\TXT_Files\testing.txt' with whatever file location you have yours at:

DeleteStaffMember()
Func DeleteStaffMember()
    Local $FilePathToRead = @DesktopDir & '\TXT_Files\testing.txt'
    Local $Delete_Staff_GUI = GUICreate('Remove Staff Member', 200, 85)
    Local $Delete_Staff_Label = GUICtrlCreateLabel('Choose which staff member to delete', 0, 5, 200, 20, 1)
    Local $Delete_Staff_Combo = GUICtrlCreateCombo('', 10, 25, 180, 100)
    Local $Delete_Staff_Button = GUICtrlCreateButton('Remove', 70, 50, 60, 30, 0x0001)
    Local $PopulateGUICombo = PopulateStaffGUICombo($FilePathToRead)
    Local $1stEntryGUIComb = StringSplit($PopulateGUICombo, '|')
    If IsArray($1stEntryGUIComb) Then
        GUICtrlSetData($Delete_Staff_Combo, $PopulateGUICombo, $1stEntryGUIComb[1])
    Else
        GUICtrlSetData($Delete_Staff_Combo, $PopulateGUICombo)
    EndIf
    GUISetState()
    While 1
        $DS_MSG = GUIGetMsg()
        Select 
            Case $DS_MSG = - 3
                ExitLoop
            Case $DS_MSG = $Delete_Staff_Button
                $DS_COMBOREAD = GUICtrlRead($Delete_Staff_Combo)
                If MsgBox(36, 'Confirm', 'Are you sure you want to remove: ' & $DS_COMBOREAD & '?') == 6 Then
                    Local $DELemployee = FileRead($FilePathToRead, FileGetSize($FilePathToRead))
                    Local $File_Open = FileOpen($FilePathToRead, 2)
                    Local $ReplaceEmployee = StringRegExpReplace($DELemployee, '(?i:' & $DS_COMBOREAD & ')', '')
                    Local $aArray = StringSplit(StringTrimRight($ReplaceEmployee, 1), @LF)
                    Local $AddLF = ''
                    For $iFile = 1 To UBound($aArray) - 1
                        If $iFile < (UBound($aArray) - 1) And StringStripWS($aArray[$iFile], 7) <> '' Then 
                            $AddLF = $AddLF & $aArray[$iFile] & @LF
                        ElseIf $iFile == (UBound($aArray) - 1) And StringStripWS($aArray[$iFile], 7) <> '' Then
                            $AddLF = $AddLF & $aArray[$iFile]
                        EndIf
                    Next
                    FileWrite($FilePathToRead, $AddLF)
                    FileClose($File_Open)
                    ExitLoop
                EndIf
        EndSelect
    WEnd
    GUIDelete($Delete_Staff_GUI)
EndFunc

Func PopulateStaffGUICombo($FilePathToRead)
    Local $File_Open = ''
    Local $TempArray = ''
    $File_Open = FileOpen($FilePathToRead, 0)
    $aArray = StringSplit(StringStripCR(FileRead($File_Open, FileGetSize($FilePathToRead))), @LF)
    FileClose($File_Open)
    For $iFile = 1 To UBound($aArray) - 1
        If $aArray[$iFile] <> '' Then $TempArray = $TempArray & $aArray[$iFile] & '|'
    Next
    Return StringTrimRight($TempArray, 1)
EndFunc
If you can't follow it let me know, and I'll break it down.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I downloaded the beta and still cant get it to work..your code looks fine...so its probably just me..

I am as thick as sh!te.

In the mean time i put this together...

Func DeleteStaffMember()

$DelUser = InputBox("CSTS - Delete User", "Enter Name To Delete.", "")

If $DelUser = "" Then Return

$tmpfile = FileOpen("tmpUsers.txt", 2)

; Check if file opened for writing OK

If $tmpfile = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

$file = FileOpen("users.txt", 0)

; Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

; Read in lines of text until the EOF is reached

While 1

$line = FileReadLine($file)

;$line2 = StringStripCR($line)

If @error = -1 Then ExitLoop

If $DelUser <> $line Then

FileWriteLine($tmpfile, $line)

;MsgBox(0, "Line read:", $line)

EndIf

Wend

FileClose($file)

FileClose($tmpfile)

EndFunc

Link to comment
Share on other sites

  • Moderators

Hmmm, all you had to do with the last one I wrote was replace the file location? It did the rest for you,.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Ahh... simple:

Func DeleteStaffMember()

   $DelUser = InputBox("CSTS - Delete User", "Enter Name To Delete.", "")

   If $DelUser = "" Then Return

   If Not _FileReadToArray("UserList.txt",$DELemployee) Then
      MsgBox(4096,"Error", " Error reading Employee List File to Array error:" & @error)
      Exit
   EndIf
   For $count = 1 To $DELemployee[0]
      If $DelUser = $DELemployee[$count ] then
         _ArrayDelete( $DELemployee,$count)
         ExitLoop 1
      EndIf
   Next
   _ArrayDisplay( $DELemployee, "Removed entry " )
   _FileWriteFromArray("UserList.txt",$DELemployee, 1)
endFunc

Read the documentation!!!

#)

EDIT: forgot to mention, requires beta, for the UDF _FileWriteFromArray().

This works well but it leaves a blank line ......the other code you put together looks pretty cool!...

Ive just printed it and am about to sit somewhere very quietly and work my way thru it.

many thanks

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...