mooseydoom Posted July 28, 2005 Posted July 28, 2005 (edited) Hey, I just modified the existing _FileCountLines function to count the number of commented lines and the number of un-commented lines. (Commented meaning that a line begins with a specified character sequence). Not sure if this is useful enough to submit as a User Defined Function... Post here if you think it's useful. =) Edit: added _FileCountCommentLines which does both of the _FileCountCommentedLines and _FileCountUnCommentedLines Edit: added Skip Header option & attached code zipped with example test cases expandcollapse popup;=============================================================================== ; ; Description: Returns the number of lines in the specified file ; which start with the specified comment character. ; Syntax: _FileCountCommentedLines( $sFilePath, $sCommentChar [, $iSkipHeaderLines=0] ) ; Parameter(s): $sFilePath - Path and filename of the file to be read ; $sCommentChar - A line is considered a commented line if this ; character sequence starts the line. ; [optional]$iSkipHeaderLines - skip this many lines at the beginning of the file ; Requirement(s): None ; Return Value(s): On Success - Returns number of commented lines in the file ; On Failure - Returns 0 and sets @error = 1 ; Author(s): Kristi <kristi.tsukida@gmail.com> (modified _FileCountLines) ; Tylo <tylo@start.no> (wrote the original _FileCountLines) ; Related Functions: _FileCountLines, _FileCountUnCommentedLines, _FileCountCommentLines ; ;=============================================================================== Func _FileCountCommentedLines($sFilePath, $sCommentChar, $iSkipHeaderLines = 0) Local $N = FileGetSize($sFilePath) - 1 If @error Or $N = -1 Then Return 0 ;This is the file with an added @CR character on each line with the first ;$iSkipHeaderLines number of @LF characters removed Local $sFile = StringReplace(@LF & @LF & StringAddCR(FileRead($sFilePath, $N)), @LF, "", $iSkipHeaderLines + 1) ;remove @LF characters for each line which begins with the $sCommentChar StringReplace($sFile, @LF & $sCommentChar, $sCommentChar, 0, 1) ;The number of commented lines is the number of replacements which were made Return @extended EndFunc ;==>_FileCountCommentedLines ;=============================================================================== ; ; Description: Returns the number of lines in the specified file, ; not including lines that start with the specified ; comment char. ; Syntax: _FileCountUnCommentedLines( $sFilePath, $sCommentChar [, $iSkipHeaderLines=0] ) ; Parameter(s): $sFilePath - Path and filename of the file to be read ; $sCommentChar - A line is considered a commented line if ; this character sequence starts the line. ; [optional]$iSkipHeaderLines - skip this many lines at the beginning of the file ; Requirement(s): None ; Return Value(s): On Success - Returns number of uncommented lines in the file ; On Failure - Returns 0 and sets @error = 1 ; Author(s): Kristi <kristi.tsukida@gmail.com> (modified _FileCountLines) ; Tylo <tylo@start.no> (wrote original _FileCountLines) ; Related Functions: _FileCountLines, _FileCountCommentedLines, _FileCountCommentLines ; ;=============================================================================== Func _FileCountUnCommentedLines($sFilePath, $sCommentChar, $iSkipHeaderLines = 0) Local $N = FileGetSize($sFilePath) - 1 If @error Or $N = -1 Then Return 0 ;This is the file with an added @CR character on each line with the first ;$iSkipHeaderLines number of @LF characters removed Local $sFile = StringReplace(@LF & @LF & StringAddCR(FileRead($sFilePath, $N)), @LF, "", $iSkipHeaderLines + 1) ;Remove @LF characters for each line which begins with the $sCommentChar ;The number of additional characters is the number of un-commented lines Return StringLen(StringReplace($sFile, @LF & $sCommentChar, $sCommentChar, 0, 1)) - $N EndFunc ;==>_FileCountUnCommentedLines ;=============================================================================== ; ; Description: Counts the number of commented lines, un-commented lines, ; and total number of lines ; Syntax: _FileCountCommentLines( $sFilePath, $sCommentChar [, $iSkipHeaderLines=0] ) ; Parameter(s): $sFilePath - Path and filename of the file to be read ; $sCommentChar - A line is considered a commented line if ; this character sequence starts the line. ; [optional]$iSkipHeaderLines - skip this many lines at the beginning of the file ; Requirement(s): None ; Return Value(s): On Success - Returns an array ; array[1] = number of commented lines ; array[2] = number of un-commented lines ; array[3] = total number of lines ; On Failure - Returns 0 and sets @error = 1 ; Author(s): Kristi <kristi.tsukida@gmail.com> (modified _FileCountLines) ; gafrost (combined the commented and uncommented functions) ; Tylo <tylo@start.no> (wrote original _FileCountLines) ; Related Functions: _FileCountLines, _FileCountCommentedLines, _FileCountUnCommentedLines ; ;=============================================================================== Func _FileCountCommentLines($sFilePath, $sCommentChar, $iSkipHeaderLines = 0) Local $a_counts[4] Local $N = FileGetSize($sFilePath) - 1 If @error Or $N = -1 Then Return 0 ;This is the file with an added @CR character on each line with the first ;$iSkipHeaderLines number of @LF characters removed Local $sFile = StringReplace(@LF & @LF & StringAddCR(FileRead($sFilePath, $N)), @LF, "", $iSkipHeaderLines + 1) ;Remove @LF characters for each line which begins with the $sCommentChar Local $sReplaced = StringReplace($sFile, @LF & $sCommentChar, $sCommentChar, 0, 1) $a_counts[0] = 3 ;The number of commented lines is the number of replacements which were made $a_counts[1] = @extended;number of commented lines ;The number of additional characters is the number of un-commented lines $a_counts[2] = StringLen($sReplaced) - $N;number of un-commented lines $a_counts[3] = $a_counts[1] + $a_counts[2];total number of lines Return $a_counts EndFunc ;==>_FileCountCommentLinesfile2.zip Edited July 28, 2005 by mooseydoom
GaryFrost Posted July 28, 2005 Posted July 28, 2005 do them both in one function? Func _FileCountLines($sFilePath, $sCommentChar) Local $a_counts[4] Local $N = FileGetSize($sFilePath) - 1 If @error Or $N = -1 Then Return 0 StringReplace(@LF & FileRead($sFilePath, $N), @LF & $sCommentChar, $sCommentChar, 0, 1) $a_counts[0] = 3 $a_counts[1] = @extended $a_counts[2] = StringLen(StringReplace(@LF & StringAddCR(FileRead($sFilePath, $N)), @LF & $sCommentChar, $sCommentChar, 0, 1)) - $N $a_counts[3] = $a_counts[1] + $a_counts[2] Return $a_counts EndFunc ;==>_FileCountLines Dim $file = "file2.au3" Local $counts = _FileCountLines($file, ";") If IsArray($counts) Then For $x = 1 To $counts[0] MsgBox(0, "line count", $counts[$x]) Next EndIf SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
mooseydoom Posted July 28, 2005 Author Posted July 28, 2005 (edited) Hey, that's cool... slightly modified so it only reads the file once & changed name since there's already a _FileCountLines UDF. Func _FileCountCommentLines($sFilePath, $sCommentChar) Local $a_counts[4] Local $N = FileGetSize($sFilePath) - 1 If @error Or $N = -1 Then Return 0 Local $sReplaced = StringReplace(@LF & StringAddCR(FileRead($sFilePath, $N)), @LF & $sCommentChar, $sCommentChar, 0, 1) $a_counts[0] = 3 $a_counts[1] = @extended;number of commented lines $a_counts[2] = StringLen($sReplaced) - $N;number of un-commented lines $a_counts[3] = $a_counts[1] + $a_counts[2];total number of lines Return $a_counts EndFunc;==>_FileCountCommentLines PS: nice title, "Co-Author of CodeWizard" Edited July 28, 2005 by mooseydoom
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now