[已解决]請問filewriteline這個函數如何寫入文字到指定行數
本帖最后由 ipmitool 于 2016-12-20 18:42 编辑請問如何寫入$serial的字串到testconfig.ini的第5行, 也就是指定行數來做寫入 $testconfig =FileOpen("testconfig.ini", 2)
$serial=InputBox ("Input serial number" , "Please input system serial number: " & @CRLF _
& @CRLF _
&"Example: XXPFGGSHK01-001" ,"","")
FileWriteLine($testconfig,$serial)
帮助里找_FileWriteToLine,如果写不上可以用下面那个,这是论坛里前辈修改过的,自带的可能有点问题Func _FileWriteToLine($sFilePath, $iLine, $sText, $bOverWrite = False)
If $iLine <= 0 Then Return SetError(4, 0, 0)
If Not IsString($sText) Then
$sText = String($sText)
If $sText = "" Then Return SetError(6, 0, 0)
EndIf
If $bOverWrite = Default Then $bOverWrite = False
If Not (IsBool($bOverWrite) Or $bOverWrite = 0 Or $bOverWrite = 1) Then Return SetError(5, 0, 0) ; For old versions.
If Not FileExists($sFilePath) Then Return SetError(2, 0, 0)
Local $aArray = FileReadToArray($sFilePath)
Local $iUBound = UBound($aArray) - 1
If ($iUBound + 1) < $iLine Then Return SetError(1, 0, 0)
Local $hFileOpen = FileOpen($sFilePath, FileGetEncoding($sFilePath) + $FO_OVERWRITE)
If $hFileOpen = -1 Then Return SetError(3, 0, 0)
Local $sData = ""
$iLine -= 1 ; Now the array is 0-based, so reduce the line number by 1.
For $i = 0 To $iUBound
If $i = $iLine Then
If $bOverWrite Then
If $sText Then $sData &= $sText & @CRLF
Else
$sData &= $sText & @CRLF & $aArray[$i] & @CRLF
EndIf
ElseIf $i < $iUBound Then
$sData &= $aArray[$i] & @CRLF
ElseIf $i = $iUBound Then
$sData &= $aArray[$i]
EndIf
Next
FileWrite($hFileOpen, $sData)
FileClose($hFileOpen)
Return 1
EndFunc ;==>_FileWriteToLine
页:
[1]