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
rootmos
  • Members
  • 13 posts
  • Last active: Mar 11 2016 09:37 AM
  • Joined: 03 Jul 2013

It seems

 

so try with RemoveComments:=TF_Find("!Comments.txt",1,0,"^\s*;",0,0) ; make sure ; is at start of a line

 

 

Tested the code, here's the results:

=== WORKING ===
;This comment will be removed.
; This too.


=== NOT WORKING ===
Msgbox Hello World ; But this will NOT be removed.
Msgbox Hello World ;NOR this one.
Gui, Add, Button, x352 y50 w30 h20, Button ; I want to remove only the comment, not the whole line.

Since the Startline vary from script to script, im not sure how to determine where TF_Find should start looking if i want to remove those examples above. 



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
OK I see you want to remove all comments, that is a bit more complicated - people have developed dedicated scripts which you could try http://ahkscript.org...c.php?f=6&t=689 otherwise you'll have to find a (complicated) regular expression and use RegExReplace (TF also has two of those functions TF_RegExReplace and TF_RegExReplaceInLines) The only foolproof way I can think of would be to compile a script and then decompile it again as that would remove all comments anywhere in the script. As you can both compile and decompile via the commandline that might be another option - see the two decompilers here http://ahkscript.org...=FAQ#Protection

rootmos
  • Members
  • 13 posts
  • Last active: Mar 11 2016 09:37 AM
  • Joined: 03 Jul 2013

Alright, I understand. I will look into the compile/decompile idea you had. Thanks for the fast replys!



Sprinkle Master
  • Members
  • 24 posts
  • Last active: Mar 06 2016 02:32 AM
  • Joined: 08 Mar 2014

I have been working a lot with TF_RemoveBlankLines, TF_RemoveLines and TF_CountLines.  While the count seams to recognize the current working directory, the remove functions don't.

 

Here are a few details of my application:

Script location:  %A_ProgramFiles%\program\script.exe

File location:  %A_AppData%\program\file.txt

SetWorkingDir, %A_AppData%\program

script has #include TF.ahk built in (v3.4)

 

When script.exe uses a TF function relying on the working directory, I get mixed results.  The CountLines seams to work fine while RemoveBlankLines and RemoveLines seam to fail.

 

If I force a hard path to the files I have no problem getting the functions to work but that isn't feasible when creating a program for distribution.  I need to rely on system variables to locate properly stored files.

 

If the Script, files and working directory are all in the same folder, I have no problem accessing the files without a hard path.  This is not feasible because the script is a program and belongs in ProgramFiles.  Since ProgramFiles is a restricted folder, a text file can't be edited in that directory unless the user is logged in as an admin.

 

I tried to do a work-around by using the apropriate system variable as the path for the file name within the TF function call.  This didn't work either because TF functions can't contain variables as a part of a file name or because I have been unable to work out the syntax on it.  Either way, the TF functions should be using the WorkingDir anyway.

 

I tried going into the TF code to determine the problem but I am still learning and it was a bit over my head for now.

 

Any help for this bug or a work-around would be greatly appreciated.

 

Thanks.



faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
Perhaps it depends on how you call the functions, simply pass on the built-in variables(s) in the function call?
TF_function( A_AppData "\program\file.txt", ....)
TF_function("!" A_AppData "\program\file.txt", ....)


Sprinkle Master
  • Members
  • 24 posts
  • Last active: Mar 06 2016 02:32 AM
  • Joined: 08 Mar 2014

@faqbot

 

Thanks for the suggestion, I will give that a try.  After studying your syntax I can see where I may have gone wrong with my attempted work-around.  However, it still doesn't explain why TF isn't changing the file with just the file name since file.txt is in the working directory.



Sprinkle Master
  • Members
  • 24 posts
  • Last active: Mar 06 2016 02:32 AM
  • Joined: 08 Mar 2014

@faqbot

 

Now that I understand the syntax better, the work-around you suggested seams to be working!  Thanks!

 

However, I still say TF should have handled this in the WorkingDir without the work-around.  ;)



ozzii
  • Members
  • 167 posts
  • Last active: Oct 30 2015 04:54 PM
  • Joined: 23 Mar 2011
For me for working, I've change into the tf.ahk "Dir := A_ScriptDir" to "Dir := A_WorkingDir"

Sprinkle Master
  • Members
  • 24 posts
  • Last active: Mar 06 2016 02:32 AM
  • Joined: 08 Mar 2014

For me for working, I've change into the tf.ahk "Dir := A_ScriptDir" to "Dir := A_WorkingDir"

Nice one ozzii  :)

 

I found that string in 2 places and just changed them both.  Not sure why it wasn't that way to start but I bet this will fix my problem.

 

Thanks!  :)



ozzii
  • Members
  • 167 posts
  • Last active: Oct 30 2015 04:54 PM
  • Joined: 23 Mar 2011
You're welcome. And yes, me too I don't know why TF don't use this syntax by default.

Sprinkle Master
  • Members
  • 24 posts
  • Last active: Mar 06 2016 02:32 AM
  • Joined: 08 Mar 2014

You're welcome. And yes, me too I don't know why TF don't use this syntax by default.

 

Thanks again!  Your correction for the TF file has done the trick!  :)



Elalya
  • Members
  • 8 posts
  • Last active: Jul 09 2014 08:27 AM
  • Joined: 28 Jun 2014
Hi. Thanks for this lib, it's great. I am having one issue with tf_find, however:

;Menu, Tray, Icon, config.ico

#include tf.ahk

FileSelectFile, LogOriginal, 3, , Select Log
FileSelectFile, LogNew, 8, , Select Destination
FileCopy %logoriginal%, %lognew%


Lines:=TF_ReadLines(lognew)
MsgBox , , Starting, Grabbing GPS data. This may take a few minutes.

RCOU:=TF_Find(Lines,1,0,"RCOU", 0, 0)
TF_RemoveLines(Lines,RCOU)

GPS:=TF_Find(Lines,1,0,"GPS, 3,", 0, 1)

FileDelete, %lognew%
;FileAppend, %GPS%, %lognew%
TF_Save(GPS,lognew)

Parent := SubStr(lognew, 1, InStr(SubStr(lognew,1,-1), "\", 0, 0)-1)
MsgBox , , Done, GPS data written to directory %Parent%.
The following is a sample of the log that I am trying to filter:
FMT, 27, 25, MAG2, Ihhhhhhhhh, TimeMS,MagX,MagY,MagZ,OfsX,OfsY,OfsZ,MOfsX,MOfsY,MOfsZ
FMT, 6, 19, PM, BBHHIhBHB, RenCnt,RenBlw,NLon,NLoop,MaxT,PMT,I2CErr,INSErr,INAVErr
FMT, 8, 20, CMD, BBBBBeLL, CTot,CNum,CId,COpt,Prm1,Alt,Lat,Lng
FMT, 1, 19, ATT, IccccCC, TimeMS,DesRoll,Roll,DesPitch,Pitch,DesYaw,Yaw
FMT, 3, 6, MODE, Mh, Mode,ThrCrs
FMT, 10, 3, STRT, , 
FMT, 13, 4, EV, B, Id
FMT, 20, 6, D16, Bh, Id,Value
RCOU, GPS
RCOU, GPS, 3
RCOU, RCOU
FMT, 21, 6, DU16, BH, Id,Value
FMT, 22, 8, D32, Bi, Id,Value
FMT, 23, 8, DU32, BI, Id,Value
FMT, 24, 8, DFLT, Bf, Id,Value
FMT, 18, 27, CAM, IHLLeccC, GPSTime,GPSWeek,Lat,Lng,Alt,Roll,Pitch,Yaw
FMT, 19, 5, ERR, BB, Subsys,ECode
PARM, SYSID_SW_MREV, 120
PARM, SYSID_SW_TYPE, 10
PARM, SYSID_THISMAV, 1
PARM, SYSID_MYGCS, 253
PARM, SERIAL1_BAUD, 57
PARM, SERIAL2_BAUD, 57
PARM, TELEM_DELAY, 0
PARM, RTL_ALT, 1500
PARM, SONAR_ENABLE, 0
PARM, SONAR_TYPE, 0
PARM, SONAR_GAIN, 0.8
PARM, FS_BATT_ENABLE, 1
PARM, FS_BATT_VOLTAGE, 13.8
The following is the output that my script currently gives:
1: 1: 17: 380: GPS, 3, 427268000, 1795, 10, 1.54, 37.7547854, -121.9122698, 4.93, 144.33, 0.02, 272.82, 0.02, 686631
2: 2: 18: 383: GPS, 3, 427268800, 1795, 10, 1.54, 37.7547852, -121.9122695, 4.93, 144.35, 0.02, 272.82, 0, 686651
The following is what I need the output to give:
GPS, 3, 427268000, 1795, 10, 1.54, 37.7547854, -121.9122698, 4.93, 144.33, 0.02, 272.82, 0.02, 686631
GPS, 3, 427268800, 1795, 10, 1.54, 37.7547852, -121.9122695, 4.93, 144.35, 0.02, 272.82, 0, 686651
I am not sure why it is putting all of those strange integers in front of each line. Does anyone know the answer?

Thanks!

faqbot
  • Members
  • 997 posts
  • Last active:
  • Joined: 10 Apr 2012
Just guessing - I think you have only copied a partial section of your code those line numbers mean you probably pass on a ReturnText parameter 2 - see https://github.com/hi5/TF#TF_Find - so the returned text include the line numbers of the lines it finds the text on. So perhaps somewhere else in your call TF_Find with a 2 value at the end? There may be other errors in your script - I've commented a few lines
;Menu, Tray, Icon, config.ico

#include tf.ahk

FileSelectFile, LogOriginal, 3, , Select Log
FileSelectFile, LogNew, 8, , Select Destination
FileCopy %logoriginal%, %lognew%


Lines:=TF_ReadLines(lognew) ; you could do this but there is no need for this (you could use FileRead, that should be faster for large(r) files - but remember lines is now a variable
MsgBox , , Starting, Grabbing GPS data. This may take a few minutes.

RCOU:=TF_Find(Lines,1,0,"RCOU", 0, 0) ; this looks good, remember TF_Find is using RegEx and is therefore case sensitive
TF_RemoveLines(Lines,RCOU) ; probably incorrect - you have to remember lines is now a variable - see above - that means that if you want to remove the lines from the variable you need to return the result to a variable as well so actually:
; lines:=TF_RemoveLines(Lines,RCOU) ; this would be correct

GPS:=TF_Find(Lines,1,0,"GPS, 3,", 0, 1) ; looks OK

FileDelete, %lognew% ;  ; TF_Save should already do this so no need 
;FileAppend, %GPS%, %lognew%
TF_Save(GPS,lognew)

Parent := SubStr(lognew, 1, InStr(SubStr(lognew,1,-1), "\", 0, 0)-1) ; seems like a complicated way to get the directory name, have a look at http://ahkscript.org/docs/commands/SplitPath.htm
MsgBox , , Done, GPS data written to directory %Parent%.


Elalya
  • Members
  • 8 posts
  • Last active: Jul 09 2014 08:27 AM
  • Joined: 28 Jun 2014
Hi FAQBot.

I just want to personally thank you for the help on this. I am quite new to this game and it has been very satisfying wading through unknown territory. Your comments are really fantastic and have given me a great foothold to begin moving again.

Two more questions, if you don't mind me imposing them:

1. The only modification that I have made is copying your method of handling the lines variable in regards to TF_Remove_Lines. Now, my output is 0, as an integer.
hCgxxjg.png
;Menu, Tray, Icon, config.ico

#include tf.ahk

FileSelectFile, LogOriginal, 3, , Select Log
FileSelectFile, LogNew, 8, , Select Destination
FileCopy %logoriginal%, %lognew%


Lines:=TF_ReadLines(lognew) ; Change later if slow; http://www.autohotkey.com/docs/commands/FileRead.htm
;MsgBox , , Starting, Grabbing GPS data. This may take a few minutes., 3

RCOU:=TF_Find(Lines,1,0,"RCOU", 0, 0)
lines:=TF_RemoveLines(Lines,RCOU) 

GPS:=TF_Find(Lines,1,0,"GPS, 3,", 0, 1)

MsgBox % GPS

;FileDelete, %lognew% ; TF_Save handles the deleting of the copied version and appending of the new version. 
;FileAppend, %GPS%, %lognew%
TF_Save(GPS,lognew)

Parent := SubStr(lognew, 1, InStr(SubStr(lognew,1,-1), "\", 0, 0)-1) ; Change later; http://ahkscript.org/docs/commands/SplitPath.htm
;MsgBox , , Done, GPS data written to directory %Parent%.
To create transparency, I created a new source file for the log. It is a few hundred lines of this:
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
RCOU
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
GPS, 3,
My output is still 0, so I rolled back to the original version of the script that I posted which was giving me a result close to what I wanted--and strangely enough, the output is also 0.

Clearly, there is something strange happening outside of the script. I didn't change anything other than put my desktop to sleep last night. Any ideas?

2. This is indeed my full script. Can you think of another reason that the output would be printing multiple copies of line numbers?

Elal.

Elalya
  • Members
  • 8 posts
  • Last active: Jul 09 2014 08:27 AM
  • Joined: 28 Jun 2014
EDIT: Oops, clicked post button too many times! Delete me.

Edited by Elalya, 28 June 2014 - 06:19 PM.