Jump to content

delete lines from file


DiskAI
 Share

Recommended Posts

$file = "list.txt"

$filea = FileOpen($file, 0)

$i = 0

While 1

$i = $i + 1

$line = FileReadLine($file,$i)

If @error = -1 Then ExitLoop

If StringInStr($line,"yahoo.com") Or StringInStr($line,"myspace.com") Or StringInStr($line,"hotmail.") Or StringInStr($line,"friendster.com") Or StringInStr($line,"login.passport") Or StringInStr($line,"file://") Or StringInStr($line,"premierleague") Or StringInStr($line,"soccernet") Or StringInStr($line,"ukm.my") Or StringInStr($line,"java script:") Or StringInStr($line,"uclfantasy") Or StringInStr($line,"about:") Or StringInStr($line,"My Computer") Or StringInStr($line,"jobstreet.com") Or StringInStr($line,"singingfool.com") Or StringInStr($line,"malaysiaairlines") Or StringInStr($line,"cyki") Or StringInStr($line,"ismgames") Or StringInStr($line,"uefa") Or StringInStr($line,"google.com") Or StringInStr($line,"smpweb") Or StringInStr($line,"ftp:") Or StringInStr($line,"sppk") Or StringInStr($line,"msn.") Or StringInStr($line,"jakionline") Or StringInStr($line,"trendmicro.com") Then

;;;

Else

FileWrite("_" & $file,$line & @CRLF)

EndIf

Wend

FileClose($file)

SoundPlay("C:\WINDOWS\Media\ding.wav")

MsgBox(0,"","Done")

several questions

1. how do i replace the "if stringinstr" bit so it wont be soooooo huge

2. is there an better way to go about this script

3. why the above script eats up a lot of resources cause i got txt files close to 1mb and it took a long time to process

p/s : code looks rough ... i'm a chm guy (learn from help files ;))

Edited by DiskAI

i think

Link to comment
Share on other sites

several questions

1. how do i replace the "if stringinstr" bit so it wont be soooooo huge

2. is there an better way to go about this script

3. why the above script eats up a lot of resources cause i got txt files close to 1mb and it took a long time to process

p/s : code looks rough ... i'm a chm guy (learn from help files ;))

here are some ideas. WARNING: Code not tested !!!

1.) Use a function and pass the string arguments separated by a delimiter. Looks nicer...

$pattern = "pattern1|pattern2|pattern3"
    if not _PatternFound($line, $pattern) then
     ; write line to new file
    else
     ; do something meaningfull or nothing at all :-)
    endif 

    func _PatternFound($line,$pattern)
         local $stringarray = StringSplit($pattern, "|")
         local $counter
         for $counter = 1 to $stringarray[0]
              if StringInStr($line,$stringarray[$counter]) then return 1
         next
         return 0
    endfunc

2.) Yes, read the whole file into an array. See: _FileReadToArray(). It's much faster than

reading the whole file in one string. Then loop through the array to search the pattern in every line.

3.) Reason: see 2.) String operations are slow when you apply them on a large string

EDIT: Fixed some errors in code.

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

do a select case endselect but that just split its up in several lines

e.g.

select
case not stringinstr ($line, "yahoo.com")
filewrite ("_" & $file, $line & @crlf)
case not stringinstr ($line, "myspace.com")
filewrite ("_" & $file, $line & @crlf)
endselect

and your right your script uses quite a lot of ressources

just is a bit more optical

Link to comment
Share on other sites

thanks kurt for your idea ... beats the hell of my code ;)

some typo thou

$searchstring = "pattern1|pattern2|pattern3"

if _PatternFound($line, $pattern) then

; do whatever you have to do

endif

func _PatternFound($line,$pattern)

local $stringarray = StringSplit($line, "|")

local $counter

for $counter = 1 to $stringarray[0]

if StringInStr($line,$stringarray[$counter]) then return 1

next

return 0

endfunc

$pattern should be $searchstring

speed improve greatly with _filereadtoarray

thanks again ...

i think

Link to comment
Share on other sites

Try this:

#include <Array.au3>
  #include <File.au3>
  opt("MustDeclareVars", 1)
  
  Dim $ArraySourceStrings, $ArrayRemoveStrings, $txtValues, $StringsFile, $SourceFile
; File containing strings to remove. (One per line)
  $StringsFile = "RemoveList.txt"
; Source File
  $SourceFile = "list.txt"
  
; Read Strings File
  _FileReadToArray($StringsFile, $ArrayRemoveStrings)
  $txtValues = _ArrayToString($ArrayRemoveStrings, "|", 1)
  
; Prep Strings for RegExp
; Duplicate this line for every character that must be escaped. (Such as Periods)
  $txtValues = StringReplace($txtValues, ".", "\.")
  
; Read Source File and Build Results
  _FileReadToArray($SourceFile, $ArraySourceStrings)
  For $x = 1 To $ArraySourceStrings[0]
     If Not StringRegExp($ArraySourceStrings[$x], $txtValues) Then FileWriteLine("_" & $SourceFile, $ArraySourceStrings[$x])
  Next
  
; Done
  SoundPlay("C:\WINDOWS\Media\ding.wav")
  MsgBox(0, "", "Done")

Edit: Kurt, you beat me to the post with the RegExp idea. Do you know which other characters need to be escaped (besides periods)?

Edited by Skruge

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Link to comment
Share on other sites

Edit: Kurt, you beat me to the post with the RegExp idea. Do you know which other characters need to be escaped (besides periods)?

virtually everything that could have a meaning to RegExp: .,+,*,?, etc... See help file of StringRegExp().

Cheers

Kurt

Edited by /dev/null

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

thanks kurt for your idea ... beats the hell of my code ;)

some typo thou

see EDIT: in my first post. Fixed some errors in code.

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

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