TF v3.8 - AutoHotkey library for Text files & Variables (strings)
Library and documentation at https://github.com/hi5/TF
As the name suggest this is a AHK Library with a number of functions to "manipulate" text, both files such as *.txt, *.ahk, *.html, *.css etc AND Strings (or variables). It is NOT useful for binary files or data such as MS Office files, PDFs, EXEcutables, images etc.
By using a matchlist* for the lines you want to process for the majority of the functions it is flexible in use, but it does make it a bit slower for large files or variables. If speed is of great importance this may not be the most suitable library.
* The matchlist is generated automatically in each function based on the parameters.
Changelog v3.8
- Prevent TF_Sort from removing last character from the last line https://github.com/hi5/TF/issues/11
Changelog v3.7
- Added: additional check for passing on just zero/zeros as text to TF functions in TF_GetData() - see v3.6
- Fix: changed readme.md to fix rendering issue of MD files on GH - https://github.com/hi5/TF/issues/5
Changelog v3.5
- Changed A_ScriptDir to A_WorkingDir in TF_ReturnOutPut - https://github.com/hi5/TF/issues/1
- Fixed: TF_ColGet negative startcolumn and sections now work correctly
Available Functions
TF
TF_CountLines
TF_Count
TF_ReadLines
TF_Tail
TF_Replace
TF_ReplaceInLines
TF_RegExReplace
TF_RegExReplaceInLines
TF_RemoveLines
TF_RemoveBlankLines
TF_RemoveDuplicateLines
TF_InsertLine
TF_ReplaceLine
TF_LineNumber
TF_InsertPrefix
TF_InsertSuffix
TF_TrimLeft
TF_TrimRight
TF_AlignLeft
TF_AlignCenter
TF_AlignRight
TF_ConCat
TF_ColGet
TF_ColPut
TF_ColCut
TF_ReverseLines
TF_Find
TF_SplitFileByLines
TF_SplitFileByText
TF_Merge
TF_Prepend
TF_Append
TF_Substract
TF_WhiteSpace
TF_Wrap
TF_RangeReplace
TF_MakeFile
TF_Tab2Spaces
TF_Spaces2Tab
TF_Sort
TF_Save
TF_Join
Keywords: FileDeleteLine Deleteline Delete Line Lines Text file duplicate remove trim split merge replace align sort wrap append prepend find column columns variable strings whitespace leading trailing reverse tail count insert read grep join
[library] TF: Text files & Variables (strings) v3.8
[library] TF: Text files & Variables (strings) v3.8
Last edited by ahk7 on 11 Dec 2020, 13:46, edited 3 times in total.
Re: [library] TF: Text files & Variables (strings) v3.4
Some of these functions can be kinda slow on big files, but this is a very useful lib
Re: [library] TF: Text files & Variables (strings) v3.4
Hi there, How would I use TF_Find on multiple files and display the results all together in one msgbox? I have this loop:
I can get:
1. results displayed in a separate msgbox for each file searched.
OR
2. results displayed in one msgbox but only for the last file in the list.
I can't figure out how to display all the results together so any help would be much appreciated
Code: Select all
FileSelectFile, Files, M 1,%StartFolder%, Select the files to search, *.ahk
InputBox, SearchText, Search File Contents,
loop, Parse, Files, `n
{
if a_index != 1
{
FilePath = %FolderPath%\%A_LoopField%
result = % TF_Find(FilePath, "","", SearchText, 0, 1)
}
}
return
1. results displayed in a separate msgbox for each file searched.
OR
2. results displayed in one msgbox but only for the last file in the list.
I can't figure out how to display all the results together so any help would be much appreciated
Re: [library] TF: Text files & Variables (strings) v3.4
You can "Concatenate" the results variable - see http://ahkscript.org/docs/Variables.htm#Operators
As a side note: TF_Find uses Regular Expressions so by its default case sensitive, so in your case if you simply type "msgbox" in the input it won't find "MsgBox" - therefore I've wrapped SearchText with some default options in the code above to save you the hassle of typing it each time "i)\Q" SearchText "\E" you can alter this to suit your needs of course. You could also return line numbers - See also https://github.com/hi5/TF#TF_Find
Code: Select all
FileSelectFile, Files, M 1,%StartFolder%, Select the files to search, *.ahk
InputBox, SearchText, Search File Contents,
result:=""
loop, Parse, Files, `n
{
if a_index != 1
{
FilePath = %FolderPath%\%A_LoopField%
Result .= "`n--------------------" A_LoopField ":`n" TF_Find(FilePath, "","", "i)\Q" SearchText "\E", 0, 1)
}
}
MsgBox % result
return
Re: [library] TF: Text files & Variables (strings) v3.4
Thanks ahk7!! I did actually manage to figure it out (along with removing duplicate results) which was a pat-on-the-back moment for me I used a temporary text file but I like yours better because it removes the need for that. Cool!!
And yes it took me while to realise that the default for TF_Find was case-sensitive. Then it took me another wee while to find the code in TF.ahk to turn it off. I didn't really want it permanently turned off but wasn't sure how to tackle it and you just solved that problem for me!!! Thanks heaps!!!
And yes it took me while to realise that the default for TF_Find was case-sensitive. Then it took me another wee while to find the code in TF.ahk to turn it off. I didn't really want it permanently turned off but wasn't sure how to tackle it and you just solved that problem for me!!! Thanks heaps!!!
Re: [library] TF: Text files & Variables (strings) v3.5
Updated to v3.5
- Changed A_ScriptDir to A_WorkingDir in TF_ReturnOutPut - https://github.com/hi5/TF/issues/1
- Fixed: TF_ColGet negative startcolumn and sections now work correctly
https://github.com/hi5/TF
- Changed A_ScriptDir to A_WorkingDir in TF_ReturnOutPut - https://github.com/hi5/TF/issues/1
- Fixed: TF_ColGet negative startcolumn and sections now work correctly
https://github.com/hi5/TF
Re: [library] TF: Text files & Variables (strings) v3.5
Thanks to all previous authors and of course to you, ahk7. I use this lib very often. Hope it will be there as long as AHK itself...
Re: [library] TF: Text files & Variables (strings) v3.5
Glad you like it - as soon as specifications for AHK v2 is are "final" I will prepare a AHK v2 compatible version
Re: [library] TF: Text files & Variables (strings) v3.5
Glad to hear it. I wont be migrating to AHK2 instantly but I will be in the end. Can I have one request? Couldnt you add function to remove all the diacritics from strings? Like ěščřžýáíé etc. It means ě or é to e, š to s etc.
Re: [library] TF: Text files & Variables (strings) v3.5
I managed to do it by myself in the end -> http://ahkscript.org/boards/viewtopic.php?f=6&t=8089
Thanks.
Thanks.
Re: [library] TF: Text files & Variables (strings) v3.5
Indeed it is in principle a series of find/replace actions - you can now call your function from other TF functions, here TF reads file.txt, passes it on to your function, which in turn is passed on to TF_Save
Code: Select all
TF_Save(RemoveLetterAccents(TF("file.txt")),"file.txt")
; just to note this will create a global variable 't' - https://github.com/hi5/TF#TF
Re: [library] TF: Text files & Variables (strings) v3.5
Hi all,
Sorry to bump this thread but i have a little problem to manage it in my project ! I will try to explain you my goal :
I have a txt file where i want to modify values with TF_ReplaceInLines.
My text file look like something like this :
I would like to modify the values by another and this is what i tried :
And in this way that doesn't work
Then if i try this code :
Another error appear in the TF.ahk @line 81 : OutPut .= LoopField "`n"
Perhaps i do it bad so if someone can help me please i will appreciate it !
Edit: Well i manage it it was simply the activate#Warn sorry for disturb you guys !
Thank you in advance, regards.
Sorry to bump this thread but i have a little problem to manage it in my project ! I will try to explain you my goal :
I have a txt file where i want to modify values with TF_ReplaceInLines.
My text file look like something like this :
Code: Select all
value1 = 15;
value2= 5000;
value3= 1000;
value4= true;
value5= false;
value6= 50;
value7= 80;
value8= 40;
Code: Select all
FileSelectFile, SelectedFileSettings, 3, %A_WorkingDir%, Open a file, settings.txt
FileContents := SelectedFileSettings
TF_ReplaceInLines("!FileContents", "1", "", "15", "20")
Then if i try this code :
Code: Select all
FileSelectFile, SelectedFileSettings, 3, %A_WorkingDir%, Open a file, settings.txt
FileContents := SelectedFileSettings
TF_ReplaceInLines("!" FileContents, "1", "", "15", "20")
Perhaps i do it bad so if someone can help me please i will appreciate it !
Edit: Well i manage it it was simply the activate#Warn sorry for disturb you guys !
Thank you in advance, regards.
-
- Posts: 2
- Joined: 16 Nov 2015, 19:08
Re: [library] TF: Text files & Variables (strings) v3.5
Hello,
TF_ReplaceLine creates a new file, it does not modify the initial file.
TF_ReplaceLine creates a new file, it does not modify the initial file.
Re: [library] TF: Text files & Variables (strings) v3.5
@Depeche_Mode
In principle that is correct, by default all functions create a copy of the original file for safety reasons so you can first test it, only if you're sure the command works correctly you can tell TF to overwrite the source file by adding a ! in front of your file name.
You can read more about it here: (! prefix)
https://github.com/hi5/TF#quick-intro-to-parameters
https://github.com/hi5/TF#textfile-and-the--prefix
In principle that is correct, by default all functions create a copy of the original file for safety reasons so you can first test it, only if you're sure the command works correctly you can tell TF to overwrite the source file by adding a ! in front of your file name.
You can read more about it here: (! prefix)
https://github.com/hi5/TF#quick-intro-to-parameters
https://github.com/hi5/TF#textfile-and-the--prefix
Re: [library] TF: Text files & Variables (strings) v3.4
Hi, how you have changed the code in TF.ahk to turn off case sensitive?kiwichick wrote:Thanks ahk7!! I did actually manage to figure it out (along with removing duplicate results) which was a pat-on-the-back moment for me I used a temporary text file but I like yours better because it removes the need for that. Cool!!
And yes it took me while to realise that the default for TF_Find was case-sensitive. Then it took me another wee while to find the code in TF.ahk to turn it off. I didn't really want it permanently turned off but wasn't sure how to tackle it and you just solved that problem for me!!! Thanks heaps!!!
thank you
Re: [library] TF: Text files & Variables (strings) v3.5
@ness_1: There is no need to modify TF.ahk to "turn it off". Depending on the TF function used you can simply make use of AutoHotkey options and settings. (Regular expressions and StringCaseSense)
If you are using TF_Find for example: it uses Regular Expressions which by default is case-sensitive. If you don't want that, simply use the "i)" option.
So TF_Find("File.txt", , , "keys") will find 'keys' but not 'Keys' or 'KEYS'
If you want to make it case-insensitive use "i)":
TF_Find("File.txt", , , "i)keys")
See https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
and https://github.com/hi5/TF#TF_Find (tip on literal search \Q...\E, see also a few posts above)
If you are using TF_Find for example: it uses Regular Expressions which by default is case-sensitive. If you don't want that, simply use the "i)" option.
So TF_Find("File.txt", , , "keys") will find 'keys' but not 'Keys' or 'KEYS'
If you want to make it case-insensitive use "i)":
TF_Find("File.txt", , , "i)keys")
See https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
and https://github.com/hi5/TF#TF_Find (tip on literal search \Q...\E, see also a few posts above)
Re: [library] TF: Text files & Variables (strings) v3.5
Great!! Thank you, it solve my problemGuest wrote:@ness_1: There is no need to modify TF.ahk to "turn it off". Depending on the TF function used you can simply make use of AutoHotkey options and settings. (Regular expressions and StringCaseSense)
If you are using TF_Find for example: it uses Regular Expressions which by default is case-sensitive. If you don't want that, simply use the "i)" option.
So TF_Find("File.txt", , , "keys") will find 'keys' but not 'Keys' or 'KEYS'
If you want to make it case-insensitive use "i)":
TF_Find("File.txt", , , "i)keys")
See https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
and https://github.com/hi5/TF#TF_Find (tip on literal search \Q...\E, see also a few posts above)
Re: [library] TF: Text files & Variables (strings) v3.5
TF_ReplaceLine replaces all lines in the file with a zero per line
Re: [library] TF: Text files & Variables (strings) v3.5
Most likely a syntax error or the lines you want to replace aren't there, if you have a file with three lines, don't expect it to work when you want to replace line 10 for example.
The test script below works for me:
Before:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
After:
Line 1
new line 2
Line 3
new lines to replace lines 4 to 6
new lines to replace lines 4 to 6
new lines to replace lines 4 to 6
If you still believe it fails, post your (test) script to replicate the problem, perhaps there is a simple solution.
The test script below works for me:
Code: Select all
; create a test file, 6 lines
Loop, 6
FileAppend, Line %A_Index%`n,TFTESTFILE-replaceline.txt
; Syntax:
; TF_ReplaceLine(Text, StartLine = 1, Endline = 0, ReplaceText = "")
TF_ReplaceLine("!TFTESTFILE-replaceline.txt",2,2,"new line 2")
TF_ReplaceLine("!TFTESTFILE-replaceline.txt",4,6,"new lines to replace lines 4 to 6")
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
After:
Line 1
new line 2
Line 3
new lines to replace lines 4 to 6
new lines to replace lines 4 to 6
new lines to replace lines 4 to 6
If you still believe it fails, post your (test) script to replicate the problem, perhaps there is a simple solution.
Re: [library] TF: Text files & Variables (strings) v3.5
I must be out of my mind, i was:
TF_ReplaceLine("!"Var, StartLine=9, EndLine=9, ReplaceText="testline")
Thank you Guest.
TF_ReplaceLine("!"Var, StartLine=9, EndLine=9, ReplaceText="testline")
Thank you Guest.
Return to “Scripts and Functions (v1)”
Who is online
Users browsing this forum: kiwichick and 72 guests