[library] TF: Text files & Variables (strings) v3.8

Post your working scripts, libraries and tools for AHK v1.1 and older
Guest

Re: [library] TF: Text files & Variables (strings) v3.7

07 Feb 2018, 05:19

You need to learn the basics of AutoHotkey - you can't just use A_LoopReadLine like that, you need to have it "outside" of the quotes otherwise it won't be "translated" in the contents. As A_LoopReadLine may also contain new line characters you may need to trim

Code: Select all

TF_RegExReplace("!" A_LoopFileFullPath,"im)\b" trim(A_LoopReadLine,"`r`n") "\b","lock") ; use trim to remove trailing new line chars which may interfere
If it is a word list you are processing you can do it all in one go but you need to study and learn RegEx (hint|list|of|words|like|this) - see regex docs linked to above.
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: [library] TF: Text files & Variables (strings) v3.7

07 Feb 2018, 05:53

Thanks Guest :bravo:

I know i should have keep the variables outside the quotes from my first exmaple :lol: :lol: :lol:
(although i posted my sample code into the quotes just so you see my sample code).

anyway, what i needed is Trim :)

ps. why you using new lines (,"`r`n") ?
tested it without it and works as well
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: [library] TF: Text files & Variables (strings) v3.7

26 Jul 2018, 15:15

I have not been able to use it. Can someone explain it in detail? Could you give me a working example? I really need this library. I'm new to Autohotkey. But I'm so bored with that.

How do we include the library in our work? I did, but things did not work and things went wrong. "C: \ Users \ hasan \ Desktop \ Deneme.ahk (19): ==> Call to nonexistent function.
     Specifically: TF_Find (FilePath, "", "", "i) \ Q" SearchText "\ E", 0, 1)

Thanks.
Guest

Re: [library] TF: Text files & Variables (strings) v3.7

27 Jul 2018, 02:33

Its pretty straight forward.

You can either use #include or use the LIB folder:

1. You can either use #include path-to-your-copy-of\tf.ahk * in your script

* if you have your script and tf.ahk in the same folder #include tf.ahk will work of course

2. Place a copy of tf.ahk in one of the following folders on your computer:

%A_ScriptDir%\Lib\ ; Local library - requires [v1.0.90+].
%A_MyDocuments%\AutoHotkey\Lib\ ; User library.
directory-of-the-currently-running-AutoHotkey.exe\Lib\

Source & More info https://autohotkey.com/docs/Functions.htm#include

Working sample script

Code: Select all

FileDelete, tftestfile123.txt
FileAppend, 
(
01 AutoHotkey is a free, open-source utility for Windows. With it, you can:
02 - Automate almost anything by sending keystrokes and mouse clicks.
03 - Create hotkeys for keyboard, joystick, and mouse. Virtually any key, button, or combination can become a hotkey.
04 - Expand abbreviations as you type them. For example, typing "btw" can automatically produce "by the way".
05 - Create custom data-entry forms, user interfaces, and menu bars. See GUI for details.
06 - Remap keys and buttons on your keyboard, joystick, and mouse.
07 - Respond to signals from hand-held remote controls via the WinLIRC client script.
08 - Convert any script into an EXE file that can be run on computers that don't have AutoHotkey installed.
), tftestfile123.txt


; TF_Find(FilePath, "", "", "i)\Q" SearchText "\E")
MsgBox % TF_Find("tftestfile123.txt", "", "", "abbreviations") ; remember it is case sensitive by default
MsgBox % TF_Find("tftestfile123.txt", "", "", "and",0) ; return a CSV list of all lines with 'and'
MsgBox % TF_Find("tftestfile123.txt", "5", "", "AutoHotkey") ; start at line 5 so it skips the first result e.g. line 1 here

myvar:="autohotkey" ; lowercase

MsgBox % TF_Find("tftestfile123.txt", "", "", "i)\Q" myvar "\E",0,1) ; return the text of all found lines 
Karner
Posts: 13
Joined: 25 Nov 2018, 04:12

Re: [library] TF: Text files & Variables (strings) v3.7

25 Nov 2018, 04:32

Hello to all, my newbie post to new AHK forum...

I am trying to reformat MS Outlook's (multi-) email reply to a more readable layout...

I am new to TF too, reading a lot, but couldn't solve following problem:

How to find and replace rows containing text with various parts (bold/underlined):
Am 22.11.2018 um 12:36 schrieb "[email protected]" <[email protected]>:
...
Am 11.10.2018 um 11:11 schrieb "[email protected]" <[email protected]>:
...
It shall result in:
-----Original-Message-----
Am 22.11.2018 um 12:36 schrieb "[email protected]" <[email protected]>:
...
-----Original-Message-----
Am 11.10.2018 um 11:11 schrieb "[email protected]" <[email protected]>:
...
with -----Original-Message----- inserted.

Thx for help and best regards!
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.7

25 Nov 2018, 07:35

Be sure to make backups so you don't loose the source material. This works with a text file so not sure how your source material looks (if its msg, eml or html it can be a lot trickier due to all the formatting).

Code: Select all

FileDelete, tf-testemail.txt ; create a test file
FileAppend, 
(
Am 22.11.2018 um 12:36 schrieb "[email protected]" <[email protected]>:
lorem impsum
Am 11.10.2018 um 11:11 schrieb "[email protected]" <[email protected]>:
doler factum
), tf-testemail.txt

TF_RegExReplace("tf-testemail.txt","iUm)^Am (\d{1,2})\.(\d{1,2})\.(\d{4})","-----Original-Message-----`nAm $1.$2.$3")

; now you should have a file tf-testemail_copy.txt which looks like you want it, if you use TF_RegExReplace("!tf-testemail.txt",.... it will OVERWRITE the source file
Look up meaning of the RegExReplace options here https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
Karner
Posts: 13
Joined: 25 Nov 2018, 04:12

Re: [library] TF: Text files & Variables (strings) v3.7

25 Nov 2018, 12:27

Thank you very much, it is working for example file :) .

But how to append function to clipboard content?
Something like this?
Clipboard:=TF_RegExReplace(Clipboard...
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.7

25 Nov 2018, 12:46

Yes, that should work.
Karner
Posts: 13
Joined: 25 Nov 2018, 04:12

Re: [library] TF: Text files & Variables (strings) v3.7

25 Nov 2018, 13:55

Code: Select all

TF_RegExReplace(Clipboard,"iUm)^Am (\d{1,2})\.(\d{1,2})\.(\d{4})","-----Original-Message-----`nAm $1.$2.$3")
is not working :? There must be a syntax misunderstanding!?
Thx + regards.
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.7

25 Nov 2018, 14:14

No it works, as I mentioned if it is a different format (and the clipboard can hold various formats and it doesn't have to include text as such) it may not work. If it is only the clipboard you're working with you have no need for TF as TF simply wraps the standard AHK RegExReplace function

Code: Select all

Clipboard=
(join`r`n
Am 22.11.2018 um 12:36 schrieb "[email protected]" <[email protected]>:
lorem impsum
Am 11.10.2018 um 11:11 schrieb "[email protected]" <[email protected]>:
doler factum
)

clipboard:=TF_RegExReplace(clipboard,"iUm)^Am (\d{1,2})\.(\d{1,2})\.(\d{4})","-----Original-Message-----`nAm $1.$2.$3")

MsgBox % Clipboard
if remove TF_ its the same

Code: Select all

clipboard:=RegExReplace(clipboard,"iUm)^Am (\d{1,2})\.(\d{1,2})\.(\d{4})","-----Original-Message-----`nAm $1.$2.$3")
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.8

11 Dec 2020, 13:47

TF v3.8 - Fix to prevent TF_Sort removing last character @ https://github.com/hi5/TF/
-- 11 year old bug it seems :)
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: [library] TF: Text files & Variables (strings) v3.8

14 Jan 2021, 13:26

hello,

i have a txt file with some same words,
i want to count only these words to a number

file contanis:

1
2
3
4
4
4
5
6
7

i want to count how many "4" i have in the file,

result should be 3
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.8

14 Jan 2021, 14:48

Well, there is https://github.com/hi5/TF#TF_Count but as you will have seen it doesn't support files only variables (strings).
But we can make use of TF() https://github.com/hi5/TF#TF to read the contents of a file into a variable and use that - see also section underneath "Introduced in v3: TF()"

Notes:
- It uses a Simple StringReplace to count, so no RegEx and it will count part of a word as shown below
- TF() creates a global variable, t by default, but to store the WordCount I've set it to "WordCount". As the result is stored in the same variable you don't have to clear the variable afterwards.

Code: Select all

text=
(join`r`n
1
2
3
4
Word4
4
5
6
7
)

FileDelete, Test_TestFile.txt
FileAppend, % text, Test_TestFile.txt

; Don't do this, just fyi re the use of TF()
; if we did this, there would now be a (global) variable "t" with the contents
; of the file as it simply does a FileRead. In this case you should clear the 
; variable by using t:="" 
; WordCount:=TF_Count(TF("Test_TestFile.txt"),"4") 

; by using WordCount to read the file contents into AND then 
; store the result of the count, we don't have to clear any 
; variables afterwards
WordCount:=TF_Count(TF("Test_TestFile.txt", "WordCount"),"4") 

MsgBox % WordCount
User avatar
Tomer
Posts: 366
Joined: 21 Aug 2016, 05:11

Re: [library] TF: Text files & Variables (strings) v3.8

18 Jan 2021, 09:42

ahk7 wrote:
14 Jan 2021, 14:48
Well, there is https://github.com/hi5/TF#TF_Count but as you will have seen it doesn't support files only variables (strings).
But we can make use of TF() https://github.com/hi5/TF#TF to read the contents of a file into a variable and use that - see also section underneath "Introduced in v3: TF()"

Notes:
- It uses a Simple StringReplace to count, so no RegEx and it will count part of a word as shown below
- TF() creates a global variable, t by default, but to store the WordCount I've set it to "WordCount". As the result is stored in the same variable you don't have to clear the variable afterwards.

Code: Select all

text=
(join`r`n
1
2
3
4
Word4
4
5
6
7
)

FileDelete, Test_TestFile.txt
FileAppend, % text, Test_TestFile.txt

; Don't do this, just fyi re the use of TF()
; if we did this, there would now be a (global) variable "t" with the contents
; of the file as it simply does a FileRead. In this case you should clear the 
; variable by using t:="" 
; WordCount:=TF_Count(TF("Test_TestFile.txt"),"4") 

; by using WordCount to read the file contents into AND then 
; store the result of the count, we don't have to clear any 
; variables afterwards
WordCount:=TF_Count(TF("Test_TestFile.txt", "WordCount"),"4") 

MsgBox % WordCount
hero
thanks!

Edit:
https://www.autohotkey.com/boards/viewtopic.php?f=76&t=85909
mauriceo22
Posts: 2
Joined: 25 Sep 2021, 07:09

Re: [library] TF: Text files & Variables (strings) v3.8

06 Jan 2023, 10:51

Hey. Can u help please?
I try to use TF_Replace for my ahk file ( file with shortcuts ).
For examle, if i try to replace a simple string like "This my string", its working okay. Bat if i try to replace string with paragraphs, for example:
"This my string.

Text text text. One two tree
Lallala" , thats doesnt work ( because of Enters, i think? ). Maybe can u help me to solve it.
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.8

06 Jan 2023, 12:38

That is most likely the answer - apart from any tab/space characters that might be present which you don't "see" directly in the file. Note that text files can have various line endings which may cause an issue as well (it could be `r`n, `n, `r, or a combination of these in one single file (if you open a file, many editors should indicate this in the status bar CR LF CRLF or win/dos/mac/unix) If you are sure you're only working with one type you can use the TF_Replace pretty reliably, if it is unsure I'd suggest TF_RegExReplace so you're a bit more flexible.

Code: Select all

text=
(join`r`n
1 Hi this
2 a test variable
3 to demonstrate
4 how to 
5 use this
6 new version
7 of TF.AHK
)

FileDelete, Test_TestFile.txt
FileAppend, % text, Test_TestFile.txt

var1:="2 a test variable`r`n3 to demonstrate" ; this will work
var2:="2 a test variable`n3 to demonstrate"   ; this will fail
var3:="imsU)2 a test variable\r?\n\s*3 to demonstrate"   ; this will work, note the ? to indicate \r doesn't have to be present

TF_Replace("test_testfile.txt", var1, "this is the`r`nnew text1")
FileMove, test_testfile_copy.txt, test_testfile_copy_var1.txt, 1

TF_Replace("test_testfile.txt", var2, "this is the`r`nnew text2")
; var2 fails so no new file created

TF_RegExReplace("test_testfile.txt", var3, "this is the`r`nnew text3")
FileMove, test_testfile_copy.txt, test_testfile_copy_var3.txt, 1
mauriceo22
Posts: 2
Joined: 25 Sep 2021, 07:09

Re: [library] TF: Text files & Variables (strings) v3.8

10 Jan 2023, 05:17

ahk7 wrote:
06 Jan 2023, 12:38
That is most likely the answer - apart from any tab/space characters that might be present which you don't "see" directly in the file. Note that text files can have various line endings which may cause an issue as well (it could be `r`n, `n, `r, or a combination of these in one single file (if you open a file, many editors should indicate this in the status bar CR LF CRLF or win/dos/mac/unix) If you are sure you're only working with one type you can use the TF_Replace pretty reliably, if it is unsure I'd suggest TF_RegExReplace so you're a bit more flexible.

Code: Select all

text=
(join`r`n
1 Hi this
2 a test variable
3 to demonstrate
4 how to 
5 use this
6 new version
7 of TF.AHK
)

FileDelete, Test_TestFile.txt
FileAppend, % text, Test_TestFile.txt

var1:="2 a test variable`r`n3 to demonstrate" ; this will work
var2:="2 a test variable`n3 to demonstrate"   ; this will fail
var3:="imsU)2 a test variable\r?\n\s*3 to demonstrate"   ; this will work, note the ? to indicate \r doesn't have to be present

TF_Replace("test_testfile.txt", var1, "this is the`r`nnew text1")
FileMove, test_testfile_copy.txt, test_testfile_copy_var1.txt, 1

TF_Replace("test_testfile.txt", var2, "this is the`r`nnew text2")
; var2 fails so no new file created

TF_RegExReplace("test_testfile.txt", var3, "this is the`r`nnew text3")
FileMove, test_testfile_copy.txt, test_testfile_copy_var3.txt, 1
Oh, unfortunately, this method doesn't work for me. I have an ahk script with 3000+ rows ( hats a script with many templates for work in chat ).

its realized like this:

Code: Select all

::paytmiss::
paytmiss() {
    hs:= A_ThisHotkey
    paytmiss= 
    (LTrim
At the moment, your withdrawal request is pending.

Please have patience as withdrawals via PayTM may take any time between 1 to 5 days.

As soon as it is done processing, it should be credited to your account/wallet.
    )
    if (hs == "::paytmiss")
        {
        Clipboard := paytmiss
        Send, ^v
        }
    }
    
And i need to implement a task to make users correct templates byself (Without going into the code itself). Template is
'At the moment, your withdrawal request is pending.

Please have patience as withdrawals via PayTM may take any time between 1 to 5 days.

As soon as it is done processing, it should be credited to your account/wallet.'
ahk7
Posts: 574
Joined: 06 Nov 2013, 16:35

Re: [library] TF: Text files & Variables (strings) v3.8

10 Jan 2023, 12:20

Not sure that I understand what you're doing, and no doubt you can organise your code much more efficiently (for start define all variables at the start of the script or even in a separate file (CSV?) - not as part of each hotstring, but that is besides the point

One thing you could try is replacing (LTrim with (LTrim Join`r`n perhaps that already works, if so bob's your uncle

Or pass on the your hotstring to a helper function - here tfprocess() that puts it in a format so that it works with TF
You may need to play around with it to see what works.

Code: Select all

::paytmiss::
paytmiss() {
    hs:= A_ThisHotkey
    paytmiss= 
    (LTrim
At the moment, your withdrawal request is pending.

Please have patience as withdrawals via PayTM may take any time between 1 to 5 days.

As soon as it is done processing, it should be credited to your account/wallet.
    )
    if (hs == "::paytmiss")
        {
        Clipboard := paytmiss
        Send, ^v
        }
	find:="This my string"
	tfprocess(find,paytmiss)

    }

; new functions that takes hotstring and prepares it for TF

tfprocess(f,replace)
	{
	 replace:=StrReplace(replace,"`n","`r`n") ; play around with it, perhaps \r\n etc or make it regex etc
	 TF_Replace(file,f,replace)
	}
Jasonosaj
Posts: 46
Joined: 02 Feb 2022, 15:02
Location: California

Re: [library] TF: Text files & Variables (strings) v3.8

20 Jul 2023, 19:17

Has anyone taken a crack at porting this one over to V2? I use it in my autocorrect script to get rid of duplicates, keep my strings in alphabetical order, and preserve the order for the current code. Would love not having to convert the library but can take a crack at it if nobody else has.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 98 guests