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

Post your working scripts, libraries and tools
malcev
Posts: 67
Joined: 12 Aug 2014, 12:37

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

09 Apr 2017, 10:44

Hi.
Found some bugs.
1) If I have this string in ClipBoard then I will have an error:

2) If I have any filename (not file content) in Clipboard - I also will get error:

3) If I have filename with first letter "!" then I will have error.
For example I am waiting that script creates copy of !TestFile.txt but it modifies it.

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

ahk7-nli

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

10 Apr 2017, 05:14

Sorry, but these examples aren't bugs and work as they should.

Example 1:

Code: [Select all] [Download] GeSHi © Codebox Plus

Clipboard=12345
Clipboard:=TF_TrimLeft(Clipboard,1,1,3) ; means trim the first three chars, so 123
MsgBox % Clipboard ; shows 4-5 so it works


Example 2 - it will create a FILE not alter the clipboard, look for c:\1_copy.txt:

Code: [Select all] [Download] GeSHi © Codebox Plus

Clipboard=c:\1.txt
Clipboard:=TF_TrimLeft(Clipboard,1,1,3) ; you are passing on a file: TF_TrimLeft("c:\1.txt" ...
MsgBox % Clipboard ; won't show anything as result is NOT put back on the clipboard

Note: writing to c:\ is a bad idea, the OS usually won't allow it so it makes it look like it hasn't worked, use a normal regular folder where you permissions to read and write files.

Example 3 - you have a syntax error, filename should be quoted in "" the ! should also be within the ""

Code: [Select all] [Download] GeSHi © Codebox Plus

TF_TrimLeft("!TestFile.txt",1,1,3)


If you pass on a (reference) to a file the result will ALWAYS be a file, only if you pass on an actual variable the result will be returned by the TF functions. Otherwise it may (or may not) return an ErrorLevel indicating if it was possible to create a backup file. See Note 2 "2. Backup files: If a subdirectory "backup" is present in the directory of TextFile a backup is made before overwriting the original file (both for file.txt and file_copy.txt) with the BAK extension"

See also https://github.com/hi5/TF/issues/4
malcev
Posts: 67
Joined: 12 Aug 2014, 12:37

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

10 Apr 2017, 07:07

1) Your example works:

Code: [Select all] [Download] GeSHi © Codebox Plus

Clipboard=12345
Clipboard:=TF_TrimLeft(Clipboard,1,1,3) ; means trim the first three chars, so 123
MsgBox % Clipboard ; shows 4-5 so it works

But my does not work:

Code: [Select all] [Download] GeSHi © Codebox Plus

Clipboard=00000
Clipboard:=TF_TrimLeft(Clipboard,1,1,3)
MsgBox % Clipboard ; shows error

You can try it by yourself.

2) If I have variable that consists of filename for example:
var := "c:\1.txt"
And I want to trim some symbols with your function.
I am waiting that when I do this

Code: [Select all] [Download] GeSHi © Codebox Plus

var := "c:\1.txt"
var:=TF_TrimLeft(var,1,1,3)

tnen I will get var = 1.txt
The same as it can be done with substr():

Code: [Select all] [Download] GeSHi © Codebox Plus

var := "c:\1.txt"
MsgBox % SubStr(var, 4) ; Returns "1.txt"

How does your function understand what I want pass to it - reference to a file or simply variable?

3) Sorry I forgot to put quotes in function.
I mean this.
This code works as expected.
It creates 2 files: TestFile.txt and trimmed TestFile_copy.txt

Code: [Select all] [Download] GeSHi © Codebox Plus

TestFile= 12345; create variable
FileDelete, TestFile.txt
FileAppend, %TestFile%, TestFile.txt ; create file
TF_TrimLeft("TestFile.txt",1,1,3)

But this one does not work as expected:

Code: [Select all] [Download] GeSHi © Codebox Plus

TestFile= 12345; create variable
FileDelete, !TestFile.txt
FileAppend, %TestFile%, !TestFile.txt ; create file
TF_TrimLeft("!TestFile.txt",1,1,3)

It creates 1 file: !TestFile.txt
So If I have file "!example.txt" then I can not edit this file with your library because your library does not support filenames that starts with "!" ?
Guest

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

10 Apr 2017, 07:26

In your Example 1 it shows 00 in my MsgBox, I just used 12345 so you could see what chars are trimmed.

malcev wrote:2) If I have variable that consists of filename for example:
var := "c:\1.txt"
And I want to trim some symbols with your function.
I am waiting that when I do this

Code: [Select all] [Download] GeSHi © Codebox Plus

var := "c:\1.txt"
var:=TF_TrimLeft(var,1,1,3)

tnen I will get var = 1.txt
How does your function understand what I want pass to it - file path or simply variable?
In this particular case you should either use normal AutoHotkey function SubStr() - https://autohotkey.com/docs/commands/SubStr.htm - OR the SplitPath - https://www.autohotkey.com/docs/commands/SplitPath.htm - command.
TF tries to make an educated guess to see if something is a file or a variable, if the variable is a file path it will try to process a file. If you use var:="c:\1.txt`nc:\2.txt" for example it sees there is a newline character which means what you pass on to TF can't be a file and the result will be
1.txt
c:\2.txt

That is how it is intended to be used. If you really must or want to use TF_TrimLeft in such a case you can just make sure you pass on a non-existing file path by prepending a space (or other character) to the variable so it no longer looks like a file path, like so:

Code: [Select all] [Download] GeSHi © Codebox Plus

var:="c:\1.txt"
Clipboard:=TF_TrimLeft(" " var,1,1,4) ; prepend a space and take that into account by increasing 3 to 4 to the additional space is also trimmed
MsgBox % Clipboard ; will now show 1.txt

malcev wrote:3) If I have file "d:\!example.txt" then I can not edit this file with your library because your library does not support filenames that starts with "!" ?
Almost correct ;) It should be "!d:\example.txt". You have to start with the exclamation mark.
malcev
Posts: 67
Joined: 12 Aug 2014, 12:37

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

10 Apr 2017, 07:48

Now I understand about second and third example.
But with first one I get error:
"Read Error:`npossible reason: perhaps you used ! vs ""!""

Code: [Select all] [Download] GeSHi © Codebox Plus

Clipboard=00000
Clipboard:=TF_TrimLeft(Clipboard,1,1,3)
MsgBox % Clipboard ; shows error

May be this is because I use 3.6 version?
And function TF_GetData thinks that my variable is 0?

Code: [Select all] [Download] GeSHi © Codebox Plus

TF_GetData(byref OW, byref Text, byref FileName) 
{
If (text = 0) ; v3.6
{
MsgBox, 48, TF Lib Error, % "Read Error:`npossible reason: perhaps you used ! vs ""!"" ?"
ExitApp
}
Guest

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

10 Apr 2017, 10:05

If var=00000 is a real case scenario (but why would it?) I would still recommend SubStr(). It will be faster and shorter SubStr("00000",4).
A file with the contents "00000" works

Code: [Select all] [Download] GeSHi © Codebox Plus

FileDelete, 0.txt
FileAppend, 00000, 0.txt
TF_TrimLeft("0.txt",1,1,3) ; creates 0_copy.txt and works


If you really must use TF for something like this you can "fix" it by changing If (text = 0) to If (text = 0 "") (adding "" after the zero makes it "think" it is text not a number and therefore the comparison will work better.
If you would pass on var=0 it would still show the MsgBox but I doubt very much that would be a real case scenario :roll:
malcev
Posts: 67
Joined: 12 Aug 2014, 12:37

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

10 Apr 2017, 12:50

I see. Thank You for your help!
ahk7
Posts: 105
Joined: 06 Nov 2013, 16:35

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

16 Apr 2017, 03:58

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
User avatar
Tomer
Posts: 239
Joined: 21 Aug 2016, 05:11

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

11 May 2017, 06:24

Thanks, very useful!
ahk7
Posts: 105
Joined: 06 Nov 2013, 16:35

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

15 May 2017, 16:33

[via PM] wrote:can I ask if is it possible to remove all spaces in txt file with tf.ahk ? couldn't find it on doc.

Just like StringReplace

Code: [Select all] [Download] GeSHi © Codebox Plus

TF_Replace("File.txt"," ")
if you want to remove tabs you can pass on a tab char

Code: [Select all] [Download] GeSHi © Codebox Plus

TF_Replace("File.txt","	")

or use A_Space or A_Tab

Code: [Select all] [Download] GeSHi © Codebox Plus

TF_Replace("File.txt",A_Space)

If you want to remove multiple chars in one go TF_RegExReplace() would be the one to use to do it all in one line vs repeating it for each character using TF_Replace(). If you need to work on specific lines use the TF_ReplaceInlines() and TF_RegExReplaceInLines() versions.

TF_Replace uses StringReplace, TF_RegExReplace uses RegExReplace so look for the AHK documentation for more general info on that.

If you want to remove white space at the start and/or end of lines there is TF_WhiteSpace()
User avatar
Tomer
Posts: 239
Joined: 21 Aug 2016, 05:11

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

04 Feb 2018, 13:35

hi
im trying to replace lines in all txt files stored in c:\,
what im doing wrong ? (it seems like tf.ahk not works with variables :cry: )

Code: [Select all] [Download] GeSHi © Codebox Plus

#Include tf.ahk

Loop, Files, c:\*.txt
{

TF_Replace("!%A_LoopFileFullPath%","word","new_word")
}


thanks in advance
Guest

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

04 Feb 2018, 14:52

Don't use %% and "" around variable names when passing on (built-in) variables to functions, so in the example above it should be

Code: [Select all] [Download] GeSHi © Codebox Plus

TF_Replace("!" A_LoopFileFullPath,"word","new_word")
With variables for find/replace:

Code: [Select all] [Download] GeSHi © Codebox Plus

find:="find word"
replace:="replacement word"
TF_Replace("!" A_LoopFileFullPath,find,replace)
burque505
Posts: 385
Joined: 22 Jan 2017, 19:37

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

04 Feb 2018, 15:39

Hi @Tomer, try this:

Code: [Select all] [Download] GeSHi © Codebox Plus

#Include tf.ahk

Loop, Files, %A_ScriptDir%\*.txt
{
TF_Replace("!"A_LoopFileFullPath,"word","new_word")
}


If you have a file "foo.txt" in the directory, with the word "word" in it, it'll be replaced with "new_word".
If you want to keep a copy, do this:

Code: [Select all] [Download] GeSHi © Codebox Plus

#Include tf.ahk

Loop, Files, %A_ScriptDir%\*.txt
{
TF_Replace(A_LoopFileFullPath,"word","new_word")
}



Just tried it, works here with Win7 64-bit AHK_H v1.1.27.06
regards,
burque505
User avatar
Tomer
Posts: 239
Joined: 21 Aug 2016, 05:11

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

04 Feb 2018, 16:31

Thanks @burque505 @Guest
User avatar
Tomer
Posts: 239
Joined: 21 Aug 2016, 05:11

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

05 Feb 2018, 11:09

another question please:

i have a txt file with 3 lines:
    unlock
    unlock2
    unlock3

i want to change only the specific word "unlock" to "lock",
but it changes all the text to:

    lock
    lock2
    lock3

any way to refer only to specific word from the file ?
(ps. i dont know what is the line number of the specific word i want to change since its randomly)

Code: [Select all] [Download] GeSHi © Codebox Plus

TF_Replace("!list.txt","unlock","lock")
Guest

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

05 Feb 2018, 11:22

You could try with "unlock`n","lock`n" but that depends on the file, if there is a space or other char after the K it won't work

So better would be to use Regular Expression, this should do it:

TF_RegExReplace("!list.txt","im)\bunlock\b","lock")

For the meaning of i m and \b see:
https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
User avatar
Tomer
Posts: 239
Joined: 21 Aug 2016, 05:11

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

06 Feb 2018, 00:10

Thanks Guest.
did you test it ? because none of the above samples working :(
Guest

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

06 Feb 2018, 03:02

Of course it works, it must mean something is wrong with the source file at your end. What that is only you can determine.
The ...k`n may have to be ...k`r`n - it depends on the file (dos, unix, mac). The RegEx version just works out of the box.
If it doesn't you have to do some testing to find the problem.
Guest

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

06 Feb 2018, 04:00

Also, never operate on files on "C:\" the root folder is often protected from writing to it by the OS and/or Antivirus/security software.
Make sure you have the file(s) in a folder where you have the proper rights to read/write files. Never in the root "C:\"
User avatar
Tomer
Posts: 239
Joined: 21 Aug 2016, 05:11

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

07 Feb 2018, 03:50

ok this working good
TF_RegExReplace("!list.txt","im)\bunlock\b","lock")

thank you very much @Guest

Edit:
now I cant combine it into a loop :(

Code: [Select all] [Download] GeSHi © Codebox Plus

loop, read, D:\test\list2.txt
Loop, Files, d:\*.txt
{
TF_RegExReplace("!"A_LoopFileFullPath,"im)\bA_LoopReadLine\b","lock")
}


i failed to separate the variable A_LoopReadLine from the quotation marks ("")
Last edited by Tomer on 07 Feb 2018, 05:22, edited 2 times in total.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 13 guests