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
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
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
It shall result in:Am 22.11.2018 um 12:36 schrieb "[email protected]" <[email protected]>:
...
Am 11.10.2018 um 11:11 schrieb "[email protected]" <[email protected]>:
...
with -----Original-Message----- inserted.-----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]>:
...
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
Code: Select all
TF_RegExReplace(Clipboard,"iUm)^Am (\d{1,2})\.(\d{1,2})\.(\d{4})","-----Original-Message-----`nAm $1.$2.$3")
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
Code: Select all
clipboard:=RegExReplace(clipboard,"iUm)^Am (\d{1,2})\.(\d{1,2})\.(\d{4})","-----Original-Message-----`nAm $1.$2.$3")
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
heroahk7 wrote: ↑14 Jan 2021, 14:48Well, 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
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 ).ahk7 wrote: ↑06 Jan 2023, 12:38That 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
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
}
}
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)
}
Return to “Scripts and Functions (v1)”
Users browsing this forum: No registered users and 93 guests