Sign in to follow this  
Followers 0
themonk214

Read Only x Lines of a file

8 posts in this topic

#1 ·  Posted (edited)

I have made two files and i use one to read from and then i write to the other both of the text files are the exact same.

However i only want the file to read until x lines heres my code i have commented on it. But some comments arent true, otherwise i wouldn't be posting would i :D

so reading only the first 3 lines

$filewild = $namewrite & ".txt" ; this defines the wild one
$file = FileOpen($name & ".txt", 0) ; 1 for write and 0 for read
; Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, "Error", "Save File In Same Directory As Script")
    Exit
EndIf
$times4 = 1 
For $times4 = 1 to 6 ; only reads the first 6 lines of the file for the wildcarding 
$line = FileReadLine($file) ; line is what is read $file is the file thats saved 
$linewild = $times3 
If StringInStr($line, "68") Then _FileWriteToLine($filewild, $linewild, "?? ?? ?? ?? ??", 1) 
Next 
FileClose($file) ; closes the unwildcarded file 
FileClose($filewild) ; closes the wildcarded file

Text file # 1 and # 2 are the same

55

8BEC

6A FF

68 EFDC6300 <== this one i want changed

64:A1 00000000

50

64:8925 00000000

83EC 08

53

56

57

68 3E270000 <== this one kept!

E8 682D0200

6A 10

8BF0

E8 3FC3F7FF

If my method is too complicated.

Could you tell me either in code or logic how i would just replace the first occurance of "68"

So if a line has 68 for the first 2 digits it will turn all the characters in that line into ?? marks.

This is what i want it to do

BEFORE:

55

8BEC

6A FF

68 EFDC6300 <== this one i want changed

64:A1 00000000

50

64:8925 00000000

83EC 08

53

56

57

68 3E270000 <== this one kept!

E8 682D0200

6A 10

8BF0

E8 3FC3F7FF

AFTER:

55

8BEC

6A FF

?? ?? ?? ?? ?? <== this one i want changed

64:A1 00000000

50

64:8925 00000000

83EC 08

53

56

57

68 3E270000 <== this one kept!

E8 682D0200

6A 10

8BF0

E8 3FC3F7FF

What happens is that both occurances of 68 are changed into ?? marks :D

Edited by themonk214

Share this post


Link to post
Share on other sites



Hi,

First to read some lines, use a loop and FileReadLine function like this:

For $iLoopCounter = 1 to 3
    $sLineData = FileReadLine($hFileHandler,$iLoopCounter)
Next

To find the entry in your case, just exit after you found the first occurrence and did your thing. I would do this with a function and use return to exit it.

Bye,

Tim

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

The before text in post #1 was copied, pasted into Notepad, and saved as "tst1.txt" file in the script's directory.

After running this script the forth line only of the produced "tst2.txt" file contains question marks.

;
Local $sStr = FileRead("tst1.txt")
StringReplace(StringRegExpReplace($sStr, "(?s)(.+?68 )(.*)$", "\1"), @CRLF, @CRLF) ; 0-base line number in @extended.

Local $iLine = @extended, $sReplaceLine = "?? ????????"

$sREResult = StringRegExpReplace($sStr, "(?m)((^.*\v+|\v+){" & $iLine & "})(.*\v+|\v+)(?s)(.*)", "\1" & $sReplaceLine & @CRLF & "\4") ; Return string with replaced line. (0-based line number)

$file = FileOpen("tst2.txt", 2)
FileWrite($file, $sREResult)
FileClose($file)
ShellExecute("tst2.txt")
;
Edited by Malcy

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Thanks everyone! This is why autoit is one of the best scripting languages!!!! Because the community is insanely helpful. First responses average in at minuets! Thank you everyone! :D

Malcy that script was amazing! But i dont understand

StringReplace(StringRegExpReplace($sStr, "(?s)(.+?68 )(.*)$", "\1"), @CRLF, @CRLF) ; 0-base line number in @extended.

nor $sREResult = StringRegExpReplace($sStr, "(?m)((^.*\v+|\v+){" & $iLine & "})(.*\v+|\v+)(?s)(.*)", "\1" & $sReplaceLine & @CRLF & "\4") ; Return string with replaced line. (0-based line number)

i dont get what all the characters you wrote there but thanks!

Edited by themonk214

Share this post


Link to post
Share on other sites

So, what are you trying to do?

Is this serious thread or are you just fooling around not knowing exactly what you do?

If serious then I see you are trying to change, or better said manipulate, exception handler.

I find the subject to be extremely interesting and am willing to learn from you if you are willing to share your knowledge.

On the other hand, if this is I read somewhere and I don't know what that is and what are you talking about? then by all means ignore this post. I wouldn't be interested.

What is it themonk214?


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

So, what are you trying to do?

Is this serious thread or are you just fooling around not knowing exactly what you do?

If serious then I see you are trying to change, or better said manipulate, exception handler.

I find the subject to be extremely interesting and am willing to learn from you if you are willing to share your knowledge.

On the other hand, if this is I read somewhere and I don't know what that is and what are you talking about? then by all means ignore this post. I wouldn't be interested.

What is it themonk214?

huh?

This is serious i wouldn't ask a question and type up this much effort for a joke.

What do u want to know about this?

Its an ollyDBG binary copy of 10 lines of binary for a program?

I cant get onto what i do with these binary strings on this forum however if you are curious add me on msn

henreybellchester@hotmail.com

Share this post


Link to post
Share on other sites

huh?

This is serious i wouldn't ask a question and type up this much effort for a joke.

What do u want to know about this?

Its an ollyDBG binary copy of 10 lines of binary for a program?

I cant get onto what i do with these binary strings on this forum however if you are curious add me on msn

henreybellchester@hotmail.com

I can read hex dump, especially that arranged, so I have pretty much the idea of what you are doing.

Let's stay here.

What do you replace with?


♡♡♡

.

eMyvnE

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