Jump to content
Sign in to follow this  
BrendanB

Need help grabbing line from text file...

Recommended Posts

BrendanB

Hello,

I'm using v3 and need help capturing the second-to-last line of text from a .txt file. The goal of this section of code is to grab this line of text from one file and write it to another. I've read on other threads that the "_FileCountLines()" function simply does not work. If not, I'd like another way to do what I need. Any suggestions?

#include <File.au3> 
$qv1=(_FileCountLines("I:\data\datesDB.txt") - 1)
$qv2=FileReadLine("I:\data\datesDB.txt", $qv1)
$queriesDB=FileOpen("I:\data\queriesDB.txt", 9)
FileWrite($queriesDB, $qv2)
FileClose($queriesDB)

Share this post


Link to post
Share on other sites
Killerthibs

hi :)

You can try the function IniRead ,if your text file is a .ini file :)

Share this post


Link to post
Share on other sites
BrendanB

hi :)

You can try the function IniRead ,if your text file is a .ini file :)

It's a straight text file (.txt), using DOS linefeeds to separate the lines of text (the file is generated by another part of my script)

Share this post


Link to post
Share on other sites
Armand

- can you give us an example of what the text file looks alike ?!

here you go mate: (that works 100% just checked)

;open the file for read!
$file = FileOpen("YourFileName.Whatever",0)
;reads the entire file into a variable
$text = FileRead ($file)
;parses the file text into lines using the line feed code
$fileLines = StringRegExp($text,"(.*)\n",3)
; now the number of file lines is: Ubound($fileLines)
MsgBox(0,"This is the first line:",$fileLines[0])
MsgBox(0,"This is the last line:",$fileLines[Ubound($fileLines)-2])
;Note that i've used '-2' because the counting start from 0 and not from 1!
FileClose("YourFileName.Whatever")
;don't forget to close the file after done!
Edited by Armand

[u]My Au3 Scripts:[/u]____________(E)Lephant, A Share download manager (RS/MU etc)Http1.1 Console, The Ez Way!Internet Reconnection Automation Suite & A Macro Recording Tool.SK's Alarm Clock, Playing '.MP3 & .Wav' Files._________________Is GOD a mistake of the Humanity Or the Humanity is a mistake of GOD ?!

Share this post


Link to post
Share on other sites
BrendanB

- can you give us an example of what the text file looks alike ?!

here you go mate: (that works 100% just checked)

;open the file for read!
$file = FileOpen("YourFileName.Whatever",0)
;reads the entire file into a variable
$text = FileRead ($file)
;parses the file text into lines using the line feed code
$fileLines = StringRegExp($text,"(.*)\n",3)
; now the number of file lines is: Ubound($fileLines)
MsgBox(0,"This is the first line:",$fileLines[0])
MsgBox(0,"This is the last line:",$fileLines[Ubound($fileLines)-2])
;Note that i've used '-2' because the counting start from 0 and not from 1!
FileClose("YourFileName.Whatever")
;don't forget to close the file after done!
Thanks for the reply. I'll try your code out when I get a chance. The text file I'm grabbing from is attached. The last line of said file is always blank (it is constantly added to later in the script).

datesDB.txt

Share this post


Link to post
Share on other sites
enaiman

There is another way to do it:

Use StringStripCR to read your file into an array (the element [0] of the array will hold the amount of lines the file has)

$aSplit = StringSplit(StringStripCR("I:\data\datesDB.txt"), @LF)
For $i = 2 To $aSplit[0]
FileReadLine ("I:\data\datesDB.txt", $i)
FileWriteLine ("I:\data\queriesDB.txt")
Next

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
BrendanB

Hmm.. doesn't appear to be working. I ran a little test script with some of your code on my development machine:

$aSplit = StringSplit(StringStripCR("C:\datesDB.txt"), @CRLF)
For $i = 2 To $aSplit[0]
FileOpen("C:\datesDB.txt", 0)
FileReadLine ("C:\datesDB.txt", $i)
FileClose("C:\datesDB.txt")
FileOpen("C:\queriesDB.txt", 1)
FileWriteLine ("C:\queriesDB.txt", $i)
FileClose("C:\queriesDB.txt")
Next

Share this post


Link to post
Share on other sites
Mast3rpyr0

Here you go

#include <GUIConstants.au3>
#include <Constants.au3>
#include <File.au3>

$dir="I:\data\datesDB.txt"
$dir2="I:\data\queriesDB.txt"
$qv1=_FileCountLines($dir)
$qv2=FileReadLine($dir, $qv1-1) ; subtract 1 from $qv1 to get second to last line
$queriesDB=FileOpen($dir2, 1) ; 9 doesnot exist
FileWrite($queriesDB, @CRLF & $qv2) ;write from datesDB to queriesDB on new line
FileClose($queriesDB)
MsgBox(0, "Done", "Operation Complete")

Share this post


Link to post
Share on other sites
BrendanB

Here you go

Thanks! That did the trick! You guys are going to hate me, but I was pressing F7, thinking it was the Go command, which is why none of the code you all posted worked. Man, I need some sleep! Thanks for all of your help, despite my "stupid user error". :)

Share this post


Link to post
Share on other sites
enaiman

You've made a mistake when trying to run the code I've posted

look:

my code:

$aSplit = StringSplit(StringStripCR("I:\data\datesDB.txt"), @LF)oÝ÷ Û*.­Êy«­¢+ØÀÌØíMÁ±¥ÐôMÑÉ¥¹MÁ±¥Ð¡MÑÉ¥¹MÑÉ¥Á
H ÅÕ½ÐíèÀäÈíÑ͹ÑáÐÅÕ½Ð줰
I1

and your script can't work because you were trying to split a string using @CRLF as delimiter while you stripped the @CR :)

No worries - glad you found some working code.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×