Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

TF: Text file & Variables/String Library v3.4 [lib]


  • Please log in to reply
244 replies to this topic
SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

New TF thread
http://ahkscript.org...c.php?f=6&t=576

 

 

Code:

https://github.com/hi5/TF

 

Introduction

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.
(Tip: search the AHK forum for "binread" to find some pointers
on how to read, write and "edit" binary files.)

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.

Download

https://github.com/hi5/TF

Install / Use

You can either place TF.ahk in your LIB directory
(see http://www.AutoHotke...nctions.htm#lib) or use
#include (see http://www.AutoHotke...ds/_Include.htm)

Documentation

https://github.com/hi5/TF

 

Support

 

http://ahkscript.org...c.php?f=6&t=576

Examples

See post below for test script and examples of each function.

History

Update v3.3, 16 April 2010

- Fixed: If you used variables with any of the replacement functions or tried to remove empty lines from a VARIABLE in the following format: variable:=TF_..(variable,"search","replace") and the searchtext was NOT present in the variable it returned an empty variable (e.g. deleted the contents of "variable")

It affected TF_ReplaceInLines, TF_Replace, TF_RegExReplaceInLines, TF_RegExReplace, TF_RemoveBlankLines, TF_RangeReplace

The built-in check only worked correctly for FILES and with the introduction of variables in TF 3 this didn't surface during the test :-(

- Fixed: documentation error for TF_Merge (seperator and filename where swapped) and added examples on how to use TF_Merge in a Loop and with FileSelectFile

Update v3.2

- Fixed: TF_AlignRight, due to a bug it didn't work as it should have, it prepended the number of spaces rather than aligning the text at the specified width

- Changed: TF_SplitFileByLines: New options for SplitAt, now three methods available, see notes at function description

- Changed: TF_RemoveLines: New option for StartLine, if negative value is used it will remove the last X lines from file, see notes at function description

- Changed: TF_GetData (helper function) should now avoid unnecessary IfNotExist for files, for scripts with many loops in combination with variables it should improve the speed slightly

Update v3.1:

- Rewrite of TF_Find. Can now return multiple lines (like TF_Findlines used to), but not only line numbers but the entire line (text) of the found lines so it can be used as a basic grep.

- Deprecated: TF_FindLines (see change TF_Find). Kept in for backwards compatibility

TF_Find(Lines) compatibility note:

- Because the old TF_Find(Lines) did not use RegExMatch you may need to rewrite some of your SearchText parameters. This means that if you used "special" characters which have a special meaning in a RegEx: \.*?+[{|()^$ they must be preceded by a backslash to be seen as literal. For example, \. is a literal period and \\ is a literal backslash. Escaping can be avoided by using \Q...\E. For example: \QLiteral Text\E. See http://www.autohotke.../RegExMatch.htm for further information.

- The CaseSensitive paramaters has been dropped, if you used that update your call to TF_Find I'm affraid

v3 Complete overhaul of library, now accepts files & variables for input and output:

- Changed: New parameter for TF_Readlines & TF_Tail: trailing new line now optional
- Added: TF_Save, shorthand for filedelete+fileappend - HugoV
- Added: TF_GetData, helper function to determine if a file or a variable is passed on to function - HugoV
- Added: TF() To read a text file in global var, t by default - Credits various ...
- Added: TF_ReturnOutput has replaced: Overwrite, MakeCopy and the newly developed ReturnStr - HugoV
- Changed: Complete rewrite of TF_Tail, new options - borrowed bits from Tuncay (Thanks!)
- Changed: MakeMatchList: Removed TF_Countlines (one less fileread), Pass on "String" and not a TextFile
- Fix: TF_ConCat & TF_MakeFile didn't write output file :-( same bug as splitby* earlier.

Note: If you are using TF v2.4a, 10 August 2009, there is a minor bug
TF_MakeFile and TF_ConCat do not produce output files, so upgrade to v3.

You can use the examples in the post below to test the library and see how it works.

"TODO" (which means I may or may not)

- Have another look at TF_Substract, introduce delete entire line on partial match
- Deprecate TrimLeft & TrimRight and introduce Trim (which can trim left, right and
left & right at the same time as well as trimming specific characters)
- Col[get/cut/put] introduce option to process multiple columns in one go
- Look at some "grep" like functions, mainly extracting sections of data
(something like rangereplace)
- Convert delimited file to fixed columns (like in UltraEdit, will be only CSV like
function in TF)
- TF_SwapLines (work in progress)
- TF_SwapColumns (work in progress)
- TF_Reformat
- TF_Justify [if I'm really really really bored]

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_FindLines
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

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

This library is based on the Library for Text file manipulation started
by Heresy. I have contributed a number of functions to that library, but
that version also has some "bugs" which are hopefully resolved in this TF
library. Heresy seems to have dropped off the radar, so the original
library can not be updated at the moment.



SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
You can use the examples below to test the library and see how it works.
;#include tf.ahk ; remove comment if you don't include TF in your library
;*** Test script to demonstrate how to use TF library -----------------------------------------------------
; 
; Notes:
; This script will create a number of text files: testfile.txt, replace.txt and where applicable testfile_copy.txt
; Variables in this testscript:
; - TestFile (used to create a file but also as variable to test the capability of TF to process files & variables
; - F (shorthand for filename Testfile.txt)
; - F2 (shorthand for filename Testfile.txt)
; - t (a global var created by TF())
; 
; ! if used in combination with F or TestFile.txt it will overwrite the inputfile (e.g. testfile.txt)
; 
; TF_Merge, TF_Prepend, TF_Append, currently do not support variables
; and only work with FILES. 
; 
; Be sure to read the introduction first to understand the basic concepts of start/end,
; the ! prefix and difference between files & variables
; Documentation : https://ahknet.autohotkey.com/~hugov/tf-lib.htm
; AHK Forum     : http://www.autohotkey.com/forum/viewtopic.php?p=307344#307344 (Also for examples)
; 
; Most examples below create TestFile_copy.txt so you can see what it it is 
; doing if you compare it to the original TestFile.txt that is also generated
; 
; create sample file
;
TestFile=
(join`r`n
   01 AutoHotkey is a free, open-source utility for Windows.     
     02 * Automate almost anything by sending keystrokes and mouse clicks. You can write a mouse or keyboard macro by hand or use the macro recorder.   
06 * Remap keys and buttons on your keyboard, joystick, and mouse.   
 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".
   06 * Create custom data-entry forms, user interfaces, and menu bars. See GUI for details   
         05 * Create custom data-entry forms, user interfaces, and menu bars. See GUI for details
%A_Tab%
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.%A_Space%%A_Space%

08 * Run existing AutoIt v2 scripts and enhance them with new capabilities.

08 * Run existing AutoIt v2 scripts and enhance them with new capabilities.
06 * Remap keys and buttons on your keyboard, joystick, and mouse.
08 * Run existing AutoIt v2 scripts and enhance them with new capabilities.
08 * Run existing AutoIt v2 scripts and enhance them with new capabilities.
09 * Convert any script into an EXE file that can be run on computers that don't have AutoHotkey installed.
10 Getting started might be easier than you think. Check out the quick-start tutorial.
)
Replace=
(join`r`n
this is
the Text
from the replace.txt
file
)
FileDelete, %A_ScriptDir%\Replace.txt
FileDelete, %A_ScriptDir%\TestFile.txt
FileDelete, %A_ScriptDir%\TestFile_Copy.txt
FileAppend, %TestFile%,%A_ScriptDir%\TestFile.txt
FileAppend, %Replace%,%A_ScriptDir%\Replace.txt

F=TestFile.txt
F2=TestFile.txt

;To test an example remove the ; on the line

;*** Examples for TF_CountLines ---------------------------------------------------

;MsgBox % TF_CountLines(F)                   ; count lines, pass on textfile
;MsgBox % TF_CountLines("TestFile.txt")     ; count lines, pass on textfile
;MsgBox % TF_CountLines(TF(F))              ; count lines, use TF(F), this will also create global var t, so not the smartest way to do it in this particular case, see example below
;MsgBox % TF_CountLines(TF("TestFile.txt")) ; count lines, use TF(F), this will also create global var t, so not the smartest way to do it in this particular case, see example below
;MsgBox % TF_CountLines(TestFile)           ; count lines, pass on variable

;uncomment both lines
;TF(F)                                       ; will create global var t
;MsgBox % TF_CountLines(t)                  ; count lines

;uncomment both lines
;t:=TF_CountLines(TF(F))                    ; count lines, the TF() will create global var t but we assign the output of TF_Countlines to t as well
;MsgBox % t

;uncomment both lines 
;TF(F, MyTFileVar)                          ; create global var MyTFileVar
;MsgBox % TF_CountLines(MyTFileVar)        ; count lines

;*** Examples for TF_ReadLines ---------------------------------------------------

;MsgBox % TF_ReadLines(F,5)                 ; pass on file, read lines 5 to end of file
;MsgBox % TF_ReadLines(F,5)                 ; pass on file, read lines 5 to end of file

;uncomment both lines 
;t:=TF_ReadLines(TF(F),5)                 ; pass on global var t created by TF(), read lines 5 to end of file, asign result to t 
;MsgBox % t

;MsgBox % TF_ReadLines(F,1,5)              ; pass on file, read lines 1 to 5
;MsgBox % TF_ReadLines(TestFile,"1-5")     ; pass on variable, read lines 1 to 5

;uncomment both lines 
;t:=TF_ReadLines(TF(F),"1-5")             ; pass on global var t created by TF(), asign output to t, read lines 1 to 5
;MsgBox % t 

;uncomment both lines 
;MyVar:=TF_ReadLines(TF("testfile.txt","MyVar"),"1-5")             ; create global var MyVar by TF(), read lines 1 to 5
;MsgBox % MyVar

;MsgBox % TF_ReadLines(F,"2+3")             ; pass on file, start with line 2, increment 3 until end of file so 2,5,8 etc
;MsgBox % TF_ReadLines(TestFile,"2+3")      ; pass on variable, start with line 2, increment 3 until end of file so 2,5,8 etc
;MsgBox % TF_ReadLines(F,"1-3,8-10")        ; read lines 1 to 3 and 8 to 10
;MsgBox % TF_ReadLines(F,"1,2,5,8",3)       ; read lines 1 2 5 8, ignore 3. 3 is endline value which is ignored in such cases

;MsgBox % TF_ReadLines(F,TF_FindLines(F,1,0,"keys"))

;*** Examples for TF_ReplaceInLines ------------------------------------------------

;TF_ReplaceInLines("!" . F,3,5,"key","lock")           ; Pass on file, replace key with lock in lines 3 to 5
;MsgBox % TF_ReplaceInLines(TestFile,3,5,"key","lock") ; Pass on variable, replace key with lock in lines 3 to 5

;uncomment both lines 
;MyVar:=TF_ReplaceInLines(TestFile,3,5,"key","lock")   ; Pass on variable, replace key with lock in lines 3 to 5
;MsgBox % MyVar

;uncomment both lines 
;t:=TF_ReplaceInLines(TF(F),"3-5","","key","lock")     ; pass on global var t created by TF(), asign output to t, replace key with lock in lines 3 to 5
;MsgBox % t

;TF_ReplaceInLines("!" . F,"1,3,9","","key","lock")    ; pass on file, overwrite input, replace key with lock in lines 1 3 and 9 
;TF_ReplaceInLines(F,"1,3,9","","key","lock")          ; pass on file, replace key with lock in lines 1 3 and 9
;TF_ReplaceInLines(F,"1+3","","key","lock")            ; pass on file, replace key with lock in lines 1 4 7 etc until end of file
;TF_ReplaceInLines("!" . F,"1+3","","key","lock")      ; pass on file, overwrite input, replace key with lock in lines 1 4 7 etc until end of file

;uncomment both lines 
;FileRead, Replace, replace.txt                         ; Useful trick, you can use another file or multiple lines for a replacement
;TF_ReplaceInLines(F,"2","8","Remap keys",replace)      ; replace "Remap keys" with contents from replace.txt

;*** Examples for TF_Replace -----------------------------------------------------

;uncomment both lines 
;FileRead, Replace, replace.txt              ; Useful trick, you can use another file or multiple lines for a replacement
;TF_Replace(F,"Remap keys",replace)          ; replace Remap keys with contents from replace.txt

;TF_Replace(F,"key","lock")            ; pass on file, replace key with lock in file

;uncomment both lines 
;MyVar:=TF_Replace(TestFile,"key","lock")       ; pass on variable, replace key with lock in var
;MsgBox % MyVar

;uncomment both lines 
;t:=TF_Replace(TF(F),"key","lock")          ; pass on variable, replace key with lock in var
;MsgBox % t

;TF_Replace(F,"`r`n"," ")                    ; pass on file, replace new lines with space
;MsgBox % TF_Replace(TestFile,"`r`n"," ")   ; pass on variable, replace new lines with space

;*** Examples for TF_RegExReplaceInLines -----------------------------------------------------

;TF_RegExReplaceInLines(F,3,8," [a-z]{3} "," lock ")                 ; pass on file, replace any three letter word with lock in lines 3 to 8
;TF_RegExReplaceInLines(F,"1-3,8-10","","( [a-z]{3}) "," $1lock ")   ; pass on file, replace any three letter word with wordlock in lines 1 to 2 and 8 to 10
;MsgBox % TF_RegExReplaceInLines(TF(F),"1-3,8-10","","( [a-z]{3}) "," $1lock ")   ; pass on variable, replace any three letter word with wordlock in lines 1 to 2 and 8 to 10. In this case a global var t is also created so using the MsgBox here is not the best of methods, see examples above with t:=TF_... for better use

;*** Examples for TF_RegExReplace ------------------------------------------------------------

;TF_RegExReplace(F,"im)^(.*)$","[$1]")        ; pass on file, wrap all lines in []
;TF_RegExReplace("!" . F,"im)^(.*)$","[$1]")  ; pass on file, wrap all lines in [], overwrite it

;uncomment both lines 
;t:=TF_RegExReplace(TF(F),"im)^(.*)$","[$1]") ; pass on variable created by TF(), wrap all lines in []
;MsgBox % t

;TF_RegExReplace(F,"im)^(.*)\r?\n(.*)","[$1|$2]") ; pass on file, join two consective lines with seperator | and wrap lines in []

;*** Examples for TF_RemoveLines -------------------------------------------------------------

;TF_RemoveLines("!" . F,5)                  ; pass on file, remove from line 5 to end of file
;TF_RemoveLines("!" . F,5)                  ; pass on file, remove from line 5 to end of file. Note the ! to indicate TestFile.txt will be the output file

;uncomment both lines 
;t:=TF_RemoveLines(TF(F),5)                 ; pass on variable created by TF(), remove from line 5 to end of var
;MsgBox % t

;MsgBox %TF_RemoveLines(TestFile,"2+3")     ; pass on var, delete line 2, increment 3 until end of var so 2,5,8,11 etc
;TF_RemoveLines(F,"5,8")                    ; remove lines 5 and 8
;TF_RemoveLines(F,"5-8")                    ; remove lines 5 to 8
;TF_RemoveLines(F,"3-5,7-9",10)             ; remove lines 3 to 5 and 7 to 9, ignore end value 10

;TF_RemoveLines(F,-5)                       ; pass on file, remove the last 10 lines

;*** Examples for TF_RemoveBlankLines ---------------------------------------------------------

;TF_RemoveBlankLines(F)                     ; remove blanklines in entire file
;TF_RemoveBlankLines("!" . F)               ; remove blanklines in entire file
;MsgBox % TF_RemoveBlankLines(TF(F))        ; remove blanklines in entire file
;TF_RemoveBlankLines(F,7)                   ; remove blanklines starting from line 7
;TF_RemoveBlankLines(F,1,6)                 ; remove blanklines from lines 1 to 6

;*** Examples for TF_RemoveDuplicateLines -----------------------------------------------------

;TF_RemoveDuplicateLines("!" . F)            ; remove duplicate lines in file 
;MsgBox % TF_RemoveDuplicateLines(TestFile)  ; remove duplicate lines in entire var

;TF_RemoveDuplicateLines(F,"","",1,false)     ; remove only Consecutive duplicate lines
;MsgBox % TF_RemoveDuplicateLines(TestFile,"","",1,false)    ; remove only Consecutive duplicate lines

;*** Examples for TF_InsertLine ---------------------------------------------------------------

;TF_InsertLine("!" . F,5,5,"---")           ; insert --- in line 5
;MsgBox % TF_InsertLine(TestFile,5,5,"---")           ; insert --- in line 5

;uncomment both lines 
;FileRead, Replace, replace.txt
;TF_InsertLine(F,5,5,replace)   ; useful trick, you can insert replace.txt in line 5 or the contents of another var
;MsgBox % TF_InsertLine(TF(F),5,5,replace)   ; useful trick, you can insert replace.txt in line 5 or the contents of another var

;TF_InsertLine(F,"2,4,9",5,"---")     ; insert --- in lines 2 4 and 9. 5 is endline will be ignored
;TF_InsertLine(F,"1+3","","---")      ; insert --- in lines 1 4 7 etc until end of file
;TF_InsertLine(F,4,8,"---")           ; insert --- between all lines 4 to 8

;*** Examples for TF_ReplaceLine ---------------------------------------------------------------

;TF_ReplaceLine(F,3,3,"---")          ; replace line 3 with ---
;TF_ReplaceLine(F,"1,5,9","","---")   ; replace lines 1 5 and 9 with ---
;TF_ReplaceLine(F,"1+3","","---")     ; replace lines 1 4 7 etc with --- until end of file
;TF_ReplaceLine(F,"1+3",8,"---")      ; replace lines 1 4 7. 8 is end line so no more lines are processed after that

;*** Examples for TF_InsertPrefix ---------------------------------------------------------------

;TF_InsertPrefix(F,"","", "Hello ")        ; Prefix Hello in all lines
;MsgBox % TF_InsertPrefix(TestFile,"","", "Hello ")        ; Prefix Hello in all lines

;TF_InsertPrefix(F,5,8, "Hello ")          ; Prefix Hello in lines 5 to 8
;TF_InsertPrefix(F,5,"", "Hello ")         ; Prefix Hello starting from line 5 to end of file
;TF_InsertPrefix(F,"5,8","", "Hello ")     ; Prefix Hello in line 5 and 8
;TF_InsertPrefix(F,"2-4,6-9","", "Hello ") ; Prefix Hello in lines 2 to 4 and 6 to 9

;*** Examples for TF_InsertSuffix ---------------------------------------------------------------

;TF_InsertSuffix(F,"2-4,6-9","", " Hello")  ; Suffix Hello in lines 2 to 4 and 6 to 9
;MsgBox % TF_InsertSuffix(TF(F),"2-4,6-9","", " Hello")  ; Suffix Hello in lines 2 to 4 and 6 to 9
;TF_InsertSuffix(F,"2+5","", " Hello")      ; Suffix Hello in lines 2 7 etc

;*** Examples for TF_TrimLeft -------------------------------------------------------------------

; A Tab is one character so lines with tabs might have unexpected results
;TF_TrimLeft(F,"","",25)                    ; Trim Left 25 Characters all lines
;MsgBox % TF_TrimLeft(TF(F),"","",25)                    ; Trim Left 25 Characters all lines
;TF_TrimLeft(F,4,7,3)                       ; Trim Left 3 Characters in lines 4 to 7
;TF_TrimLeft(F,"4,6,8","",5)                ; Trim Left 5 Characters in lines 4 6 and 8

;*** Examples for TF_TrimRight ------------------------------------------------------------------

; A Tab is one character so lines with tabs might have unexpected results
;TF_TrimRight(F,"4,6,8","",45)              ; Trim Right 45 Characters in lines 4 6 and 8
;MsgBox % TF_TrimRight(TestFile,"4,6,8","",45)              ; Trim Right 45 Characters in lines 4 6 and 8

;*** Examples for TF_ConCat ---------------------------------------------------------------------

;TF_ConCat(FirstTextFile, SecondTextFile, OutputFile, Blanks = 0, FirstPadMargin = 0, SecondPadMargin = 0)

;FirstTextFile and SecondTextFile can be both a file, a var, or one a file and the other a var
;TF_ConCat(F,F2,"cat2.txt",10, 5, 8) 
;MsgBox % TF_ConCat(F,F2,"",10, 5, 8)  ; special case: if you want to return a variable, keep outputfile empty
;MsgBox % TF_ConCat("TestFile.txt",TF(F),"",10, 5, 8) ; special case: if you want to return a variable, keep outputfile empty

;*** Examples for TF_LineNumber ----------------------------------------------------------------

;TF_LineNumber(F)               ; Add linenumbers, no padding / leading
;TF_LineNumber(F,1)             ; Add linenumbers, padd with zeros: 01 02 v 1 2
;TF_LineNumber(F,1,15)          ; Add linenumbers, padd with zeros, restart after 15
;MsgBox % TF_LineNumber(TF(F),1,15)          ; Add linenumbers, padd with zeros, restart after 15
;TF_LineNumber(F,0,15)          ; Add linenumbers, no padding, restart after 15 (linenumbers are aligned left)
;TF_LineNumber(F,1,15,A_Space)  ; Add linenumbers, padding with spaces, linenumbers are aligned right

;*** Examples for TF_ColGet ---------------------------------------------------------------------

;Note: A TAB character is 1 character so for files with TABS the column might not be where you expect it to be
;TF_ColGet(F,"","",2,13)    ; get columns 2 to 13 from all lines, so it removes all other columns from the file or variabla
;MsgBox % TF_ColGet(TF(F),"","",2,13)    ; get columns 2 to 13 from all lines
;MsgBox % TF_ColGet(TestFile,"2+2","",2,13) ; get columns 2 to 13 from lines 2 4 6 8 etc

;*** Examples for TF_ColPut ---------------------------------------------------------------------

;Note: A TAB character is 1 character so for files with TABS the column might not be where you expect it to be

;TF_ColPut(F,"","",5, "|", 0)    ; insert | in column 5 of all lines including empty lines, so that means | will be at column 1
;MsgBox % TF_ColPut(TF(F),"","",5, "|", 1)    ; insert | in column 5 of all lines skipping empty lines, so that means | will NOT be at column 1 or at any position in the empty line
;MsgBox % TF_ColPut(TestFile,"2+2","",5, "|", 1) ; insert | in column 5 of lines 2 4 6 8 etc skipping empty lines

;*** Examples for TF_ColCut ---------------------------------------------------------------------

;Note: A TAB character is 1 character so for files with TABS the column might not be where you expect it to be
;TF_ColCut(F, "2+2", "", 4, 38)  ; remove columns 4 to 38 in lines 2 4 6 8 etc
;MsgBox % TF_ColCut(TF(F), 4, 10, 10, 25)  ; remove columns 10 to 25 in lines 4 to 10 

;*** Examples for TF_Tail ---------------------------------------------------------------------
; TF_Tail(Text, Lines = 1, RemoveTrailing = 0, ReturnEmpty = 1)

;MsgBox % TF_Tail(F, 3) ; get the last three lines
;MsgBox % TF_Tail(F, -2) ; get second to last line, negative values only return one line
;MsgBox % TF_Tail(F, 5, 0, 0) ; return the last five lines, with trailing new line and excluding empty lines


;*** Examples for TF_AlignLeft ----------------------------------------------------------------

;TF_AlignLeft(F,"","",90, 1)    ; AlignLeft all lines, keep trailing white space
;MsgBox % TF_AlignLeft(TF(F),"","",90, 1)    ; AlignLeft all lines, keep trailing white space
;MsgBox % TF_AlignLeft(TestFile,"","",90, 0)    ; AlignLeft all lines, remove trailing white space, note the effect on empty lines
;TF_AlignLeft(F, "4", "", 90)   ; AlignLeft, starting from line 4
;TF_AlignLeft(F, "3,5", "", 90) ; AlignLeft, lines 3 and 5

;*** Examples for TF_AlignCenter ----------------------------------------------------------------
   
;TF_AlignCenter(F,"","",150, 1) ; AlignCenter, all lines skip emtpy lines, do not fill with spaces
;MsgBox % TF_AlignCenter(TF(F),5,"", 150)    ; AlignCenter, starting from line 5
;TF_AlignCenter(F,5,8,150)      ; AlignCenter, lines 5 to 8
;TF_AlignCenter(F,"1+2",9,150)  ; AlignCenter 1 3 5 7 and 9, stop at 9

;*** Examples for TF_AlignRight ----------------------------------------------------------------
   
;TF_AlignRight(F,"","", 190, 0) ; AlignRight, all lines, do not skip emtpy lines fill them with spaces
;MsgBox % TF_AlignRight(TestFile,4,8,150)       ; AlignRight, lines 4 to 8
;TF_AlignRight(F,"4,6","",150)  ; AlignRight, lines 4 and 6
;TF_AlignRight(F,"6-9","",150)  ; AlignRight, lines 6 to 9

;*** Examples for TF_WhiteSpace ----------------------------------------------------------------

;TF_WhiteSpace(F)               ; remove leading and trailing whitespace
;MsgBox % TF_WhiteSpace(TestFile)               ; remove leading and trailing whitespace
;TF_WhiteSpace(F, 0, 1)         ; keep leading and remove trailing whitespace
;MsgBox % TF_WhiteSpace(TF(F), 1, 1, "5-10") ; remove leading and trailing whitespace in lines 5 to 10

;*** Examples for TF_ReverseLines ----------------------------------------------------------------


;uncomment both lines to illustrate the effect more clearly
;TF_LineNumber("!" . F, 1)     ; Add linenumbers, padd with zeros: 01 02 v 1 2
;TF_ReverseLines(F,2,9) ; Now reverse lines so we can see it

;TF_ReverseLines("!" . F,2,9)

;*** Examples for TF_Find ----------------------------------------------------------------
;TF_Find(Text, StartLine = 1, EndLine = 0, SearchText = "", ReturnFirst = 1, ReturnText = 0)

;MsgBox % TF_Find(F,"","", "button")           ; Find first line with the word button. You can store this in a var for further processing (startline or endline value)
;MsgBox % TF_Find(TF(F),"","", "button")       ; Find first line with the word button. You can store this in a var for further processing (startline or endline value)
;MsgBox % TF_Find(TF(F),"","", "button", 0)    ; Find all lines with the word button, return line numbers
;MsgBox % TF_Find(TF(F),"","", "button", 0, 1) ; Find all lines with the word button, return text
;MsgBox % TF_Find(TF(F),"","", "button", 0, 2) ; Find all lines with the word button, return lines & text


;*** Examples for TF_Wrap ----------------------------------------------------------------

;TF_Wrap(F,60)            ; wrap at col 60
;TF_Wrap(F,30, 0, 10, 15) ; wrap at col 30, do not break words, process lines 10 to 15
;MsgBox % TF_Wrap(TestFile,30, 0, 10, 15) ; wrap at col 30, do not break words, process lines 10 to 15

;*** Examples for TF_Substract ----------------------------------------------------------------

;uncomment these lines for TF_Substract example
;FileDelete, File1.txt
;FileDelete, File2.txt
;FileAppend, 1`n2`n5`n9`n, file1.txt
;FileAppend, 1`n21`n3`n4`n5`n6`n7`n8`n9, file2.txt
;TF_Substract("File1.txt", "File2.txt")     ; remove 1 2 5 9 from file2, note 21 is NOT removed
;TF_Substract("File1.txt", "!File2.txt", 1)  ; remove 1 2 5 9 from file2, note 21 is removed because 1 and 2 are in file1 due to partial match

;t:=TF_Substract("File1.txt", TF("File2.txt"))  ; remove 1 2 5 9 from file2, note 21 is NOT. If file2 is variable then return output to var as well, note you can use TF() here to pass on a file as a variable
;MsgBox % t

;*** Examples for TF_MakeFile ----------------------------------------------------------------

;No need to think about how to return a var not just create file
;TF_MakeFile("empty.txt", 40, 80) ; create empty text, 40 lines, 80 columns filled with spaces
;MsgBox % TF_MakeFile("", 40, 80, "x") ; create empty text variable, 40 lines, 80 columns filled with spaces. To return as variable, keep Text empty

;*** Examples for TF_RangeReplace ----------------------------------------------------------------

;Uncomment the line below for the TF_RangeReplace examples as well
;Range=[insert this`ntext for the`nrange replace Text`ntest function]
;TF_RangeReplace(F, "Create hotkeys for keyboard", "into an EXE file", Range) ; replace from start to end
;If you want to delete a "range" simply leave Replace empty

;MsgBox % TF_RangeReplace(TF(F), "Create ", " keyboard", Range, False, 1, 1) ; replace from start to end, keep start & end IN output file
;TF_RangeReplace(F, "Create", "keyboard", Range, False, 1, 1) ; replace from start to end, keep start & end IN output file
;TF_RangeReplace(F, "Create hotkeys for keyboard", "into an EXE file", Range , False, 1) ; keep start text
;TF_RangeReplace(F, "Create hotkeys for keyboard", "easier than you think", Range , False, 0, 1) ; keep end text
;TF_RangeReplace(F, "", "into an EXE file", Range) ; replace from beginning of file
;TF_RangeReplace(F, "", "", Range) ; basically same as replacing the file with a new one
;TF_RangeReplace(F, "For example", "", Range)

;*** Examples for TF_Sort ----------------------------------------------------------------

;TF_Sort(F,"",5,10)             ; sort text file lines 5 to 10
;TF_Sort("!" . F)             ; sort text file

;t:=TF_Sort(TF(F))
;MsgBox % "sorted:" t

;TF_Sort(F, "", 4, 8)   ; sort lines 4 to 8 of text file keep rest as it is
;MsgBox % TF_Sort(TestFile, "P16")       ; sort on 16th column, see AHK Sort options 

;*** Examples for Tab2Spaces, Spaces2Tabs

;TF_Spaces2Tab(F,1) ; convert all spaces to tabs
;MsgBox % TF_Spaces2Tab(TF(F),1) ; convert all spaces to tabs
;TF_Tab2Spaces(F)
;TF_Tab2Spaces("!" . F)

;*** Examples for TF_Save ----------------------------------------------------------------

;TF_Save(TF_LineNumber(TestFile), "MyTest.txt") ; add linenumbers to variable and save as MyTest.txt
;TF_Save(TF_LineNumber(TF(F)), "MyTest.txt") ; read file into variable t, add linenumber and save as MyTest.txt. Note it creates global var t with the contents of the original file, it is a bit of a silly example 

;*** Examples for TF_Merge/Append/Prepend-------------------------------------------------
;functions below do NOT support variables

;uncomment both lines
;FileList .= F "`r`n" F2 ; create filelist, see also DOC
;TF_Merge(FileList) ; merge files in FileList, output file merged.txt, create merged_copy.txt if it exists
;TF_Merge(FileList, "`n", "!merged2.txt") ; merge files in FileList, output file merged2.txt overwrite if it exists

;TF_Append(F,F2)  ; Appends File1 to File2
;TF_Prepend(F,F2) ; Prepends File1 to File2

;*** Examples for TF_SplitFileByLines ----------------------------------------------------------------
;TF_SplitFileByLines(TextFile, SplitAt, Prefix = "file", Extension = "txt", InFile = 1)

;TF_SplitFileByLines(F, 2, "part", "zec", 1)            ; split source file every 2 lines, include 2nd line INFILE

;Uncomment both lines
;TF_LineNumber("!" . F)
;TF_SplitFileByLines(F, "3,8,16", "part", "zec", 1)    ; split source at lines 3, 8 and 16 until the end of file

;Uncomment both lines
;TF_LineNumber("!" . F)
;TF_SplitFileByLines(F, "3-5", "part", "zec", 1)    ; split source every lines 3 and 5 lines, alternate until the end

;uncomment both lines to illlustrate use of variables and returned arrays
;TF_SplitFileByText(TestFile, "keyboard", "part", "zec", 1)
;MsgBox % "Array size: " . Part0 . "`n1st array element: "  Part1

;*** Examples for TF_SplitFileByText ----------------------------------------------------------------
;TF_SplitFileByText(F, "button", "part", "zec", 1)      ; split source file on every line with the word button, include that line INFILE
;TF_SplitFileByText(F, " [a-z]{11} ", "part", "zec", 1) ; split source file on every line with a word of 11 characters, include that line INFILE



shajul
  • Members
  • 571 posts
  • Last active: Aug 01 2015 03:45 PM
  • Joined: 15 Sep 2006
Very cool..

Also the documentation page is excellent..

Thanks
If i've seen further it is by standing on the shoulders of giants

my site | ~shajul | WYSIWYG BBCode Editor

ribbet.1
  • Members
  • 198 posts
  • Last active: Feb 07 2012 01:21 AM
  • Joined: 20 Feb 2007
I'm not doing anything but reading the post right now, but it looks VERY interesting. I want to take a closer look at it this evening, but I just wanted to say Cheers!

ruespe
  • Members
  • 567 posts
  • Last active: Dec 01 2014 07:59 PM
  • Joined: 17 Jun 2008
Yes, it really looks very useful. Thanks for sharing.
Rog

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
Update v2.2 Added 10 July 2009:

- TF_Substract
- TF_WhiteSpace
- TF_Wrap

MarcInSpace
  • Members
  • 33 posts
  • Last active: Sep 04 2013 06:17 AM
  • Joined: 23 Feb 2009
Thanks a lot for sharing :D There are many functions I have wished for but somehow hadn't found the time or way (to do) for, yet!

Greetings, Marc

SouzaRM
  • Members
  • 12 posts
  • Last active: Jul 26 2018 01:32 PM
  • Joined: 26 Nov 2008
Fantastic!
Congratulations for the project.
Fight, Fight and Fight

Murp-e
  • Members
  • 531 posts
  • Last active: Sep 27 2011 11:44 AM
  • Joined: 12 Jan 2007
HugoV: You've done some great work and this is very useful. Once I started to use your library I started trashing my own -inferior- code and using your functions instead. One of my scripts uses a CSV file to keep track of some values. My script reads the last line of the CSV file to get the last recorded values. I thought I could use TXT_RemoveBlankLines to make sure the last line isn't blank but instead of removing blank lines in the original file it removed blank lines and copied the file. It seems that if the TextFile is OW (Open for Write?) the function cannot modify the existing file so it creates a copy. I think most people would expect the function to simply return an error code as opposed to creating a new copy of the file. When testing this, I was simply deleting the copy because I couldn't understand where it was coming from and the remaining file was unchanged so I thought the function didn't work properly.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Ty, very useful and nicely documented.
Posted Image

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

Ty, very useful and nicely documented.

OMG praise from one of the AHK Kings of Cool. Thanks!

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007

HugoV: You've done some great work and this is very useful.

Thanks, glad you like it.

My script reads the last line of the CSV file to get the last recorded values. I thought I could use TXT_RemoveBlankLines to make sure the last line isn't blank but instead of removing blank lines in the original file it removed blank lines and copied the file. It seems that if the TextFile is OW (Open for Write?) the function cannot modify the existing file so it creates a copy. I think most people would expect the function to simply return an error code as opposed to creating a new copy of the file. When testing this, I was simply deleting the copy because I couldn't understand where it was coming from and the remaining file was unchanged so I thought the function didn't work properly.


OW is OverWrite I guess (Heresy came up with that bit)

Basically if you use:

"file.csv" it will create "file_copy.csv" which shouldn't have
any blank lines in it. If you use "!file.csv" it will OverWrite
your file.csv, again without any blank lines in it. Could you
PM me an example file if you keep having problems.

Murp-e
  • Members
  • 531 posts
  • Last active: Sep 27 2011 11:44 AM
  • Joined: 12 Jan 2007

HugoV: You've done some great work and this is very useful. Once I started to use your library I started trashing my own -inferior- code and using your functions instead. One of my scripts uses a CSV file to keep track of some values. My script reads the last line of the CSV file to get the last recorded values. I thought I could use TXT_RemoveBlankLines to make sure the last line isn't blank but instead of removing blank lines in the original file it removed blank lines and copied the file. It seems that if the TextFile is OW (Open for Write?) the function cannot modify the existing file so it creates a copy. I think most people would expect the function to simply return an error code as opposed to creating a new copy of the file. When testing this, I was simply deleting the copy because I couldn't understand where it was coming from and the remaining file was unchanged so I thought the function didn't work properly.

Please, allow me... ...RTFM:

TextFile: The Filename including path to read from and save to. If TextFile starts with ! (eg: "!c:\sample.txt") will overwrite the text file else will save to copy of the text file (eg:Filename_copy.txt)

For what its worth, it stills feels unconventional. Also, having the file path stored in a variable requires a concatenation of the exclamation point and the path like so:
TXT_RemoveBlankLines("!" . MeterLogPath)
Wouldn't it be more conventional to have a second optional argument which defaults to Overwrite=1? Please enlighten me wise ones.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
OK, I could improve the documentation on that :wink:

Personally I like the idea of using the ! method which was introduced by Heresy. I even contemplated of introducing a second one :-)

I process many files in loops and concatenation is easy to do
otherwise I would have to pass on overwrite yes/no parameter
all the time and I already have enough parameters for each
function I guess.

One idea I have for version 3 or 4 is to introduce some global
settings via an INI function so you can set preferences for
backups, overwrite by default etc.

SoLong&Thx4AllTheFish
  • Members
  • 4999 posts
  • Last active:
  • Joined: 27 May 2007
v2.3 Added 28 July 2009:

- TF_Tab2Spaces *
- TF_Spaces2Tab *
- TF_RangeReplace (similar to "BK Replace EM" Range Replacement (highly recommend piece of software by the way, see link above). Basically an easier shortcut for a RegExp, slightly experimental)
- TF_MakeFile (Create file of X lines and Y columns, fill with space or other character(s))

* Note: yes they are a bit silly, but I find them to be useful
as they serve as a shorthand for TF_ReplaceInLines. They are
by no means "intelligent". Thanks to infogulch for the idea:
<!-- m -->http://www.autohotke... ... 570#204570<!-- m -->

Also thanks to Murp|e for pointing out some errors in the documentation
See links in first post (you may need to refresh the page to see the 2.3)

Edit: forgot to add that suggestions and test reports are always welcome