get a specific line from a folder full of textfiles

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JaxD
Posts: 9
Joined: 28 Jul 2019, 01:21

get a specific line from a folder full of textfiles

02 Aug 2019, 11:41

Hallo,

i have a problem. I'm struggeling to get a specific line from a folder full of textfiles to add in a new text file!
The Problem is that with the code below the script only weites 9 times the right line (from the first 9 textfiles) to the new document. after that the script rewrites the other lines with lines from the first textfiles

Code: Select all

    FileDelete, temp_scr.txt 
    
    Loop,  temp\*.*, ,1  
    {
       Loop, read, %A_LoopFileFullPath%, temp_scr.txt
       IfInString, A_LoopReadLine, nextline ; nextline is the needle in the textfiles
            {
            FileAppend, %A_LoopReadLine%`n 
            break  ;> max 9 times, then overwrite from line 1 again!
            }
    }


sry for my bad english i hope you understand my problem. I wand to loop through a folder full of textfiles, search for a specific word in each of these textfiles and add the lines with this specific word to a new textfile.

thank you for your help!
JaxD
Posts: 9
Joined: 28 Jul 2019, 01:21

Re: get a specific line from a folder full of textfiles

02 Aug 2019, 12:36

ok i now know what my problem is. its all about the filenames.

i have many textfiles with "name1, name2, etc, when i rename them to name01, name02, etc.. it works!

is there a way to create filenames wit %a_index% with a zero infront? or maybe change my code that he do not count 1 and then 10 instead 1, 2, 3`?
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

Re: get a specific line from a folder full of textfiles

02 Aug 2019, 16:20

Hello JaxD,

there is a simple way to create numbers with leading zeros, take a look at the built-in format function.

But there is also another way to do this which can actually be used to add a fixed length pad of any type to a string. (Not just for numbers)

It works by specifying the fixed length pad, taking a portion as big as the “remaining space” (pad length - string length) from the pad and concatenating it to the string.

Example:

Code: Select all

num := 5
numLength := StrLen(num)

pad := "000"
padLength := StrLen(pad)

leadingZeroNum := SubStr(pad, 1, Max(0, padLength - numLength)) . num
It will pad the input number with three digits of zero.
"Nothing is quieter than a loaded gun." - Heinrich Heine
JaxD
Posts: 9
Joined: 28 Jul 2019, 01:21

Re: get a specific line from a folder full of textfiles

03 Aug 2019, 06:27

Hello Alibaba,thank you for your reply. I will get a look at the format function. Meanwhile i have managed to solve my problem by using this line of code, based on the info you gave me in your post above. Thanks again.

Code: Select all

count:=SubStr("00" . A_Index, -1)
Alibaba
Posts: 480
Joined: 29 Sep 2013, 16:15
Location: Germany

Re: get a specific line from a folder full of textfiles

03 Aug 2019, 06:48

Glad I could help. You have a nice solution. But keep in mind that there could be problems when A_Index goes beyond 99.
"Nothing is quieter than a loaded gun." - Heinrich Heine

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: niCode, peter_ahk and 166 guests