Sign in to follow this  
Followers 0
Snarg

Removing Text From A List

27 posts in this topic

#1 ·  Posted (edited)

Here is what I am attempting to do. I have a text file with a list of names in it. I have an .ini file that contains the number of names listed in the text file. I would like the user to be able to push a button on the keyboard and input a name to be removed. The name needs to be removed from the text file and the .ini file updated with the current ammount of names.

I know to you super programers this is easy but I am losing hair here :)

This is what I have:

Func DelName ()

Local $A, $I, $File, $AddName

$NumberOfNames = IniRead ($SettingsFile, 'Settings', 'NumberOfNames', "ERROR")

  $RemName = InputBox ("Input Name", "Input Name", "")

    $FileHandle = FileOpen ("NameList.txt", 1)

      If $File = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
      EndIf

    $A = 1
      For $I = 1 to $NumberOfNames
        $Name = FileReadLine ($FileHandle, $A)

          If $Name = $RemName Then
            FileWriteLine ($FileHandle, '{DEL}' & '{BS}')
          EndIf

    $A = $A + 1
      Next

  FileWriteLine ($FileHandle, @LF & $AddName)
  FileClose ($FileHandle)

$NumberOfNames = $NumberOfNames - 1
IniWrite ($SettingsFile, 'Settings', 'NumberOfNames', $NumberOfNames)

EndFunc
Edited by Snarg

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Normally I would just rewrite the text file.

Here is a simple example of finding an exact name, removing it, and showing the status. You can easily add in your code for updating the count.

Names can be tough due to matching it properly, you may contemplate a popup once it finds it with a StringInstr() funtion as well, so they can use part of the name, and a confirmation box.

$outname = InputBox("Question", "Remove what Name?", "", "", -1, -1, 0, 0)
$outcode=$outname &" Not Found"
$count=0
$file = FileOpen("newnames.txt", 0)
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf
$file2 = FileOpen("testout.txt", 2)

; Check if file opened for writing OK
If $file2 = -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)
    If @error = -1 Then ExitLoop
    If $line<>$outname Then 
     FileWriteLine($file2, $line)
    $count=$count+1
Else
    $outcode=$outname &" Found and Removed"
EndIf
Wend
FileClose($file)
FileClose($file2)
FileCopy("testout.txt","newnames.txt",1)
MsgBox(1,$count&" Names in the file",$outcode)
Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

scriptkitty, thank you very much for your help. This is what I have come up with. One slight problem still remains though. The new file has a blank line at the end. What I mean is, the cursor starts out under the last entry. If a new name is added to the list, there is a gap between the names. This causes the reading of the names to fail. Not all the names are read due to the blank line. Any ideas on how to solve that would be great.

On a side note, if a user cancesl a dialogue box, how do you exit out of the function?

Func DelName ()

Local $A, $I, $File, $Line

$NumberOfNames = IniRead ($SettingsFile, 'Settings', 'NumberOfNames', "ERROR")

  $RemName = InputBox ("Delete Name", "Enter the name to be deleted:", "")

    $Count = 0

      $FileHandle = FileOpen ("NameList.txt", 0)

        If $File = -1 Then
          MsgBox (0, "Error", "Unable to open file.")
          Exit
        EndIf

      $File2 = FileOpen ("TempNameList.txt", 2)

        If $File2 = -1 Then
          MsgBox (0, "Error", "Unable to open file.")
          Exit
        EndIf

    While 1

      $SearchLine = FileReadLine ($FileHandle)

        If @error = -1 Then ExitLoop
        If $SearchLine <> $RemName Then

          FileWriteLine ($File2, $SearchLine & @LF)

    $Count = $Count + 1

        EndIf

    Wend

FileClose ($FileHandle)
FileClose ($File2)
FileCopy ("TempNameList.txt", "NameList.txt", 1)
FileDelete ("TempNameList.txt")

$NumberOfNames = $NumberOfNames - 1

IniWrite ($SettingsFile, 'Settings', 'NumberOfNames', $NumberOfNames)
EndFunc

A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

Change the FileWriteLine to FileWrite statement when you are using 3.0.94 or better, or remove the @LF ... either way should work for you.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Just to throw in a kink in I might use this.

Func DelName ()

     Local $File

     $NumberOfNames = IniRead($SettingsFile, 'Settings', 'NumberOfNames', "ERROR")

     $RemName = InputBox ("Delete Name", "Enter the name to be deleted:", "")

     RunWait(@Comspec & ' /c find /V /I "' & $RemName '" ' & $File ' > ' & @Temp & '\TempOutput.txt',"",@SW_Hide)

     FileMove(@Temp & '\TempOutput.txt',$File,1)

     $NumberOfNames = $NumberOfNames - 1

     IniWrite ($SettingsFile, 'Settings', 'NumberOfNames', $NumberOfNames)
EndFunc

This should work

red

Edit: Made a fix in the code there was missing an &

Edited by redndahead

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

my sloppy code would look like this:

Func DelName ()

local $A, $I, $File, $Line
$NumberOfNames = IniRead ($SettingsFile, 'Settings', 'NumberOfNames', "ERROR")

$RemName = InputBox ("Delete Name", "Enter the name to be deleted:", "")

$Count = 0
     $FileHandle = FileOpen ("NameList.txt", 0)
       If $File = -1 Then
         MsgBox (0, "Error", "Unable to open file.")
         Exit
       EndIf

     $File2 = FileOpen ("TempNameList.txt", 2)

       If $File2 = -1 Then
         MsgBox (0, "Error", "Unable to open file.")
         Exit
       EndIf

   While 1

     $SearchLine = FileReadLine ($FileHandle)

       If @error = -1 Then ExitLoop
If $SearchLine<>"" Then;blankline
If $SearchLine <> $RemName Then
    If $count=0 Then 
  FileWrite ($File2,$SearchLine)
    Else
  FileWrite ($File2, @CRLF&$SearchLine)
    EndIf
$count=$count+1
Else
    $out=MsgBox(4,$SearchLine&" found","Delete?")
    If $out=7 Then
     If $count=0 Then 
  FileWrite ($File2,$SearchLine)
    Else
  FileWrite ($File2, @CRLF&$SearchLine)
    EndIf
$count=$count+1
EndIf
EndIf
EndIf;blankline

   Wend

FileClose ($FileHandle)
FileClose ($File2)
FileCopy ("TempNameList.txt", "NameList.txt", 1)
FileDelete ("TempNameList.txt")

$NumberOfNames = $Count
IniWrite ($SettingsFile, 'Settings', 'NumberOfNames', $NumberOfNames)
EndFunc

It will remove blank lines for you as well.

edit.. Oh yea, I had added the confirmation box, No will cause it not to remove the name, and this one will update your ini file, if they added a few names, but forgot to update. I use the $count to count how many lines.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

On a side note, if a user cancesl a dialogue box, how do you exit out of the function?

; ......

$RemName = InputBox ("Delete Name", "Enter the name to be deleted:", "")

If @error Then Return

; ......

Try something like that. The error flag is set if the user clicks Cancel on an inputbox; and you use the "Return" statement to exit from a user-defined function.


Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

This one adds scriptkitty's idea of getting the number of names from the file.

Func DelName ()

    Local $File

    $RemName = InputBox ("Delete Name", "Enter the name to be deleted:", "")
     If @Error Then Return(0)

    RunWait(@Comspec & ' /c find /V /I "' & $RemName '" ' & $File & ' > ' & @Temp & '\TempOutput.txt',"",@SW_Hide)

    RunWait(@Comspec & ' /c find /C /I "ajdjakflj" ' & @Temp & '\TempOutput.txt > ' & @Temp & 'TempOutput2.txt',"",@SW_Hide)

    $FileHandle = FileOpen(@Temp & '\TempOutput2.txt',0)

    $ReadLine = FileReadLine($FileHandle,2)

    FileClose($FileHandle)

    FileDelete(@Temp & '\TempOutput2.txt')

    $SplitValue = StringSplit($Readline,":")

    $NumberOfNames = Number($SplitValue[2])

    FileMove(@Temp & '\TempOutput.txt',$File,1)

    IniWrite ($SettingsFile, 'Settings', 'NumberOfNames', $NumberOfNames)
    
    Return(1)
EndFunc

red

Edited by redndahead

Share this post


Link to post
Share on other sites

Wow! Thank you for all the help and ideas. Ummm...does this mean I have an old version? How do you tell what version you have?

Posted Image


A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

Change the @Temp to @TempDir


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

I like your script Red, but you still need to remove the junk find puts in the front of the file:

---------- NAMELIST.TXT

Sometimes I don't like all the junk that Dos commands add to the files. :whistle:

Oh yea, and I must have missed a quote in my copy/paste cause it errors a bit with when I tried your code.


AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

Changed it like GEO said. Now I get an error in expression.

RunWait(@Comspec & ' /c find /V /I "' & $RemName '" ' & $File & ' > ' & @TempDir & '\TempOutput.txt',"",@SW_Hide)

It points to the '@Comspec' part.


A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

oh yea, forgot a & by the looks of it:

RunWait(@Comspec & ' /c find /V /I "' & $RemName '" ' & $File & ' > ' & @Temp & '\TempOutput.txt',"",@SW_Hide)

maybe would work better as:

RunWait(@Comspec & ' /c find /V /I "' & $RemName &'" ' & $File & ' > ' & @TempDir & '\TempOutput.txt',"",@SW_Hide)

I do this constantly. Or consistantly. Hard to tell the difference. :whistle:

edit... It points only to the line, since @comspec is the first part of it, it kinda looks that way at times.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

Ok, it did 'something'. I guess I need to figure out where to add the correct file names now.

Once again, thank you all for all the help.


A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

First yes it is missing the & sign again. grrr. :whistle:

Second I don't have to parse the first part because I stringsplit it using the ":" and then only use the last Split item.

This part here

$SplitValue = StringSplit($Readline,":")

$NumberOfNames = Number($SplitValue[2])

red

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

if you want to see the output, try this:

$RemName="Joes"
$file="NameList.txt"
RunWait(@Comspec & ' /c find /V /I "' & $RemName &'" ' & $File & ' > ' &  'TempOutput.txt',"",@SW_Hide)

I still like my sloppy all AutoIt code. :whistle:

DOS tends to add more CR and extra stuff in front as well that you will need to remove, depending on windows version it might change as well, not really sure on that.

edit.. I may have missed where Red was removing the "---------- NAMELIST.TXT" and extra CR in the standard output, the count worked great. Of course this could just be a WinXP thing that it adds it to.

Red usually does what takes me 100 lines in about 10, and Larry does it in one.

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

scriptkitty, I have yours (mostly) working.

No offense red, but what you wrote is WAY beyond my level of understanding. I'm still trying to work my way though all of kitty's If statements...


A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

This may help understanding my Spagettii

DelName()
Func DelName ()

local $A, $I, $File, $Line
$NumberOfNames = IniRead ($SettingsFile, 'Settings', 'NumberOfNames', "ERROR")

$RemName = InputBox ("Delete Name", "Enter the name to be deleted:", "")

$Count = 0
$FileHandle = FileOpen ("NameList.txt", 0)
$File2 = FileOpen ("TempNameList.txt", 2)
If $FileHandle = -1  or $File2= -1 Then
   MsgBox (0, "Error", "Unable to open file.")
   Exit
EndIf

While 1
$SearchLine = FileReadLine ($FileHandle)
If @error = -1 Then ExitLoop

If $SearchLine<>"" Then;blankline removal
    If $SearchLine <> $RemName Then;if not found, just write line
        WriteMyLines($File2,$Searchline,$count)
        $count=$count+1
    Else
        $out=MsgBox(4,$SearchLine&" found","Delete?")
            If $out=7 Then; after asking question, writes line if answer=No, removes line if not.
              WriteMyLines($File2,$Searchline,$count)
              $count=$count+1
            EndIf; after asking question, writes line if answer=No, removes line if not.
    EndIf;if not found, just write line
EndIf;blankline removal

Wend

FileClose ($FileHandle)
FileClose ($File2)
FileCopy ("TempNameList.txt", "NameList.txt", 1)
FileDelete ("TempNameList.txt")

$NumberOfNames = $Count
MsgBox(1,$count,$NumberOfNames)
IniWrite ($SettingsFile, 'Settings', 'NumberOfNames', $NumberOfNames)
EndFunc

Func WriteMyLines($File2,$Searchline,$count)
   If $count=0 Then; first line doesn't put a @crlf in front.
       FileWrite ($File2,$SearchLine)
   Else
       FileWrite ($File2, @CRLF&$SearchLine)
    EndIf
EndFunc

edit..oops forgot a $

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

Share this post


Link to post
Share on other sites

Thank you.

It's close to working. If I delete a line, not a problem. The problem is when I add an line. It tacks the new line onto the end of the last line with a little box thingy inbetween the two.


A little reading goes a long way. Post count means nothing.

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

How are you adding a line?

I try to always use @CRLF, and not @lf or @cr, if you use notepad to view it, and a lot of dos programs, you get little boxes.

also make sure to use FileWrite (), FileWriteLine () will add it's own @CRLF

if you are still using part of the first code, you might try:

FileWrite ($FileHandle, @CRLF & $AddName)

Edited by scriptkitty

AutoIt3, the MACGYVER Pocket Knife for computers.

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