Hi
Hat jemadn von euch ein Script (Funktion) mit dem man in einer beliebigen Datei einen String SUCHEN und Ersetzen kann ?
Hi
Hat jemadn von euch ein Script (Funktion) mit dem man in einer beliebigen Datei einen String SUCHEN und Ersetzen kann ?
guck mal in der beta:
; ===================================================================
; Author: Kurt (aka /dev/null) and JdeB
;
; _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString,$bCaseness = 0, $bOccurance = 0)
;
; Replaces a string ($szSearchString) with another string ($szReplaceString) the given TEXT file
; (via filename)
;
; Operation:
; The funnction opens the original file for reading and a temp file for writing. Then
; it reads in all lines and searches for the string. If it was found, the original line will be
; modified and written to the temp file. If the string was not found, the original line will be
; written to the temp file. At the end the original file will be deleted and the temp file will
; be renamed.
;
; Parameters:
; $szFileName name of the file to open.
; ATTENTION !! Needs the FULL path, not just the name returned by eg. FileFindNextFile
; $szSearchString The string we want to replace in the file
; $szReplaceString The string we want as a replacement for $szSearchString
; $fCaseness shall case matter?
; 0 = NO, case doe not matter (default), 1 = YES, case does matter
; $fOccurance shall we replace the string in every line or just the first occurance
; 0 = first only, 1 = ALL strings (default)
;
; Return Value(s):
; On Success Returns the number of occurences of $szSearchString we found
;
; On Failure Returns -1 sets @error
; @error=1 Cannot open file
; @error=2 Cannot open temp file
; @error=3 Cannot write to temp file
; @error=4 Cannot delete original file
; @error=5 Cannot rename/move temp file
;
; ===================================================================
Func _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString, $fCaseness = 0, $fOccurance = 1)
Local $iRetVal = 0
Local $szTempFile, $hWriteHandle, $aFileLines, $nCount, $sEndsWith, $hFile
;===============================================================================
;== Read the file into an array
;===============================================================================
$hFile = FileOpen($szFileName, 0)
If $hFile = -1 Then
SetError(1)
Return -1
EndIf
Local $s_TotFile = FileRead($hFile, FileGetSize($szFileName))
If StringRight($s_TotFile, 2) = @CRLF Then
$sEndsWith = @CRLF
ElseIf StringRight($s_TotFile, 1) = @CR Then
$sEndsWith = @CR
ElseIf StringRight($s_TotFile, 1) = @LF Then
$sEndsWith = @LF
Else
$sEndsWith = ""
EndIf
$aFileLines = StringSplit(StringStripCR($s_TotFile), @LF)
FileClose($hFile)
;===============================================================================
;== Open the temporary file in write mode
;===============================================================================
$szTempFile = _TempFile()
$hWriteHandle = FileOpen($szTempFile, 2)
If $hWriteHandle = -1 Then
SetError(2)
Return -1
EndIf
;===============================================================================
;== Loop through the array and search for $szSearchString
;===============================================================================
For $nCount = 1 To $aFileLines[0]
If StringInStr($aFileLines[$nCount], $szSearchString, $fCaseness) Then
$aFileLines[$nCount] = StringReplace($aFileLines[$nCount], $szSearchString, $szReplaceString, 1 - $fOccurance, $fCaseness)
$iRetVal = $iRetVal + 1
;======================================================================
;== If we want just the first string replaced, copy the rest of the lines
;== and stop
;======================================================================
If $fOccurance = 0 Then
$iRetVal = 1
ExitLoop
EndIf
EndIf
Next
;===============================================================================
;== Write the lines to a temp file.
;===============================================================================
For $nCount = 1 To $aFileLines[0] - 1
If FileWriteLine($hWriteHandle, $aFileLines[$nCount]) = 0 Then
SetError(3)
FileClose($hWriteHandle)
Return -1
EndIf
Next
; Write the last record and ensure it ends wit hthe same as the input file
If $aFileLines[$nCount] <> "" Then FileWrite($hWriteHandle, $aFileLines[$nCount] & $sEndsWith)
FileClose($hWriteHandle)
;======================================================================
;== Delete the original file
;======================================================================
If FileDelete($szFileName) = 0 Then
SetError(4)
Return -1
EndIf
;======================================================================
;== Rename the temp file to the original file
;======================================================================
If FileMove($szTempFile, $szFileName) = 0 Then
SetError(5)
Return -1
EndIf
Return $iRetVal
EndFunc ;==>_ReplaceStringInFile
Alles anzeigen