本帖最后由 smooth 于 2014-2-10 22:00 编辑
$sFilePath = "demo.txt"
Func _FileCountLines($sFilePath)
Local $hFile = FileOpen($sFilePath, 0)
If $hFile = -1 Then Return SetError(1, 0, 0)
Local $sFileContent = StringStripWS(FileRead($hFile), 2)
FileClose($hFile)
Local $aTmp
If StringInStr($sFileContent, @LF) Then
$aTmp = StringSplit(StringStripCR($sFileContent), @LF)
ElseIf StringInStr($sFileContent, @CR) Then
$aTmp = StringSplit($sFileContent, @CR)
Else
If StringLen($sFileContent) Then
Return 1
Else
Return SetError(2, 0, 0)
EndIf
EndIf
Return $aTmp[0]
EndFunc ;==>_FileCountLines
Local $nLines, $Mid
$lines = _FileCountLines($sFilePath)
$hOpen = FileOpen($sFilePath, 0)
For $i = 1 To $lines
$rLine = FileReadLine($hOpen)
If StringRegExp($rLine, "\d") And StringInStr($rLine, "YES") Then
$rLine = StringReplace($rLine, "YES", "")
$rLine = StringRegExpReplace ($rLine, "[^A-Za-z0-9/\_/]", "")
For $i = 1 To StringLen($rLine)
$Mid = StringLeft($rLine, $i)
If StringIsDigit($Mid) = 0 Then
$r = $i - 1
ExitLoop
EndIf
Next
$rLine = StringRight($rLine, StringLen($rLine) - $r)
$nLines &= $rLine & @CRLF
EndIf
Next
FileClose($hOpen)
$hOpen = FileOpen($sFilePath, 2)
Local $Fwrite = FileWrite($hOpen, $nLines)
FileClose($hOpen)
如果文件名里有空格,这些代码就解决不了了,因为在用正则替换的过程中,把空格都搞掉了。 |