Jump to content
Sign in to follow this  

Can Autoit determine the number of lines in a file?

Recommended Posts

Well, it seems I can use @error to do what I wanted... Now, a new question!

When I have the time and date in this format, how will I be able to date add to it, as well as determine the day of the week.

Jul 19, '06 11:52

Edited by brodie28

Share this post

Link to post
Share on other sites

Counting lines in a file can be done with FileCountLines, while you can add to your

date with DateAdd. Btw, with that date-fomat you have to use the string-functions

to get it to the proper format, so that DateAdd can use the information.

Edited by Helge

Share this post

Link to post
Share on other sites

This was my own version of _FileCountLines, needed it for another other script I was working on

Just change the $filename var to a file you want to test this against

(Requires beta for this one)

Opt("MustDeclareVars", 1)


Func _Main()
    Local $a_count
    Local $file_name = @ScriptDir & "\test.au3"
    $a_count = _FileCountLines($file_name) ; get the count of all lines only
    MsgBox(0, "Count", $a_count)
    $a_count = _FileCountLines($file_name, ";", True) ; return array containing count of commented/un-commented/all lines
    If IsArray($a_count) Then
        MsgBox(0, "Count", "# of Comment Lines: " & @TAB & $a_count[1] & @LF & _
                "# of Un-Commented Lines: " & @TAB & $a_count[2] & @LF & _
                "The Total # of Lines: " & @TAB & $a_count[3])
        MsgBox(0, "Count", $a_count)
    $a_count = _FileCountLines($file_name, ";") ; return count of commented lines
    MsgBox(0, "Count", $a_count)
    $a_count = _FileCountLines($file_name, ";", Default, 2) ; return count of un-commented lines
    MsgBox(0, "Count", $a_count)
EndFunc   ;==>_Main

Func _FileCountLines($sFilePath, $sCommentChar = "", $b_ReturnArray = False, $i_ReturnCount = 1)
    Local $a_counts[4]
    Local $N = FileGetSize($sFilePath) - 1
    If @error Or $N = -1 Then Return 0
    If @NumParams = 1 Then Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1
    Local $s_text = StringReplace(StringReplace(FileRead($sFilePath, $N), " ", ""), @TAB, "")
    Local $sReplaced = StringReplace(@LF & StringAddCR($s_text), @LF & $sCommentChar, $sCommentChar, 0, 1)
    $a_counts[0] = 3
    $a_counts[1] = @extended;number of commented lines
    $a_counts[2] = StringLen($sReplaced) - StringLen($s_text);number of un-commented lines
    $a_counts[3] = $a_counts[1] + $a_counts[2];total number of lines
    If $b_ReturnArray Then
        Return $a_counts
        If $b_ReturnArray <> 1 And $b_ReturnArray <> 2 Then $b_ReturnArray = 1
        Return $a_counts[$i_ReturnCount]
EndFunc   ;==>_FileCountLines

SciTE for AutoItDirections for Submitting Standard UDFs


Don't argue with an idiot; people watching may not be able to tell the difference.


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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...