Drawing a blank! AHK Gods Unite!

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Drawing a blank! AHK Gods Unite!

12 Feb 2019, 15:18

I am drawing a blank. I have GUI that asks for a text file (haystack) and asks for some keywords to search each line of the file for. I basically want to read the file using Loop, Read (given it reads one line at a time and some files could be large), and then check each line of the file for each of the provided terms (which could vary each time), and if the line contains a term, have it write (fileappend) the entire line of that haystack file to a new log.

While I am sure I could hard code a bunch of individual checks for each terms, there has to be a way to loop through each of the terms or with some sort of OR so that the script can be dynamic depending on the terms provided each time.

If you could help me with sample code that I could play with, that would be a big help.

Code: Select all

Gui, Add, Text, x12 y9 w160 h20 , Technician Name (Last_First)*:
Gui, Add, Edit, x182 y9 w370 h20 vTECH,JOHN DOE
Gui, Add, Text, x12 y39 w330 h20 , Full Path of Log (drag file here)*:
Gui, Add, Edit, x12 y59 w570 h20 vFile,%A_Desktop%\Parser\haystack.txt 
Gui, Add, Text, x12 y99 w100 h20 , Terms (one per line)*:
Gui, Add, Edit, x12 y119 w570 h130 vterms,Big`nRed`nDog
Gui, Add, Button, x22 y259 w140 h40 gOK, Parse
Gui, Add, Button, x202 y259 w130 h40 gCancel, Cancel
; Generated using SmartGUI Creator 4.0
Gui, Show, x652 y408 h330 w605, %AppName%
Return

GuiClose:
ExitApp

GuiDropFiles:
 GuiControl,, File, %A_GuiEvent% ;This will only work properly if one file is dropped on the GUI. To handle several files being dropped, see help file on GuiDropFiles.
 return

Cancel:
exitapp

OK:
Gui,Submit,NoHide

♥ ❤ ❥ coder_chick ♥ ❤ ❥
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Drawing a blank! AHK Gods Unite!

12 Feb 2019, 15:46

You can have the data entered instead of one per line entered as keyword1,keyword2,keyword3

and then in your loop do

if A_LoopReadLine contains %terms% {
FileReadLine, TheLine, TheFileName, % a_index
FileAppend, % theline, YourfileName.txt
}
User avatar
coder_chick
Posts: 235
Joined: 05 Nov 2015, 10:43

Re: Drawing a blank! AHK Gods Unite!

12 Feb 2019, 17:33

The following does work, so that is promising. But is there a way I can take the input from the edit field (one term per line) and create the variable where they are delimited by comma. In other words, still allow the user to enter them in one per line, and once they push the button to execute, the script concatenates the terms with commas in between and then leverages it as shown below?

Code: Select all

terms = big,red,dog,apple

Loop, Read, %File%
{
if A_LoopReadLine contains %terms% 
{
FileAppend,%A_LoopReadLine%`n,log.txt
}
}
♥ ❤ ❥ coder_chick ♥ ❤ ❥
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Drawing a blank! AHK Gods Unite!

12 Feb 2019, 17:45

Alternatively, you could put the terms in an array/index-based object and use a for-loop.

Code: Select all

; sometime after submitting the GUI info
termsObj:=strSplit(terms,"`n")

; inside file-read loop
for i,a in termsObj{
    if(inStr(a_loopReadLine,a))
        fileAppend,% a_loopReadLine,file.txt
}
Considering that using a matchlist would also be looping through each item in the list anyway (behind the scenes), or using regex, speed/efficiency should be nearly the same.

Edit: if you'd rather use a matchlist, you can simply do terms:=strReplace(terms,"`n",",") to change from line-delimited to comma-delimited.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Drawing a blank! AHK Gods Unite!

12 Feb 2019, 17:54

coder_chick wrote:
12 Feb 2019, 17:33
The following does work, so that is promising. But is there a way I can take the input from the edit field (one term per line) and create the variable where they are delimited by comma. In other words, still allow the user to enter them in one per line, and once they push the button to execute, the script concatenates the terms with commas in between and then leverages it as shown below?

Code: Select all

terms = big,red,dog,apple

Loop, Read, %File%
{
if A_LoopReadLine contains %terms% 
{
FileAppend,%A_LoopReadLine%`n,log.txt
}
}

Code: Select all

#NoEnv
#SingleInstance Force
SetWorkingDir %A_ScriptDir%

Gui Add, Edit, x29 y10 w167 h68 Multi vTerms
Gui Add, Button, x68 y85 w80 h23 gConnect, &OK

Gui Show, w230 h133, Window
Return

connect:
gui, submit, nohide
loop, parse, Terms, `n
   NewTerms .= A_LoopField ","
msgbox, % NewTerms
return


GuiEscape:
GuiClose:
    ExitApp

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb, wineguy and 332 guests