Sign in to follow this  
Followers 0
SwordHunter4Ever

File reading and writing script.

7 posts in this topic

#1 ·  Posted (edited)

This post can be closed, I just found out it won't work for what I'm trying to do.

Sorry!

OVERVIEW: Alright, I've created a autoit program that runs through a list of tasks and does them. It then logs the results of these tasks to a file (a notepad file). The problem is this log is an at the moment log, it logs things that happen when they happen and in whatever order that may be. Meaning when I open it, it takes a lot of searching and such to figure out exactly what happened.

What I Want To Do: I want to read that jumbled up mess of a file, and have a program that finds specific words. Each time it finds the word I want it to change a digit in another file that is sorted. For example, lets say I have a file called OLD.txt which will be the original file and a file called NEW.txt which is the sorted filed. I need a script that will search OLD.txt for the words "John Smith". Each time it finds "John Smith" I need it to edit the NEW.txt file by changing a number on a specific line.

So this is what the two files look like.

OLD.txt
John Smith
RUN
BOB BARKER
SARAH PARKER
John Smith
Joey Something
John Smith

NEW.txt
RUN: 1
BOB BARKER: 1
John Smith: 3
Joey Something: 1
SARAH PARKER: 1

As you can see the OLD.txt is all mixed, where as the NEW.txt is sorted. I realize that it may be hard to change the number next to 'John Smith:" each time and also reading the number so as to know what to change it to, so I've come up with a solution. I figured I could have the NEW.txt file look something like this.

RUN
1

BOb BARKER
1

John Smith
3

etc...

By doing this, I could read line 8 to find the current number of times John Smith has been read, and have the script add 1 to that and rewrite the line to the next highest number. So if the last time I had ran the script it was "5" times it had found it total and I ran it again and it found it 3 more times it would read "5" and change the line to "8"

As A Side Note: The original fire OLD.txt will be replaced each time I run my script, which is another reason for compiling all of the data into the new file. The reason for having it read and change the numbers to new ones each time in the NEW.txt is because it can't simply read the file and record that number because that doesn't include the numbers from the previous runs.

Another Option: If you guys suggest it I may just try this. I have thought about having 3 files, OLD.txt NEW.txt and DATA.txt. In this option the information from OLD.txt would be copied and pasted into DATA.txt each time the script is run, this would compile all of the data into one huge file. I could then have the script search that file and save the number of times something is found to NEW.txt. This frees up the need to read a specific line and write a new number based on that line and the amounts of time it found a piece of information in another file. It however takes up far more space on my computer (though I'm not worried about that).

SHORT VERSION

CODING I NEED: If you guys could direct me where to find this, or just write a basic version so I could edit it that would be great.

The first code I need is a code to search a file for specific words and log how many times it found those words.

The second code I need is a code to read a line form one file, recognize the number and add a new number which is based on the amount of times it found something in another file.

BY THE WAY: It will read the files line by line.

What I already know: I already know of these codes, so you don't need to post these or direct me to them.

FileReadLine ( filehandle or "filename" [, line] )

I just don't know how to get it to recognize what its reading... I can make it read a line but not log if its read something in specific or not.

#Include <File.au3>
_FileWriteToLine($sFile, $iLine, $sText[, $fOverWrite = 0])

I know its a HUGE post, but I thought I should be as specific as possible.

Thanks for your help.

Edited by SwordHunter4Ever

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Well this is one way to count how many times a string occurs:

$string = "This is a string where we talk about counting string occurences."
StringReplace ($string, "string", "")
MsgBox (0, "", @extended)

You'd want to look up StringReplace.

As for your second thing, I think you'll want to look at the IniRead/IniWrite functions, you'll find them a lot easier than what you're trying to do.

*Edit - For that matter, you could probably stand to go back to your original script that generates these logs and just have it do IniWrites at the appropriate points that it's doing things that you want to count...

Edited by exodius

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Well this is one way to count how many times a string occurs:

$string = "This is a string where we talk about counting string occurences."
StringReplace ($string, "string", "")
MsgBox (0, "", @extended)

You'd want to look up StringReplace.

As for your second thing, I think you'll want to look at the IniRead/IniWrite functions, you'll find them a lot easier than what you're trying to do.

*Edit - For that matter, you could probably stand to go back to your original script that generates these logs and just have it do IniWrites at the appropriate points that it's doing things that you want to count...

IniRead ( "filename", "section", "key", "default" )

That is the iniread script, I'm slightly confused. How would I tell it which line of a notepad file to read?

I understand there is the section value, I however don't understand how to work with it.

EDIT: Ahh I think I understand, this is for finding a specific word or phrase? For instance "John Smith" would be the key?

EDIT2: I would go back and have the original script do it, however that script simply saves a log that it pulls form a server. I have no control of the order in which I receive the information. This script is going to be added onto the end of that one to sort it out each time its run.

Edited by SwordHunter4Ever

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Going off your example above, make a txt file and rename it to example.ini. Open the text file and paste this into it:

[Count]
RUN=1
BOB BARKER=1
John Smith=3
Joey Something=1
SARAH PARKER=1

Then make a new script in the same directory and try using this code:

MsgBox(0, "", IniRead("example.ini", "Count", "BOB BARKER", "Default - what shows up if nothing's found"))

*Edit - Even if you can't work it into the original script, you'll keep track of your counting a lot easier this way, if you do an iniread to get your previous value and then IniWrite it back out again.

*Edit2 - Type in my MsgBox code, fixed it.

Edited by exodius

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Going off your example above, make a txt file and rename it to example.ini. Open the text file and paste this into it:

[Count]
RUN=1
BOB BARKER=1
John Smith=3
Joey Something=1
SARAH PARKER=1

Then make a new script in the same directory and try using this code:

MsgBox(0, "", IniRead("example.ini", "Count", "BOB BARKER", "Default - what shows up if nothing's found"))

*Edit - Even if you can't work it into the original script, you'll keep track of your counting a lot easier this way, if you do an iniread to get your previous value and then IniWrite it back out again.

*Edit2 - Type in my MsgBox code, fixed it.

Alright, I did that and it came up with the default message. My example.ini file reads as example.ini.txt because its still a notepad document. Is there any way to make it just a .ini file? or does it not matter?

This post can be closed, I just found out that it won't work for what I'm trying to do x_X

Sorry!

Edited by SwordHunter4Ever

Share this post


Link to post
Share on other sites

Definitely matters, because it's looking for example.ini, not example.ini.txt.

If you don't see the .txt on the end of the file, in Windows XP at least, go into your My Computer --> Tools --> Folder Options --> "View" tab --> unselect "Hide extensions for known file types" --> Hit OK.

Share this post


Link to post
Share on other sites

Alright, I did that and it came up with the default message. My example.ini file reads as example.ini.txt because its still a notepad document. Is there any way to make it just a .ini file? or does it not matter?

This post can be closed, I just found out that it won't work for what I'm trying to do x_X

Sorry!

What are you trying to do? Maybe you just need to approach this another way.

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