Help with reading a file Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Help with reading a file

Post by Tomer » 19 Jan 2021, 13:31

i want to get in a msgbox only the SESSIONNAME of the user "test_user",

in my sample code i managed to get only the entire line,
any help would be appreciated.

p.s - the sample txt file in the code is dynamist, more lines of users can appear.

Code: Select all

FileDelete, c:\temp\querytest.txt

FileAppend,
(
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 12345678                                  1  Disc         1:14  19/01/2021 16:38
 test_user             rdp-tcp#13          2  Active          .  14/01/2021 19:43
 
),c:\temp\querytest.txt


FileRead, OutputVar, c:\temp\querytest.txt
Loop, Parse, OutputVar, `n,`r
{
FileLine = %A_LoopField%
Lookfor := "test_user" ; im looking for the SESSIONNAME of "test_user" which should be "rdp-tcp#13"
    IfInString, FileLine, %Lookfor%
	{
		MsgBox % A_LoopField
	}
}


User avatar
mikeyww
Posts: 5382
Joined: 09 Sep 2014, 18:38

Re: Help with reading a file

Post by mikeyww » 19 Jan 2021, 13:46

Code: Select all

file := "c:\temp\querytest.txt", session := {}
If !FileExist(file) {
 MsgBox, 48, Error, File not found.`n`n%file%
 Return
}
Loop, Read, %file%
 RegExMatch(A_LoopReadLine, "([\w-#]+)\s+([\w-#]+)", part), session[part1] := part2
MsgBox, 64, % "Session name for " user := "test_user", % session[user]

garry
Posts: 2630
Joined: 22 Dec 2013, 12:50

Re: Help with reading a file

Post by garry » 19 Jan 2021, 13:50

@mikeyww thank you , I tried this

Code: Select all

;- Help with reading a file 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=85851

f1=%a_scriptdir%\querytest.txt
ifexist,%f1%
FileDelete,%f1%
FileAppend,
(
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 12345678                                  1  Disc         1:14  19/01/2021 16:38
 test_user             rdp-tcp#13          2  Active          .  14/01/2021 19:43
 98765432                                  1  Disc2        1:14  19/01/2021 16:38
 test_user             rdp-tcp#14          2  Active2         .  14/01/2021 19:43
 
),%f1%
FileRead, OutpurVar,%f1%
Lookfor := "test_user" ; im looking for the SESSIONNAME of "test_user" which should be "rdp-tcp#13"
Loop, Parse, OutpurVar, `n,`r
{
FileLine = %A_LoopField%
IfInString, FileLine, %Lookfor%
  {
  xc:=RegExReplace(fileline,"\x20{2,}"," ")
  stringsplit,a,xc,%a_space%
  e .= a2 . "`r`n"	  
  }
}
msgbox,%e%
exitapp

User avatar
mikeyww
Posts: 5382
Joined: 09 Sep 2014, 18:38

Re: Help with reading a file

Post by mikeyww » 19 Jan 2021, 14:13

Good. Did it work? If not, you can provide a complete description of exactly what should happen, based on the text file that you are showing.

garry
Posts: 2630
Joined: 22 Dec 2013, 12:50

Re: Help with reading a file

Post by garry » 19 Jan 2021, 15:59

@mikeyww yes it works for me , the variable 'xc' allow only one space , use then stringsplit and variable 'e' collect column-2 with sessionname ( if line contains 'test_user' )
remark : I copied the text , not sure if contains really only space or maybe TAB's ?
Your script works also fine ( only not collect result for more lines )

User avatar
mikeyww
Posts: 5382
Joined: 09 Sep 2014, 18:38

Re: Help with reading a file

Post by mikeyww » 19 Jan 2021, 17:07

OK, very nice. If you have tabs, you can always use RegEx or just change the tabs to spaces, or spaces to tabs, and then adjust your code if needed. Thank you for the feedback. I did not realize that you would have duplicates, so your approach is more accommodating to that.

User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Re: Help with reading a file

Post by Tomer » 20 Jan 2021, 07:57

Thank you both!

User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Re: Help with reading a file

Post by Tomer » 20 Jan 2021, 08:37

@garry

can you replace it by the STATE "Active" instead of the USERNAME "test_user" to get the current SESSIONNAME ?

Sample of txt file to read

Code: Select all

f1=%a_scriptdir%\querytest.txt
ifexist,%f1%
FileDelete,%f1%
FileAppend,
(
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 12345678                                  1  Disc        21:10  19/01/2021 16:38
 test_user                                 2  Down           30  14/01/2021 19:43
>test_user             rdp-tcp#19          7  Active          .  20/01/2021 15:22

),%f1%
Look for the SESSIONNAME of the active user only:

Code: Select all

Lookfor := "Active"
Thanks!
Last edited by Tomer on 20 Jan 2021, 08:55, edited 1 time in total.

garry
Posts: 2630
Joined: 22 Dec 2013, 12:50

Re: Help with reading a file

Post by garry » 20 Jan 2021, 08:53

@Tomer I tried this , see USERNAME SESSIONNAME if ACTIVE

Code: Select all

;- Help with reading a file 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=85851
f1=%a_scriptdir%\querytest.txt
ifnotexist,%f1%
{
FileAppend,
(
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 12345678                                  1  Disc         1:14  19/01/2021 16:38
 test_userCCC          rdp-tcp#13          2  Active          .  14/01/2021 19:43
 98765432                                  1  Disc         1:22  22/01/2021 17:22
 test_userDDD          rdp-tcp#14          2  Passive         .  17/01/2021 21:05
 32165431                                  1  Disc         1:45  24/01/2021 17:24
 test_userEEE          rdp-tcp#15          2  Active          .  18/01/2021 21:07
 
),%f1%,UTF-8
}
FileRead, OutpurVar,%f1%
Lookfor := "Active"                 ; im looking for the SESSIONNAME of "test_user" which should be "rdp-tcp#13"
Loop, Parse, OutpurVar, `n,`r
{
FileLine = %A_LoopField%
IfInString, FileLine, %Lookfor%
  {
  xc:=RegExReplace(fileline,"\x20{2,}"," ")
  stringsplit,a,xc,%a_space%
  e .= a1 . " = " . a2 . " is " . a4 . "`r`n"	  
  }
}
msgbox,%e%
exitapp

User avatar
Tomer
Posts: 336
Joined: 21 Aug 2016, 05:11

Re: Help with reading a file

Post by Tomer » 20 Jan 2021, 08:58

Thanks @garry

but there not could be more than 1 same active user

i need to get in a msgbox only the SESSIONNAME of the active user without any other text without any spaces or tabs.

see my sample txt file in last post (editted)

thanks!

edit:
may this will do the job:

Code: Select all

f1=%a_scriptdir%\querytest.txt
ifexist,%f1%
FileDelete,%f1%
FileAppend,
(
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 12345678                                  1  Disc        21:10  19/01/2021 16:38
 test_user                                 2  Down           30  14/01/2021 19:43
>test_user             rdp-tcp#19          7  Active          .  20/01/2021 15:22

),%f1%

FileRead, OutpurVar,%f1%
Lookfor := "Active"                 ; im looking for the SESSIONNAME of "test_user" which should be "rdp-tcp#13"
Loop, Parse, OutpurVar, `n,`r
{
FileLine = %A_LoopField%
IfInString, FileLine, %Lookfor%
  {
  xc:=RegExReplace(fileline,"\x20{2,}"," ")
  stringsplit,a,xc,%a_space%
   e .= a2 . "`r`n"	  
  }
}
msgbox,%e%
exitapp

garry
Posts: 2630
Joined: 22 Dec 2013, 12:50

Re: Help with reading a file  Topic is solved

Post by garry » 20 Jan 2021, 09:04

@Tomer maybe only this ... sorry I was few minutes to late ...

Code: Select all

;- Help with reading a file 
;- https://www.autohotkey.com/boards/viewtopic.php?f=76&t=85851
f1=%a_scriptdir%\querytest2.txt
ifnotexist,%f1%
{
FileAppend,
(
 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
 12345678                                  1  Disc        21:10  19/01/2021 16:38
 test_user                                 2  Down           30  14/01/2021 19:43
 test_user             rdp-tcp#19          7  Active          .  20/01/2021 15:22
),%f1%,UTF-8
}
FileRead, OutpurVar,%f1%
Lookfor := "Active"                 ; im looking for the SESSIONNAME of "test_user" which should be "rdp-tcp#13"
Loop, Parse, OutpurVar, `n,`r
{
FileLine = %A_LoopField%
IfInString, FileLine, %Lookfor%
  {
  xc:=RegExReplace(fileline,"\x20{2,}"," ")
  stringsplit,a,xc,%a_space%
  ;e .= a1 . " = " . a2 . " is " . a4 . "`r`n"
  e .= a2 . "`r`n"	  
  }
}
msgbox,%e%
exitapp

Post Reply

Return to “Ask For Help”