How can I get the data from txt file? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

How can I get the data from txt file?

13 Nov 2019, 02:34

Hello,

I have two files in my WIN-7 computer (C:\a.log , C:\b.txt). When I press START, I want to get the data from a.log and b.txt like the picture as below.
I have tried many methods but it still doesn't work, so I need your help.
Have a nice day.


Code: Select all

vvv=0
Gui +AlwaysOnTop
Gui, Color, dcdcdc
Gui, Show, x131 y91 h280 w260,Text
Gui, Font, Norm S10 cred , Tahoma
Gui, Add, GroupBox, x60 y5 w80 h210 , Group_A
Gui, Add, Text, x10 y35, BTF3 04
Gui, Add, Text, x10 y65, BTF3 05
Gui, Add, Text, x10 y95, BTF3 08
Gui, Add, Text, x10 y125, BTF3 09
Gui, Add, Text, x10 y155, BTF3 34
Gui, Add, Text, x10 y185, BTF3 35
Gui, Add, Edit, x75 y30 w50 h24 vcard1,
Gui, Add, Edit, x75 y60 w50 h24 vcard2,
Gui, Add, Edit, x75 y90 w50 h24 vcard3,
Gui, Add, Edit, x75 y120 w50 h24 vcard4,
Gui, Add, Edit, x75 y150 w50 h24 vcard5,
Gui, Add, Edit, x75 y180 w50 h24 vcard6,

Gui, Add, GroupBox, x150 y5 w80 h210 , Group_B
Gui, Add, Edit, x165 y30 w50 h24 vcard7,
Gui, Add, Edit, x165 y60 w50 h24 vcard8,
Gui, Add, Edit, x165 y90 w50 h24 vcard9,
Gui, Add, Edit, x165 y120 w50 h24 vcard10,
Gui, Add, Edit, x165 y150 w50 h24 vcard11,
Gui, Add, Edit, x165 y180 w50 h24 vcard12,

Gui, Add, Button, x162 y240 w60 h25 ,START

#Persistent
WinGet, a_ID, ID, A
SetTimer, aa, 500
return

aa:
WinGet, a1_ID, ID, A
If ( a_ID != a1_ID )
k_ID = %a1_ID%
Return



/*
ButtonSTART:
*/

    GuiClose:
ExitApp
Last edited by Marcosa1020 on 19 May 2020, 01:14, edited 3 times in total.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How can I get the data from txt file?

13 Nov 2019, 02:42

is the structure of the data static and predicable?
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How can I get the data from txt file?

13 Nov 2019, 06:43

Hellbent wrote:
13 Nov 2019, 02:42
is the structure of the data static and predicable?
Yes, it has a fixed mode.
1. For the a.log file, I recommend searching for "Check BTF13 04" then getting the fourth line(+28.29).
Next searching for "Check BTF13 05" then getting the fourth line(+28.39)
Last edited by Marcosa1020 on 20 Feb 2020, 21:57, edited 2 times in total.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How can I get the data from txt file?

13 Nov 2019, 07:01

Ok, then this should be a rather simple problem to solve.

This is just off the top of my head, and likely could use some refinement.

For the txt file.

It seems to me that you could just load the txt file into a var, then split it by lines, then search for lines that start with a 3 and then a space.

Once you find a line that matches that, split it into a temp array using spaces as the delimiter. Then remove any elements that contain nothing (not sure if they would even exist, you would find out quickly by ding a bit of testing while writing your code.) and remove any elements that contain only a ".". After that the only elements you would have left would be the different values "3 0 34 etc". Next check the third element to see if it's on your list of values you want i.e if(Array[3]=34). next check the Nth element for the temp value you need and update your gui with it. i.e StoredValue:=Array[N]

After that repeat, skipping over any lines that also contain the same 3rd element. i.e if(Array[3]=34).


For the log file

It would be very similar, you just read the file then split it up line by line looking for a line that starts with the word "check"
then get the value beside it and then jump down a few lines and get the temp data.
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How can I get the data from txt file?

13 Nov 2019, 07:12

Hellbent wrote:
13 Nov 2019, 07:01
Ok, then this should be a rather simple problem to solve.

This is just off the top of my head, and likely could use some refinement.

For the txt file.

It seems to me that you could just load the txt file into a var, then split it by lines, then search for lines that start with a 3 and then a space.

Once you find a line that matches that, split it into a temp array using spaces as the delimiter. Then remove any elements that contain nothing (not sure if they would even exist, you would find out quickly by ding a bit of testing while writing your code.) and remove any elements that contain only a ".". After that the only elements you would have left would be the different values "3 0 34 etc". Next check the third element to see if it's on your list of values you want i.e if(Array[3]=34). next check the Nth element for the temp value you need and update your gui with it. i.e StoredValue:=Array[N]

After that repeat, skipping over any lines that also contain the same 3rd element. i.e if(Array[3]=34).


For the log file

It would be very similar, you just read the file then split it up line by line looking for a line that starts with the word "check"
then get the value beside it and then jump down a few lines and get the temp data.
As you said, I tried to open and search with excel, but there is a lot of information in the original file, they disappeared when they were segmentation.
I don't know what is the "VAR", this is much more difficult than I could have imagined.
Thank you for your reply.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How can I get the data from txt file?

13 Nov 2019, 07:27

Marcosa1020 wrote:
13 Nov 2019, 07:12
I don't know what is the "VAR", this is much more difficult than I could have imagined.
Sorry, I normally think of myself as a novice and neglect the fact that I do have a fair bit of experience regardless of my novice status.

I had seen that you have been around here longer than I have even known of the existence of AHK and figured that the basic procedure would ring a bell.

If you post the files as plain text I will have a look at getting something started. (I don't download anything, end of story, no compromise )
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How can I get the data from txt file?

13 Nov 2019, 07:41

Hellbent wrote:
13 Nov 2019, 07:27
Marcosa1020 wrote:
13 Nov 2019, 07:12
I don't know what is the "VAR", this is much more difficult than I could have imagined.


Sorry, I normally think of myself as a novice and neglect the fact that I do have a fair bit of experience regardless of my novice status.

I had seen that you have been around here longer than I have even known of the existence of AHK and figured that the basic procedure would ring a bell.
If you post the files as plain text I will have a look at getting something started. (I don't download anything, end of story, no compromise )
What do you mean? Do you want to the original a.log and b.txt?
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How can I get the data from txt file?

13 Nov 2019, 07:44

Marcosa1020 wrote:
13 Nov 2019, 07:41
What do you mean? Do you want to the original a.log and b.txt?
Whatever you have posted above as a download. Post that as plain text (inside some code tags)
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How can I get the data from txt file?

13 Nov 2019, 07:51

Hellbent wrote:
13 Nov 2019, 07:44

Whatever you have posted above as a download. Post that as plain text (inside some code tags)

I have post the files as plain text on 1F. Thank you.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How can I get the data from txt file?

13 Nov 2019, 07:56

Thank you.

I'll take a crack at it after I grab a bite to eat.
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How can I get the data from txt file?

13 Nov 2019, 08:09

The text file looks like factory component specs. Do you really need to search through it? Seems to me to be the kind of thing that you could hard code and then compare to your log.

20191113082117.png
20191113082117.png (81.6 KiB) Viewed 3237 times
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How can I get the data from txt file?

13 Nov 2019, 08:31

Hellbent wrote:
13 Nov 2019, 08:09
The text file looks like factory component specs. Do you really need to search through it? Seems to me to be the kind of thing that you could hard code and then compare to your log.


20191113082117.png
Yes, if it can get the value accurately. Can you get the value from a.log and b.txt to Text AHK ?
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How can I get the data from txt file?

13 Nov 2019, 08:37

Marcosa1020 wrote:
13 Nov 2019, 08:31
Yes, if it can get the value accurately.
Well you're only testing 6 points (from what your gui suggests). So you only need to get the recommended / max / min / [w/e] from spec sheet for the 6 items and hard code them.

Is that a true statement?
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How can I get the data from txt file?

13 Nov 2019, 08:39

Marcosa1020 wrote:
13 Nov 2019, 08:31
Can you get the value from a.log and b.txt to Text AHK ?
If the txt is just a spec sheet then you only need to get the data from your log. The data from the spec sheet can just get typed right into the script, there's no need to look for the value, it never changes, it's the log that changes.
tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

Re: How can I get the data from txt file?  Topic is solved

13 Nov 2019, 08:55

Code: Select all

...
gui, show
LoadData("G:\Download\a.log", "G:\Download\b.txt")
return

LoadData(FileA, FileB) {
	FileRead, data, %FileA%
	objA := RegExMatchAll_Obj(data, "`s)Check BTF\d+ (\d+).*?\R+    ([\d.+-]+)")

	FileRead, data, %FileB%
	objB := RegExMatchAll_Obj(data, "\R(?:3AAAAA |3 *Bank ).*?\R+\d+ *\d+ *(\d+).*?([\d.]+) *3\R")

	for n, ctrlId in {4:"card1", 5:"card2", 8:"card3", 9:"card4", 34:"card5", 35:"card6"} {
		GuiControl,, % ctrlId, % objA[n]
	}
	for n, ctrlId in {4:"card7", 5:"card8", 8:"card9", 9:"card10", 34:"card11", 35:"card12"} {
		GuiControl,, % ctrlId, % objB[n]
	}
}

RegExMatchAll_Obj(ByRef Haystack, NeedleRegEx, KeyPat := 1, ValPat := 2) {
	obj := {}, startPos := 1
	while ( pos := RegExMatch(Haystack, NeedleRegEx, m, startPos) ) {
		obj[ m%KeyPat% ] := m%ValPat%
		startPos := pos + StrLen(m)
	}
	return obj.Count() ? obj : ""
}
Time to learn RegEx? It's actually very easy.
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How can I get the data from txt file?

13 Nov 2019, 09:08

Hellbent wrote:
13 Nov 2019, 08:37
Marcosa1020 wrote:
13 Nov 2019, 08:31
Yes, if it can get the value accurately.
Well you're only testing 6 points (from what your gui suggests). So you only need to get the recommended / max / min / [w/e] from spec sheet for the 6 items and hard code them.

Is that a true statement?
No. For the a.log file, each time the value will be different.
#Part_1
1. Search "Check BTF 04" then find fourth line, get value(+28.29) and fill in the vcard1.
2. Search "Check BTF 05" then find fourth line, get value(+28.39) and fill in the vcard2.
3. Search "Check BTF 08" then find fourth line, get value(+31.70) and fill in the vcard3.
4. Search "Check BTF 09" then find fourth line, get value(+28.64) and fill in the vcard4.
5. Search "Check BTF 34" then find fourth line, get value(+25.64) and fill in the vcard5.
6. Search "Check BTF 35" then find fourth line, get value(+24.64) and fill in the vcard6.

For the b.txt file, each time the value will be different.
#Part_2
1. Search "04" then get value(30.6) and fill in the vcard7.
2. Search "05" then get value(30.6) and fill in the vcard8.
3. Search "08" then get value(29.6) and fill in the vcard9.
4. Search "09" then get value(29.6) and fill in the vcard10.
5. Search "34" then get value(30.1) and fill in the vcard11.
6. Search "35" then get value(30.1) and fill in the vcard12.

#Part_3
Finally, some calculations will be done for Group_A and Group_B. (I will write the code to complete this part).
User avatar
Hellbent
Posts: 2102
Joined: 23 Sep 2017, 13:34

Re: How can I get the data from txt file?

13 Nov 2019, 09:12

tmplinshi wrote:
13 Nov 2019, 08:55
Time to learn RegEx?
Never!!!!! :lol: (But seriously if I can go until I die without spending one second trying to figure out what the symbols mean, i'll die that much happier. Just doesn't interest me.)

Nice work. :bravo:
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How can I get the data from txt file?

13 Nov 2019, 09:54

tmplinshi wrote:
13 Nov 2019, 08:55

Code: Select all

...
gui, show
LoadData("G:\Download\a.log", "G:\Download\b.txt")
return

LoadData(FileA, FileB) {
	FileRead, data, %FileA%
	objA := RegExMatchAll_Obj(data, "`s)Check BTF\d+ (\d+).*?\R+    ([\d.+-]+)")

	FileRead, data, %FileB%
	objB := RegExMatchAll_Obj(data, "\R(?:3AAAAA |3 *Bank ).*?\R+\d+ *\d+ *(\d+).*?([\d.]+) *3\R")

	for n, ctrlId in {4:"card1", 5:"card2", 8:"card3", 9:"card4", 34:"card5", 35:"card6"} {
		GuiControl,, % ctrlId, % objA[n]
	}
	for n, ctrlId in {4:"card7", 5:"card8", 8:"card9", 9:"card10", 34:"card11", 35:"card12"} {
		GuiControl,, % ctrlId, % objB[n]
	}
}

RegExMatchAll_Obj(ByRef Haystack, NeedleRegEx, KeyPat := 1, ValPat := 2) {
	obj := {}, startPos := 1
	while ( pos := RegExMatch(Haystack, NeedleRegEx, m, startPos) ) {
		obj[ m%KeyPat% ] := m%ValPat%
		startPos := pos + StrLen(m)
	}
	return obj.Count() ? obj : ""
}
Time to learn RegEx? It's actually very easy.
Thank you and thank you.
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How can I get the data from txt file?

13 Nov 2019, 09:55

Hellbent wrote:
13 Nov 2019, 09:12
tmplinshi wrote:
13 Nov 2019, 08:55
Time to learn RegEx?
Never!!!!! :lol: (But seriously if I can go until I die without spending one second trying to figure out what the symbols mean, i'll die that much happier. Just doesn't interest me.)

Nice work. :bravo:
Hi Hellbent,

Thank you.
Marcosa1020
Posts: 168
Joined: 23 Sep 2015, 19:15

Re: How can I get the data from txt file?

13 Nov 2019, 20:05

Hello,

I want to calculate the difference between Card1 and Card7 then display to card13.
So I have added the code "GuiControl,,card13, %card1 - card7" in Program A, but that useless.
If this value is more than plus or minus 10, I want to use black value.

Does anyone want to help me? Thank you.

Code: Select all

GuiControl,,card13, % (card1 - card7)
Program A:

Code: Select all

vvv=0
Gui +AlwaysOnTop
Gui, Color, dcdcdc
Gui, Show, x131 y91 h280 w300,Text
Gui, Font, Norm S10 cred , Tahoma
Gui, Add, GroupBox, x60 y5 w80 h210 , Group_A
Gui, Add, Text, x10 y35, BTF3 04
Gui, Add, Text, x10 y65, BTF3 05
Gui, Add, Text, x10 y95, BTF3 08
Gui, Add, Text, x10 y125, BTF3 09
Gui, Add, Text, x10 y155, BTF3 34
Gui, Add, Text, x10 y185, BTF3 35
Gui, Add, Edit, x75 y30 w50 h24 vcard1,
Gui, Add, Edit, x75 y60 w50 h24 vcard2,
Gui, Add, Edit, x75 y90 w50 h24 vcard3,
Gui, Add, Edit, x75 y120 w50 h24 vcard4,
Gui, Add, Edit, x75 y150 w50 h24 vcard5,
Gui, Add, Edit, x75 y180 w50 h24 vcard6,

Gui, Add, GroupBox, x150 y5 w80 h210 , Group_B
Gui, Add, Edit, x165 y30 w50 h24 vcard7,
Gui, Add, Edit, x165 y60 w50 h24 vcard8,
Gui, Add, Edit, x165 y90 w50 h24 vcard9,
Gui, Add, Edit, x165 y120 w50 h24 vcard10,
Gui, Add, Edit, x165 y150 w50 h24 vcard11,
Gui, Add, Edit, x165 y180 w50 h24 vcard12,

Gui, Add, Edit, x240 y30 w50 h24 vcard13,

Gui, Add, Button, x162 y240 w60 h25 ,START

#Persistent
WinGet, a_ID, ID, A
SetTimer, aa, 500
return

aa:
WinGet, a1_ID, ID, A
If ( a_ID != a1_ID )
k_ID = %a1_ID%
Return

ButtonSTART:
gui, show
LoadData("G:\Download\a.log", "G:\Download\b.txt")
return

LoadData(FileA, FileB) {
	FileRead, data, %FileA%
	objA := RegExMatchAll_Obj(data, "`s)Check BTF\d+ (\d+).*?\R+    ([\d.+-]+)")

	FileRead, data, %FileB%
	objB := RegExMatchAll_Obj(data, "\R(?:3AAAAA |3 *Bank ).*?\R+\d+ *\d+ *(\d+).*?([\d.]+) *3\R")

	for n, ctrlId in {4:"card1", 5:"card2", 8:"card3", 9:"card4", 34:"card5", 35:"card6"} {
		GuiControl,, % ctrlId, % objA[n]
	}
	for n, ctrlId in {4:"card7", 5:"card8", 8:"card9", 9:"card10", 34:"card11", 35:"card12"} {
		GuiControl,, % ctrlId, % objB[n]
	}
	
}

RegExMatchAll_Obj(ByRef Haystack, NeedleRegEx, KeyPat := 1, ValPat := 2) {
	obj := {}, startPos := 1
	while ( pos := RegExMatch(Haystack, NeedleRegEx, m, startPos) ) {
		obj[ m%KeyPat% ] := m%ValPat%
		startPos := pos + StrLen(m)
	}
	return obj.Count() ? obj : ""
}

GuiControl,,card13, % (card1 - card7) ; <----------------------------------------------

    GuiClose:
ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 139 guests