 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Should it be continued? |
| No, these are completely useless |
|
3% |
[ 1 ] |
| Yes, add more and more, make it big |
|
96% |
[ 31 ] |
|
| Total Votes : 32 |
|
| Author |
Message |
Originalsim
Joined: 08 Aug 2008 Posts: 9
|
Posted: Fri Aug 08, 2008 6:25 pm Post subject: |
|
|
| Code: | | MsgBox % TXT_TotalLines("c:\appdata.txt") |
that works and gives me the total count but I thought that this library was supposed to make a copy of the file (appdata_copy.txt) and append that info to it.
Thanks Guest for pointing out that I was missing the quotes, also. |
|
| Back to top |
|
 |
Originalsim
Joined: 08 Aug 2008 Posts: 9
|
Posted: Fri Aug 08, 2008 6:34 pm Post subject: nevermind |
|
|
| nevermind, I got it. thanks. |
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 184
|
Posted: Tue Aug 12, 2008 7:14 pm Post subject: |
|
|
I've seen a need to insert text to a specific column in a single specific line.
e.g. TXT_Insert(TextFile, Line, Column, Text)
or maybe TXT_ColPut() could be modified to accept start and end lines.
I didn't see that this functionality is already present, but I could be wrong.
 _________________
A great Beginner's Tutorial |
|
| Back to top |
|
 |
HugoV
Joined: 27 May 2007 Posts: 650
|
Posted: Tue Aug 12, 2008 7:32 pm Post subject: |
|
|
Hi, give this a few test whirls to see if suits your needs.
If you need just one line use the same startline and endline value, if the endline value is omitted it processes the rest of the file from the startline. | Code: | /*
Based on:
COLPUT.EXE & CUT.EXE, ftp://garbo.uwasa.fi/pc/ts/tsfltc22.zip
*/
;TXT_ColPut("c:\TestFile.txt", "4", "InsertThis", "3", "3")
TXT_ColPut(TextFile, StartColumn, Text, Startline = 0, EndLine = 0, Skip = 0){ ; skip = shorter lines (e.g. lines shorter startcolumn position)
Original := A_BatchLines
SetBatchLines, -1
StartColumn--
FileRead, Str, %TextFile%
TextFile := (SubStr(TextFile,1,1)="!") ? (SubStr(TextFile,2),OW=1) : TextFile
If (Endline = 0)
EndLine = 10000000000000000000000 ; this should take care of most text files ...
Loop, Parse, Str, `n, `r
{
if A_Index between %StartLine% and %EndLine%
{
StringLeft, Section1, A_LoopField, StartColumn
StringMid, Section2, A_LoopField, StartColumn
If (Skip = 1) and (StrLen(A_LoopField) < StartColumn)
OutPut .= Section1 Section2 "`n"
Else
OutPut .= Section1 Text Section2 "`n"
}
Else
OutPut .= A_LoopField "`n"
}
StringTrimRight, OutPut, OutPut, 1 ; trim empty line added
If OW {
FileDelete, %TextFile%
FileAppend, %OutPut%, %TextFile%
}
Else {
SplitPath, TextFile,, Dir, Ext, Name
FileDelete, % Dir "\" Name "_copy." Ext
FileAppend, %OutPut%, % Dir "\" Name "_copy." Ext
}
SetBatchLines, %Original%
}
;TXT_ColCut("c:\TestFile.txt", "2", "8", "2", "2")
TXT_ColCut(TextFile, StartColumn, EndColumn, StartLine = 0, EndLine = 0){
Original := A_BatchLines
SetBatchLines, -1
TextFile := (SubStr(TextFile,1,1)="!") ? (SubStr(TextFile,2),OW=1) : TextFile
StartColumn--
EndColumn++
FileRead, Str, %TextFile%
If (Endline = 0)
EndLine = 10000000000000000000000 ; this should take care of most text files ...
Loop, Parse, Str, `n, `r
{
if A_Index between %StartLine% and %EndLine%
{
StringLeft, Section1, A_LoopField, StartColumn
StringMid, Section2, A_LoopField, EndColumn
OutPut .= Section1 Section2 "`n"
}
Else
OutPut .= A_LoopField "`n"
}
StringTrimRight, OutPut, OutPut, 1 ; trim empty line added
If OW {
FileDelete, %TextFile%
FileAppend, %OutPut%, %TextFile%
}
Else {
SplitPath, TextFile,, Dir, Ext, Name
FileDelete, % Dir "\" Name "_copy." Ext
FileAppend, %OutPut%, % Dir "\" Name "_copy." Ext
}
SetBatchLines, %Original%
} |
_________________ When parsing a CSV file use Loop, parse, Inputvar, CSV! |
|
| Back to top |
|
 |
Originalsim
Joined: 08 Aug 2008 Posts: 9
|
Posted: Wed Aug 13, 2008 1:33 am Post subject: |
|
|
| HugoV wrote: | Hi, give this a few test whirls to see if suits your needs.
|
This works exactly for what I am working on.
But I would like to be able to run multiple input's.
If I do:
| Code: | TXT_ColPut("c:\TestFile.txt", "4", "InsertThis", "2", "2")
TXT_ColPut("c:\TestFile.txt", "5", "fish", "3", "3") |
I just get a file with word fish in it.
If I do:
| Code: | TXT_ColPut("c:\TestFile.txt", "4", "InsertThis", "2", "2")
TXT_ColPut("!c:\TestFile_copy.txt", "5", "fish", "3", "3") |
I get a blank document.
Oh I had to make a blank testfile.txt filled with spaces so I could just fill a new file. If you just make a new document you always get returned a blank document.
any of the above if just kept to a single line works perfectly. |
|
| Back to top |
|
 |
HugoV
Joined: 27 May 2007 Posts: 650
|
Posted: Wed Aug 13, 2008 7:27 am Post subject: |
|
|
The TXT library works with files that already exist, you can not use it to create new files from scratch. So it should work if you start with a big empty file, but like I said in response to your other question it would be very very slow as it would require multiple passes of the same file. Just write your own script it will be so much faster and easier to modify, don't waste your time on trying to use something that is not meant for your purpose. _________________ When parsing a CSV file use Loop, parse, Inputvar, CSV! |
|
| Back to top |
|
 |
olegbl
Joined: 13 Dec 2006 Posts: 42
|
Posted: Mon Aug 25, 2008 7:25 am Post subject: |
|
|
Thank you for your work! Great library!
Any chance of a find function, that'd work kind of like replace, except returns the line number/numbers instead of replacing text?
Edit:
Something like this? :
| Code: | TXT_Find(TextFile,SearchText,CaseSensitive=false,StartLine=1,EndLine=0)
{
Loop
{
Index := A_Index + StartLine - 1
if (Index > EndLine && EndLine != 0) ; Not Found!
return "0"
FileReadLine, line, magnet.ini, %Index%
if (ErrorLevel) ; Not Found!
return "0"
if (InStr(line,SearchText,CaseSensitive)) ; Yay! Found!
return Index
}
} |
Example:
| Code: | ;Find first occurance of "Hi!" in file "Lol.txt"
FirstLine := TXT_Find("Lol.txt", "Hi!", true)
; Find second occurance of "Hi!" in file "Lol.txt"
SecondLine := TXT_Find("Lol.txt", "Hi!", true, FirstLine+1) |
_________________ Real arrays are here: AHKArray Version 6! |
|
| Back to top |
|
 |
heresy
Joined: 11 Mar 2008 Posts: 291
|
Posted: Wed Aug 27, 2008 7:20 am Post subject: |
|
|
Thanks guys.
i'll rearrange this library somewhat soon. _________________ Easy WinAPI - Dive into Windows API World
Benchmark your AutoHotkey skills at PlayAHK.com |
|
| Back to top |
|
 |
mcatalini Guest
|
Posted: Fri Sep 19, 2008 1:48 pm Post subject: Split text file |
|
|
Hi, I want to know if it is possible using this library split a text file based.
For instance, I have a file with 2 markers, and I want to generate 2 different text files, one for each marker.
Thanks. |
|
| Back to top |
|
 |
HugoV
Joined: 27 May 2007 Posts: 650
|
Posted: Fri Sep 19, 2008 5:26 pm Post subject: |
|
|
Here are two Split file functions which have not been added to the
library yet, you can split by number of lines or by text. Examples
included and be sure to check the InFile parameter.
TXT_SplitFileByLines
| Code: |
TestFile=
(join`r`n
01 AutoHotkey is a free, open-source utility for Windows. With it, you can:
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.
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 * 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.
)
FileDelete, %A_ScriptDir%\TestFile.txt
FileAppend, %TestFile%,%A_ScriptDir%\TestFile.txt
;TXT_SplitFileByLines(TextFile, SplitAt, InFile = 1, Prefix = "file", Extension = "txt")
;example:
TXT_SplitFileByLines("TestFile.txt", "4", "sfile_", "txt", "1") ; split file every 3 lines
; InFile = 0 skip line e.g. do not include the actual line in any of the output files
; InFile = 1 include line IN current file
; InFile = 2 include line IN next file
TXT_SplitFileByLines(TextFile, SplitAt, Prefix = "file", Extension = "txt", InFile = 1){
FileRead, Str, %TextFile%
SplitPath, TextFile,, Dir
If (Dir = "") ; if Dir is empty TextFile & script are in same directory
Dir := A_ScriptDir
LineCounter=1
FileCounter=1
Loop, Parse, Str, `n, `r
{
OutPut .= A_LoopField "`n"
If (LineCounter = SplitAt)
{
If (InFile = 0)
{
If (PreviousOutput <> "") ; skip empty files
FileAppend, %PreviousOutput%,%Dir%\%Prefix%%FileCounter%.%Extension%
Output:=
}
If (InFile = 1)
{
FileAppend, %OutPut%,%Dir%\%Prefix%%FileCounter%.%Extension%
Output:=
}
If (InFile = 2)
{
OutPut := PreviousOutput
If (OutPut <> "") ; skip empty files
FileAppend, %OutPut%,%Dir%\%Prefix%%FileCounter%.%Extension%
OutPut := A_LoopField "`n"
}
LineCounter=0
FileCounter++
}
LineCounter++
PreviousOutput:=Output
PreviousLine:=A_LoopField
}
FileAppend, %OutPut%,%Dir%\%Prefix%%FileCounter%.%Extension% ; write last file
} |
TXT_SplitFileByText
| Code: |
TestFile=
(join`r`n
01 AutoHotkey is a free, open-source utility for Windows. With it, you can:
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.
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 * 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.
)
FileDelete, %A_ScriptDir%\TestFile.txt
FileAppend, %TestFile%,%A_ScriptDir%\TestFile.txt
TXT_SplitFileByText("TestFile.txt", "button", "sfile_", "txt") ; split file at every line with button in it, can be regexp
; InFile = 0 skip line e.g. do not include the actual line in any of the output files
; InFile = 1 include line IN current file
; InFile = 2 include line IN next file
TXT_SplitFileByText(TextFile, SplitAt,Prefix = "file", Extension = "txt", InFile = 1)
{
FileRead, Str, %TextFile%
SplitPath, TextFile,, Dir
If (Dir = "") ; if Dir is empty TextFile & script are in same directory
Dir := A_ScriptDir
LineCounter=1
FileCounter=1
Loop, Parse, Str, `r, `n
{
OutPut .= A_LoopField "`n"
FoundPos:=RegExMatch(A_LoopField, SplitAt)
If (FoundPos > 0)
{
If (InFile = 0)
{
If (PreviousOutput <> "") ; skip empty files
FileAppend, %PreviousOutput%,%Dir%\%Prefix%%FileCounter%.%Extension%
Output:=
}
If (InFile = 1)
{
FileAppend, %OutPut%,%Dir%\%Prefix%%FileCounter%.%Extension%
Output:=
}
If (InFile = 2)
{
OutPut := PreviousOutput
If (OutPut <> "") ; skip empty files
FileAppend, %OutPut%,%Dir%\%Prefix%%FileCounter%.%Extension%
OutPut := A_LoopField "`n"
}
LineCounter=0
FileCounter++
}
LineCounter++
PreviousOutput:=Output
PreviousLine:=A_LoopField
}
If (Output <> "")
FileAppend, %OutPut%,%Dir%\%Prefix%%FileCounter%.%Extension% ; write last file
}
|
_________________ When parsing a CSV file use Loop, parse, Inputvar, CSV! |
|
| Back to top |
|
 |
sebastian
Joined: 06 Nov 2007 Posts: 13 Location: Sydney
|
Posted: Thu Oct 09, 2008 5:21 am Post subject: |
|
|
hy this seems like a very lib.
is it possible to find lines starting with a given string?
my problem is that the line number might change but the start of the line will be the same.
Or should I use other functions? |
|
| Back to top |
|
 |
HugoV
Joined: 27 May 2007 Posts: 650
|
Posted: Thu Oct 09, 2008 12:53 pm Post subject: |
|
|
| sebastian wrote: | is it possible to find lines starting with a given string?
my problem is that the line number might change but the start of the line will be the same. | Yes that would be possible. Add the TXT_Find function above from olegbl to the library.
Now use TXT_Find to find the startline, now use that value and call
the other functions you need using the value returned from TXT_Find.
You may need to experiment a bit to make it work. _________________ When parsing a CSV file use Loop, parse, Inputvar, CSV! |
|
| Back to top |
|
 |
sebastian
Joined: 06 Nov 2007 Posts: 13 Location: Sydney
|
Posted: Mon Oct 13, 2008 2:58 am Post subject: |
|
|
Hi
| HugoV wrote: | Now use TXT_Find to find the startline, now use that value and call
the other functions you need using the value returned from TXT_Find.
You may need to experiment a bit to make it work. |
As a general remark it might be useful for noowbs like me to tell exactly where the TXT.ahk needs to go. That the lib folder needs to be created if not already there...
by now I don't know what combination to try else...I am lost
| Code: | | LineSolveModelFile := TXT_Find("C:\11m H SG2 UBC1 Rev2.txt", "SolveModelFile=15m H SG2 Strand2.4.st7", false) |
should this not give me the resulting line number in LineSolveModelFile?
does the string I am looking for need to be exactly the whole line? or can I use something like "SolveModelFile*"?
In my case my variable always gets set to 0 and I tried both versions
Any thoughts? |
|
| Back to top |
|
 |
HugoV
Joined: 27 May 2007 Posts: 650
|
Posted: Mon Oct 13, 2008 7:14 pm Post subject: |
|
|
| sebastian wrote: | | Any thoughts? | Try this version of TXT_Find, the one above doesn't work correctly. | Code: | LineSolveModelFile := TXT_Find("C:\11m H SG2 UBC1 Rev2.txt", "SolveModelFile=15m H SG2 Strand2.4.st7", false)
MsgBox %LineSolveModelFile%
TXT_Find(TextFile,SearchText,CaseSensitive=false,StartLine=1,EndLine=0)
{
; based olegbl on http://www.autohotkey.com/forum/post-216155.html#216155
FileRead, Str, %TextFile%
Loop, Parse, Str, `n
{
If (A_Index < StartLine)
Continue
If (A_Index > EndLine && EndLine != 0) ; Not Found!
Break
If (InStr(A_LoopField,SearchText,CaseSensitive)) ; Yay! Found!
{
Found := A_Index
Break
}
Found = 0 ; not found, so
}
Return Found
} | The text you search for can be a partial match but don't use wildcards, so SolveModelFile wil work, SolveModelFile* will not work.
Edit: updated TXT_Find used in example above _________________ When parsing a CSV file use Loop, parse, Inputvar, CSV!
Last edited by HugoV on Sat Oct 18, 2008 11:26 am; edited 1 time in total |
|
| Back to top |
|
 |
HugoV
Joined: 27 May 2007 Posts: 650
|
Posted: Sat Oct 18, 2008 11:23 am Post subject: |
|
|
Based on olegbls' idea of TXT_Find here is a TXT_Findlines function which returns all lines with the searched text, comma separated for ease of use and possible further processing. | Code: | TestFile=
(join`r`n
01 AutoHotkey is a free, open-source utility for Windows. With it, you can:
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.
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 * 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.
)
FileDelete, %A_ScriptDir%\TestFile.txt
FileAppend, %TestFile%,%A_ScriptDir%\TestFile.txt
Lines := TXT_FindLines("TestFile.txt", "and", false, 1,9)
MsgBox %Lines%
TXT_FindLines(TextFile,SearchText,CaseSensitive=false,StartLine=1,EndLine=0)
{
; based on olegbl, see http://www.autohotkey.com/forum/post-216155.html#216155
FileRead, Str, %TextFile%
Loop, Parse, Str, `n
{
If (A_Index < StartLine)
Continue
If (A_Index > EndLine && EndLine != 0) ; Not Found!
Break
If (InStr(A_LoopField,SearchText,CaseSensitive)) ; Yay! Found!
Lines .= A_Index ","
}
If (Lines <> "")
StringTrimRight, Lines, Lines, 1 ; trim trailing ,
Else
Lines = 0
Return Lines
} |
_________________ When parsing a CSV file use Loop, parse, Inputvar, CSV! |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|