Sign in to follow this  
Followers 0
brodie28

This script to edit a text file is driving me crazy!

26 posts in this topic

#1 ·  Posted (edited)

So I have this script which at the moment, is only supposed to open a text file search through it line by line and delete the lines where the substring "Civilian" and "Thug" were not found. (It is for a game I am playing...)

Everything works fine, except the actual deleteing of the line. Here is my current code.

#include <File.au3>
#include <String.au3>

$nolines = _FileCountLines( "locals.txt" )
$locals = FileOpen("locals.txt", 0)

For $number = 1 to $nolines

$line =  FileReadLine($locals, $number)


 If StringIsSpace($line) = 1 Then
sleep(10)
Else
    
If StringInStr($line, "Civilian") or StringInStr($line, "Thug") <> 0 Then

Else
deleteline()


EndIf
Endif 
   

next
    

Func deleteline ()
    FileClose ( $locals )
    
    MsgBox(0, "title", $number)
    
;_FileWriteToLine("locals.txt", $number, "", 1)
     
     $locals = FileOpen("locals.txt", 0)
     
EndFunc

You will notice I have _Filewritetoline excluded. This is because the script works perfectly when it is excluded, and doesn't work properly at all when it is included.

The message box is there so I know which lines it thinks should be deleted... It gets the right lines everytime when _Filewritetoline is excluded, but when I include it.... Somehow it thinks different lines should be deleted. So somehoe that line is affecting the rest of the script in a way that causes it not to work.

Please help, I have tried everything possible :/

Edited by brodie28

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You know what's driving me crazy? People who don't try the Search option before they post new topics. I have answered this question just over a hour ago ! It doesn't include a full script but it's the best way to tackle the problem.

"http://www.autoitscript.com/forum/index.php?showtopic=29486"

Edit: I was being too harsh.. and here's the script so you know I was really sorry, :D

tested and working:

#include <File.au3>

Dim $Result

$Read = FileRead("locals.txt")
$FileSplit = StringSplit($Read,@CRLF)
For $x = 1 to $FileSplit[0]
    If StringInStr($FileSplit[$x], "Civilian") OR StringInStr($FileSplit[$x],"Thug") Then
        $Result &= $FileSplit[$x] & @CRLF
    EndIf
Next

FileWrite(FileOpen("locals.txt", 2), $Result)
Edited by Manadar

Share this post


Link to post
Share on other sites

What I don't understand is how THIS code will not work. For some reason the FileWritetoline function is not working properly.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Well, maybe if you use SciTe, proper spacing, and indexing. You would understand what you have coded.

I can give you support, but i can't see how you inside are thinking so i can't tell where it's gone wrong.

FileWriteLine is perfect.

Edited by Manadar

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

The problem with that script is I don't understand it and therefore cannot add to it like I was planning... Do you have any ideas as to why it is acting strangely in my script?

Edited by brodie28

Share this post


Link to post
Share on other sites

I don't tink FileWriteToLine() actually deletes a line... I think it just empties it and leaves it blank.

This function actually deletes the line you choose to delete:

http://www.autoitscript.com/forum/index.ph...st&p=179430


[center]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.[/center]

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Sure, I'll look into it. Here's the first obvious improvement:

Edit: What you did wrong was the check wether or not thug or civilian was in it.

#include <File.au3>
#include <String.au3>

$nolines = _FileCountLines( "locals.txt" )
$locals = FileOpen("locals.txt", 0)

For $number = 1 to $nolines
    $line =  FileReadLine($locals, $number)
    If StringIsSpace($line) = 1 Then
        sleep(10)
    Else
        If StringInStr($line, "Civilian") or StringInStr($line, "Thug") <> 0 Then
        Else
            deleteline()
        EndIf
    Endif 
Next

Func deleteline ()
    FileClose ( $locals )
    MsgBox(0, "title", $number)
    _FileWriteToLine("locals.txt", $number, "", 1)
    $locals = FileOpen("locals.txt", 0)
EndFunc
Edited by Manadar

Share this post


Link to post
Share on other sites

Remarks

If _FileWriteToLine is called with $fOverWrite as 1 and $sText as "", it will delete the line.

I am completely stumped on how it is messing with my script though.

Share this post


Link to post
Share on other sites

Thanks for the help. Although the message box is still coming up with lines that DO contain the words Civilian and Thug... It only does this when FileWritetoLine is included.

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

I don't tink FileWriteToLine() actually deletes a line... I think it just empties it and leaves it blank.

This function actually deletes the line you choose to delete:

http://www.autoitscript.com/forum/index.ph...st&p=179430

If _FileWriteToLine is called with $fOverWrite as 1 and $sText as "", it will delete the line.

$fOverWrite by default is 1.

Edit: Oops 5 minutes late :D

Edit2: Here's the working script:

#include <File.au3>
#include <String.au3>

$nolines = _FileCountLines( "locals.txt" )
$locals = FileOpen("locals.txt", 0)

For $number = 0 to $nolines
    $line =  FileReadLine($locals, $number)
    If Not (StringInStr($line, "Civilian") OR StringInStr($line, "Thug")) Then
        deleteline()
    EndIf
Next

Func deleteline ()
    FileClose ( $locals )
    _FileWriteToLine("locals.txt", $number, "", 1)
    ;ShowVar("$number") if you want to output $number really quick
    $locals = FileOpen("locals.txt", 0)
EndFunc

Func ShowVar($varname)
    ToolTip($varname & " = " & Execute($varname),0,0)
    Sleep(1000)
EndFunc

#cs
Testfile: locals.txt
civilian
thug
hello
civilian
thug
Manadar is de gekste!
#ce

You should have a look at what AutoIt calculates first in expression, the list lookes like this:

<> =

* /

+ -

It's in the helpfile but i don't know where anymore.

Edited by Manadar

Share this post


Link to post
Share on other sites

I can send you the data in the text file I am using if you like...

NamelessPaladin Jan 01, '82 00:00 Seraphim

Gore Apr 17, '06 20:37 Boss

Public_Enemy Apr 28, '06 12:14 Consigliere

Throwdown Jun 28, '06 10:51 Made Man

TheRebel Jul 08, '06 11:45 Goomba

BananaCake Jul 16, '06 07:16 Civilian

SpamOnRye Jul 16, '06 07:47 Civilian

Jell-O Jul 16, '06 11:03 Civilian

biglawson13 Jul 16, '06 11:50 Goomba

PuddingPop Jul 16, '06 13:23 Civilian

TheMunchies Jul 16, '06 13:57 Civilian

StrawberrySmoothie Jul 16, '06 17:54 Civilian

Spaghetti_Os Jul 16, '06 21:57 Civilian

Mini_Ravioli Jul 16, '06 23:57 Civilian

Captain_Spaulding Jul 17, '06 03:39 Goomba

yem Jul 17, '06 13:42 Civilian

Dacaknuckle Jul 18, '06 00:33 Gangster

Babylon Jul 18, '06 11:31 Gangster

BlueMurder Jul 18, '06 12:34 Thug

WickedMind Jul 18, '06 19:19 Civilian

Bridget_Currio Jul 19, '06 00:03 Thug

Iron_man Jul 19, '06 15:40 Civilian

The_Don Jul 19, '06 22:43 Civilian

If you run the script with this text file the message box comes up with wrong numbers when filewriteline is included.

Share this post


Link to post
Share on other sites

BananaCake Jul 16, '06 07:16 Civilian

SpamOnRye Jul 16, '06 07:47 Civilian

Jell-O Jul 16, '06 11:03 Civilian

PuddingPop Jul 16, '06 13:23 Civilian

TheMunchies Jul 16, '06 13:57 Civilian

StrawberrySmoothie Jul 16, '06 17:54 Civilian

Spaghetti_Os Jul 16, '06 21:57 Civilian

Mini_Ravioli Jul 16, '06 23:57 Civilian

yem Jul 17, '06 13:42 Civilian

BlueMurder Jul 18, '06 12:34 Thug

WickedMind Jul 18, '06 19:19 Civilian

Bridget_Currio Jul 19, '06 00:03 Thug

Iron_man Jul 19, '06 15:40 Civilian

The_Don Jul 19, '06 22:43 Civilian

Share this post


Link to post
Share on other sites

Remarks

If _FileWriteToLine is called with $fOverWrite as 1 and $sText as "", it will delete the line.

I am completely stumped on how it is messing with my script though.

Hmm, well that wasn't an option when I wrote that function, nice to see they added it in.

[center]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.[/center]

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

#include <File.au3>
#include <String.au3>

$nolines = _FileCountLines( "locals.txt" )
$locals = FileOpen("locals.txt", 0)

For $number = 0 to $nolines
    $line =  FileReadLine($locals, $number)
    If Not (StringInStr($line, "Civilian") OR StringInStr($line, "Thug")) Then
        deleteline()
    EndIf
Next

Func deleteline ()
    FileClose ( $locals )
    _FileWriteToLine("locals.txt", $number, "", 1)
    ;ShowVar("$number") if you want to output $number really quick
    $locals = FileOpen("locals.txt", 0)
EndFunc

Func ShowVar($varname)
    ToolTip($varname & " = " & Execute($varname),0,0)
    Sleep(1000)
EndFunc

#cs
Testfile: locals.txt
civilian
thug
hello
civilian
thug
Manadar is de gekste!
#ceoÝ÷ Ûú®¢×¢é]m¶¬m«"q©eÉ·­ëëaxºÚ"µÍÚ[ÛYH   Ñ[K]LÉÝÂØØ[   ÌÍÜÑ[T]HØÜ    [È ÌÎNÉÌLÛØØ[Ë ÌÎNÂØØ[    ÌÍÛ^HH   ÌÎNÉÌÎNÂYÝÑ[TXYÐ^J ÌÍÜÑ[T] ÌÍÛ^JH[ÙÐÞ
    ÌÎNÒ[ÎÌÎNË   ÌÎNÑÜXY[È[IÌÎNÊBÜ  ÌÍÚPÛÝ[HHÈPÝ[
    ÌÍÛ^JHHBRYÝ[Ò[Ý   ÌÍÛ^VÉÌÍÚPÛÝ[K ÌÎNÐÚ][X[ÌÎNÊHÜÝ[Ò[Ý ÌÍÛ^VÉÌÍÚPÛÝ[K ÌÎNÕYÉÌÎNÊH[BWÑ[][J   ÌÍÜÑ[T] ÌÍÚPÛÝ[    ÌÎNÉÌÎNËJBQ[Y^[ÈÚÝÕ   ÌÍÝ[YJBÛÛ
    ÌÍÝ[YH   [È ][ÝÈH ][ÝÈ  [È^]J   ÌÍÝ[YJK
BÛY
L
B[[ÂØÜÂÝ[NØØ[ËÚ][X[[ÂÚ][X[YÂX[YÈHÙZÜÝIÌÌÎÂØÙ
Have no idea what ShowVar() is though.

Edit:

:D Code tags

Edit2:

Do you want Not StringInString() on both of those?

Edit3:

I was going of Manadars code, I saw you didn't want Not so I removed it.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

The code you gave which you said was a working script... Is not working for me.

#include <File.au3>
#include <String.au3>

$nolines = _FileCountLines( "locals.txt" )
$locals = FileOpen("locals.txt", 0)

For $number = 0 to $nolines
    $line =  FileReadLine($locals, $number)
    If Not (StringInStr($line, "Civilian") OR StringInStr($line, "Thug")) Then
        deleteline()
    EndIf
Next

Func deleteline ()
    FileClose ( $locals )
    _FileWriteToLine("locals.txt", $number, "", 1)
    ShowVar("$number"); if you want to output $number really quick
    $locals = FileOpen("locals.txt", 0)
EndFunc

Func ShowVar($varname)
    ToolTip($varname & " = " & Execute($varname),0,0)
    Sleep(1000)
EndFunc

It is doing the same thing it was doing for me... The number displayed goes 1, 2, 6 etc. and is just plain wrong.

What could be causing this?

Share this post


Link to post
Share on other sites

The code you gave which you said was a working script... Is not working for me.

It is doing the same thing it was doing for me... The number displayed goes 1, 2, 6 etc. and is just plain wrong.

What could be causing this?

Did you bother to try mine? (I even commented what is probably wrong in his code in mine).

[center]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.[/center]

Share this post


Link to post
Share on other sites

Your code didn't work either.

It returned

NamelessPaladin Jan 01, '82 00:00 Seraphim

Gore Apr 17, '06 20:37 Boss

Public_Enemy Apr 28, '06 12:14 Consigliere

Throwdown Jun 28, '06 10:51 Made Man

TheRebel Jul 08, '06 11:45 Goomba

SpamOnRye Jul 16, '06 07:47 Civilian

biglawson13 Jul 16, '06 11:50 Goomba

TheMunchies Jul 16, '06 13:57 Civilian

StrawberrySmoothie Jul 16, '06 17:54 Civilian

Mini_Ravioli Jul 16, '06 23:57 Civilian

yem Jul 17, '06 13:42 Civilian

Babylon Jul 18, '06 11:31 Gangster

WickedMind Jul 18, '06 19:19 Civilian

Iron_man Jul 19, '06 15:40 Civilian

The_Don Jul 19, '06 22:43 Civilian

.....

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

Edit:

Better yet... This one works for sure (I tested it), I think _FileWriteToLine() was messing that up some how.

#include <File.au3>
Local $sFilePath = @ScriptDir & '\locals.txt'
Local $nArray = '', $sStore
If Not _FileReadToArray($sFilePath, $nArray) Then MsgBox(64, 'Info:', 'Error reading File')
For $iCount = 1 To UBound($nArray) - 1
    If StringInStr($nArray[$iCount], 'Civilian') Or StringInStr($nArray[$iCount], 'Thug') Then
        $sStore &= $nArray[$iCount] & @CRLF
    EndIf
Next

$hOpen = FileOpen($sFilePath, 2)
FileWrite($hOpen, $sStore)
FileClose($hOpen)

Edit2:

Nah, now that I think about it, it was a faulty logic issue, the number of lines changed everytime you/we used _FileWriteToLine().

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

I realised what was going wrong... Deleting the lines changed the number of lines which put off the For loop... Havn't found a way around that yet :/

Share this post


Link to post
Share on other sites

I realised what was going wrong... Deleting the lines changed the number of lines which put off the For loop... Havn't found a way around that yet :/

Read above at my edits.

[center]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.[/center]

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