Zeichencode nicht korrekt

  • Hallo zusammen,

    ich habe eine Datei oder Windows (*.sh File). Diese Datei verändere ich mittels


    _ReplaceStringInFile($olPRGE & "\createdb.sh", "!!PFILE!!", $olPFILEE)


    Wenn ich diese Datei nun auf Linux kopiere kann ich sie dort nicht mehr ausführen, da bestimmte Zeichen enthalten sind die Linux nicht interpretieren kann.

    Ich vermute ich brauche die Datei im UTF-8 Format, durch das Verändern unter Windows wird dies aber umgestellt.

    Hat jemand eine Ahnung wie ich die Datei bearbeiten kann unter Windows und trotzdem noch unter Linux ausführen ?


    Gibt es eine Art Convert oder sowas ?


    Danke für Eure Hilfe, bin leider hier am Suchen, aber leider funzt nichts.

  • Hi Mega,


    versteh ich nicht ganz.

    Ich mache auf das File kein FileOpen, sondern gebe als Parameter den Filenamen bei _ReplaceStringInFile an.


    Oder versteh ich etwas falsch ?


    Danke für deine Hilfe

  • Jo, aber die öffnet nicht in UTF8. Hier eine angepasste, die Auch UTF8 kann:

    Spoiler anzeigen
    [autoit]

    ; ===================================================================
    ; Author: Kurt (aka /dev/null) and JdeB
    ;
    ; _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString,$bCaseness = 0, $bOccurance = 0, $UTF8 = 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)
    ; $UTF8 shall we open in UTF8-mode or not?
    ; 0 = ANSI (default), 1 = UTF8
    ;
    ; 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
    ; @error=6 File has read-only attribute
    ;
    ; ===================================================================
    Func _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString, $fCaseness = 0, $fOccurance = 1, $UTF8)

    [/autoit] [autoit][/autoit] [autoit]

    Local $iRetVal = 0
    Local $hWriteHandle, $aFileLines, $nCount, $sEndsWith, $hFile
    ; Check if file is readonly ..
    If StringInstr(FileGetAttrib($szFileName),"R") then
    SetError(6)
    Return -1
    EndIf
    ;===============================================================================
    ;== Read the file into an array
    ;===============================================================================
    If $UTF8 = 1 Then
    $hFile = FileOpen($szFileName, 128)
    Else
    $hFile = FileOpen($szFileName, 0)
    EndIf
    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 output file in write mode
    ;===============================================================================
    If $UTF8 = 1 Then
    $hWriteHandle = FileOpen($szFileName, 130)
    Else
    $hWriteHandle = FileOpen($szFileName, 2)
    EndIf
    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

    [/autoit] [autoit][/autoit] [autoit]

    ;======================================================================
    ;== 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 back to original 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 with the same as the input file
    If $aFileLines[$nCount] <> "" Then FileWrite($hWriteHandle, $aFileLines[$nCount] & $sEndsWith)
    FileClose($hWriteHandle)

    [/autoit] [autoit][/autoit] [autoit]

    Return $iRetVal
    EndFunc ;==>_ReplaceStringInFile

    [/autoit]
  • Noch eine Frage:

    Ich habe die Funktion in die File.au3 kopiert bzw. ersetzt.

    Aufrufen tue ich das ganze nun wie folgt:

    $oltnsnamesora1 = _ReplaceStringInFile($olPRGE & "\tnsnames.ora", "!!SID!!", $olDB_NAMEE, 0, 1, 1)

    Aber er ersetzt mir nun nichts mehr in meinem File. Es bleibt immer noch !!SID!! stehen.

    Vielleicht noch einen kleinen Tip.


    Danke für Eure Hilfe.

  • Dann mach beim lesen das Unicode wieder aus und schau, ob es dann geht. Dafür diese Zeilen:

    [autoit]

    If $UTF8 = 1 Then
    $hFile = FileOpen($szFileName, 128)
    Else
    $hFile = FileOpen($szFileName, 0)
    EndIf

    [/autoit]


    ersetzen mit

    [autoit]


    $hFile = FileOpen($szFileName, 0)

    [/autoit]
  • Wenn ich es rausnehme das klappt das ganze, auch wenn ich UTF auf Wert 0 setze.

    Das Problem liegt beim FileOpen mit Modus 128, das klappt nicht.

    Das habe ich auch einmal separat in einem Script ausprobiert.

    Wo finde ich denn in der Hilfe welcher Modus für UTF 8 eingestellt werden muss ?

    In der Hilfe finde ich nur die Werte 0,1, 8 etc. aber nicht mehr.


    Vielleicht kann mir jemand noch einen Tipp geben. Mein Tool funktioniert fast perfekt, bis eben auf diese kleine Sache.


    Danke Euch und schonmal vorab ein frohes Fest.

  • Es ist was anderes, diese Zeilen zu ersetzen oder UTF8 auf 0 zu stellen.
    Die Datei wird erst zum lesen geöffnet. Diese Stelle muss korrigiert werden, da beim Lesen UTF8 nicht verwendet werden darf.
    Dann erst wird sie zum Scheiben geöffnet. Hier muss mit UTF8 neu geschrieben werden.

    Korrigiert sollte gehen
    [autoit]

    ; ===================================================================
    ; Author: Kurt (aka /dev/null) and JdeB
    ;
    ; _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString,$bCaseness = 0, $bOccurance = 1, $UTF8 = 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)
    ; $UTF8 shall we open in UTF8-mode or not?
    ; 0 = ANSI (default), 1 = UTF8
    ;
    ; 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
    ; @error=6 File has read-only attribute
    ;
    ; ===================================================================
    Func _ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString, $fCaseness = 0, $fOccurance = 1, $UTF8 = 0)

    [/autoit] [autoit][/autoit] [autoit]

    Local $iRetVal = 0
    Local $hWriteHandle, $aFileLines, $nCount, $sEndsWith, $hFile
    ; Check if file is readonly ..
    If StringInstr(FileGetAttrib($szFileName),"R") then
    SetError(6)
    Return -1
    EndIf
    ;===============================================================================
    ;== 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 output file in write mode
    ;===============================================================================
    If $UTF8 = 1 Then ; WRITE FILE IN UTF8 if wanted.
    $hWriteHandle = FileOpen($szFileName, 130)
    Else
    $hWriteHandle = FileOpen($szFileName, 2)
    EndIf
    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

    [/autoit] [autoit][/autoit] [autoit]

    ;======================================================================
    ;== 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 back to original 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 with the same as the input file
    If $aFileLines[$nCount] <> "" Then FileWrite($hWriteHandle, $aFileLines[$nCount] & $sEndsWith)
    FileClose($hWriteHandle)

    [/autoit] [autoit][/autoit] [autoit]

    Return $iRetVal
    EndFunc ;==>_ReplaceStringInFile

    [/autoit]