AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

counting lines in text file

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
chipmanners



Joined: 30 Aug 2007
Posts: 70

PostPosted: Sun Dec 02, 2007 3:15 am    Post subject: counting lines in text file Reply with quote

hi,

Im looking to count the number of lines in a text file and return it as a variable. Is there a way to do this?

Thanks,
Chip
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 2556

PostPosted: Sun Dec 02, 2007 3:56 am    Post subject: Reply with quote

Code:
FileRead, file, YourFile.txt
StringSplit, File, File, `n
MsgBox, Your file has %File0% lines.

_________________
My Home Thread
Common Answers: [1]. It's in the FAQ [2]. Ternary ( a ? b : c ) guide [3]. Post code inside [code][/code] tags !
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7609

PostPosted: Sun Dec 02, 2007 4:26 am    Post subject: Reply with quote

Whoa! That would create as many variables Shocked

Better to use StringReplace:

Code:
FileRead, Var, %A_ScriptFullPath%
StringReplace, Var, Var, `n,`n, UseErrorLevel
Lines := ErrorLevel+1
Var= ; empty it

MsgBox, %A_ScriptFullPath%`ncontains %Lines% lines.

;
;
;
;
;
;


Smile
Back to top
View user's profile Send private message
Wouther



Joined: 01 May 2007
Posts: 83
Location: The Netherlands

PostPosted: Sun Dec 02, 2007 1:34 pm    Post subject: Reply with quote

Skan wrote:
Code:
FileRead, Var, %A_ScriptFullPath%
StringReplace, Var, Var, `n,`n, UseErrorLevel
Lines := ErrorLevel+1
Var= ; empty it

MsgBox, %A_ScriptFullPath%`ncontains %Lines% lines.

Woah! Just what I needed! Thanks! Cool
_________________
Printing css/html-formatted text
Back to top
View user's profile Send private message
engunneer



Joined: 30 Aug 2005
Posts: 7703
Location: Madison, WI

PostPosted: Tue Dec 04, 2007 12:07 am    Post subject: Reply with quote

another way
Code:

Loop, Read, YourFile.txt
  numlines := A_Index

Msgbox, %numlines%

_________________
Unless noted, all code is UNTESTED.
Answers Here: 1.(Loops, Viruses, etc.) 2.Search 3.RTFM 4.Ask for Help.
PMs will be ignored unless you are hiring me.
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 7609

PostPosted: Tue Dec 04, 2007 12:25 am    Post subject: Reply with quote

That would be slower, but will consume less memory! Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group