AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Deleting Multiple Lines in a Text File

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Delusion



Joined: 16 Jul 2008
Posts: 210
Location: Greece/Rhodos

PostPosted: Thu Mar 04, 2010 10:35 pm    Post subject: Deleting Multiple Lines in a Text File Reply with quote

what i want to do is delete multiple lines from my text file

i have a list of lines want to delete shown in a lisbox so my variable would look like this :

$DMF = someline|another line|line number 3

and here is my code :

Code:

DELETEGUI_Ok:
  IF $DM_Temp1 =
  {
    Notify($DelGuiName " - ERROR", "No Movie has been Selected!", "5", "GC=White TC=Black MC=Black IN=13", $ResDLL)
    RETURN
  } ELSE {
    IF LB_GETCOUNT = 0
    {
      ;RESERVED
    } ELSE {
      FileRead $FileReadOutput, % $MovieList
      StringTrimRight, $DMF, $DMF, 1
      Loop Parse, $DMF, |
      {
        StringReplace $AppendOutput4, $FileReadOutput, %A_LoopField%, , All
        StringReplace $AppendOutput4, $AppendOutput4, `r`n`r`n, `r`n, All
      }   
      IF Errorlevel = 0
      {
        FileDelete % $MovieList
        FileAppend % $AppendOutput4, % $MovieList

        Gui %$MainGui%: Default
        RowNumber = 0
        While RowNumber := LV_GetNext(RowNumber - 1)
          LV_Delete(RowNumber)   
        Notify($DelGuiName " - INFO","All [ " . LB_GETCOUNT . " ] entries have been Deleted Successfully!", "5", "GC=White TC=Black MC=Black IN=13", $ResDLL)
        MovieCount()
      }   
      ELSE IF Errorlevel = 1
        Notify($DelGuiName " - ERROR","There was an Error with Deleting!", "5", "GC=White TC=Black MC=Black IN=13", $ResDLL)
      Gui %$DelGui%: Submit
      IF $MovieCount = 0
        GuiControl %$MainGui%: Text, $MOVIESEARCH,
    }
  }
  Loop 6
    $DM_Temp%A_Index% := ""
  IL_Destroy(hIL)
  Gui %$MainGui%: -Disabled
  Gui %$MainGui%: Default
  WinActivate ahk_id %MLWIN_ID%
RETURN


it only deletes one line
what am i doing wrong at the red part here
do i have to fileappend every time i string replace?
_________________
Popcorn Movie Db
Simple Apnea Trainer
Back to top
View user's profile Send private message Visit poster's website
da_boogie_man
Guest





PostPosted: Fri Mar 05, 2010 1:24 am    Post subject: Reply with quote

I won't fiddle with your code, but here's one solution {may not be the best, but it's what I would do}:

Code:

FileRead, mveFile, c:\SomeFolder\MovieList.txt
newFile =

Loop, Parse, mveFile, `n
  IfNotInString, a_loopfield, Some Text
    newFile := newFile a_loopfield `n

FileDelete, c:\SomeFolder\MovieList.txt
FileAppend, newFile, c:\SomeFolder\MovieList.txt


That would be the simple way, and I like to keep my coding simple {none of that fancy-shmancy stuff}. One Read, one Append and in-between is just done within a loop {or 2-?? depending on what criteria you're looking to keep out of your list}.

DBM
Back to top
Delusion



Joined: 16 Jul 2008
Posts: 210
Location: Greece/Rhodos

PostPosted: Fri Mar 05, 2010 9:01 am    Post subject: Reply with quote

i tried it and its not working

well i usually like simple code too
and the red part of my code is pretty simple i think
the rest is only needed for my gui
_________________
Popcorn Movie Db
Simple Apnea Trainer
Back to top
View user's profile Send private message Visit poster's website
garry



Joined: 19 Apr 2005
Posts: 2212
Location: switzerland

PostPosted: Fri Mar 05, 2010 4:45 pm    Post subject: Reply with quote

hello Delusion,
you want delete multiple marked lines in text-file using Listview (?)
mark multiple lines (leftclick) and delete it with righclick
Code:

;------- create a test-file ----
F1=%A_scriptdir%\test55.txt
Gui,1:default
Gui,1:Font,S10 cDefault, Verdana

EF=`r
ifnotexist,%F1%
{
AA=
(
Line3A;3AA;3AAA;3AAAA%EF%
Line1A;1BB;1CCC;1DDDD%EF%
Line5A;5BB;5CCC;5DDDD%EF%
Line2A;2BB;2CCC;2DDDD%EF%
Line4A;4BB;4CCC;4DDDD%EF%
)
Fileappend,%AA%`n,%F1%
}


T1=200       ;column1 width
T2=100       ;column2 width
T3=100       ;column3 width
T4=100       ;column4 width
LSW:=(T1+T2+T3+T4+20)    ;Listview width
GSW:=(LSW+30)            ;GUI      width
Gui,1:Add, ListView,x10 y10 h280 w%LSW%   grid +hscroll nosorthdr altsubmit vMLV1A gMLV1B, AA|BB|CC|DD
  LV_ModifyCol(1,T1)
  LV_ModifyCol(2,T2)
  LV_ModifyCol(3,T3)
  LV_ModifyCol(4,T4)
gosub,SB
Gui,1:Show, x2 y0 w%GSW% h610,TEST FILEREAD
return
;-------------------------------

Guiclose:
exitapp

SB:
Gui,1:ListView,MLV1A
LV_Delete()
  loop,read,%F1%
  {
  stringsplit,C,A_LoopReadLine,`;,
  LV_Add("",C1,C2,C3,C4)
  }

GC:=LV_GetCount()
LV_Modify(LV_GetCount(), "Vis")    ;jumps to last row
;GuiControl,1:Text,TOT1,%GC%
LV_Modify(GC, "+Select +Focus")    ;focus last row
return

;=================================================

;---------------------------------


;-----------------------------------------------
MLV1B:
Gui,1:Submit,nohide
Gui,1:ListView,MLV1A
    RN:=LV_GetNext("C")
    RF:=LV_GetNext("F")
    GC:=LV_GetCount()

if A_GuiEvent=normal
    return


;--------- DELETE multiple rows in text-file -----------
if A_GuiEvent = RightClick
{
CLL=
RF= 0
RFL=
Loop
   {
   RF:=LV_GetNext(RF)
     if (RF="" OR RF=0)
         break
   RFL = %RF%|%RFL%      ;yyy
   LV_GetText(CLL_Temp,RF,1)
   CLL = %CLL%`n%CLL_Temp%
   }

if CLL !=
 {
   msgbox, 262452, DELETE,Want you delete lines in %F1%`n%CLL%?
   IfMsgBox,No
      Return
   Else
   {
      Loop, parse, RFL, |      ;yyy
          LV_Delete(A_LoopField)

    filedelete,%F1%
    RFL=
    CLL=
    Loop % LV_GetCount()
      {
      BX1=
      BX2=
      BX3=
      BX4=
      LV_GetText(BX1,A_INDEX,1)
      LV_GetText(BX2,A_INDEX,2)
      LV_GetText(BX3,A_INDEX,3)
      LV_GetText(BX4,A_INDEX,4)
      e4=%e4%%BX1%`;%BX2%`;%BX3%`;%BX4%`r`n
      }
   Fileappend,%E4%,%F1%
   e4=
   }
 }
Gosub,SB
return
}
return




another example ( I think not wanted) , like above
Code:
;-------- removexxx somelines in textfile which contains zzzz -----
F1=test1.txt
;ifexist,%f1%
;  filedelete,%f1%

a4=
(
aaaa
bbbbzzzzccc
dddd
eeee
zzzz
ggggrrrr
)

stringreplace,a4,a4,`r,`r`n
ifnotexist,%f1%
  Fileappend,%a4%,%f1%

S1=zzzz
FileRead, mveFile,%f1%
newFile =

Loop, Parse, mveFile, `n
  {
  IfNotInString, a_loopfield, %s1%
    new1=%new1%%a_loopfield%
  }
FileDelete, %f1%
FileAppend,%new1%,%f1%
run,%f1%
return


EDIT modified first script


Last edited by garry on Fri Mar 05, 2010 10:36 pm; edited 3 times in total
Back to top
View user's profile Send private message
Delusion



Joined: 16 Jul 2008
Posts: 210
Location: Greece/Rhodos

PostPosted: Fri Mar 05, 2010 8:30 pm    Post subject: Reply with quote

garry wrote:
hello Delusion,
you want delete multiple marked lines in text-file using Listview (?)


yeah something like that
i have a listview and when i select some files and press delete it opens another gui where all the files that will be deleted are in a listbox

i will try your examples and post back
_________________
Popcorn Movie Db
Simple Apnea Trainer
Back to top
View user's profile Send private message Visit poster's website
Delusion



Joined: 16 Jul 2008
Posts: 210
Location: Greece/Rhodos

PostPosted: Fri Mar 05, 2010 10:08 pm    Post subject: Reply with quote

@garry

well the first method seems to work but its not the best way to do it when you have to fileappend 500 times
and besides that it would mess up the order of lines in my text file...i want to still be able to see what line i have added first and what last

the second method same as the example posted earlier..it doesnt work

isnt there some other method for doing this similar to what i was trying to do?
_________________
Popcorn Movie Db
Simple Apnea Trainer
Back to top
View user's profile Send private message Visit poster's website
garry



Joined: 19 Apr 2005
Posts: 2212
Location: switzerland

PostPosted: Fri Mar 05, 2010 10:39 pm    Post subject: Reply with quote

Hi Delusion,
-first script modified , fileappend every time is not good
it keeps sequence how it was

-second script worked, but not what you use
Back to top
View user's profile Send private message
Delusion



Joined: 16 Jul 2008
Posts: 210
Location: Greece/Rhodos

PostPosted: Sat Mar 06, 2010 9:57 am    Post subject: Reply with quote

garry wrote:
Hi Delusion,
-first script modified , fileappend every time is not good
it keeps sequence how it was

-second script worked, but not what you use


i tried again
and yeah its better now but the file sequence is still messed up because it saves it as it is in the listview
and in the listview it appears in different order as in the file because it is sorted

so it would be better to use some method that operates on the file only and not depending on the listview at all
_________________
Popcorn Movie Db
Simple Apnea Trainer
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group