Jump to content

Problems writing to files in Unix format


Recommended Posts

I am writing a script that silently installs Firefox (FF) and Thunderbird (TB) and also adds some settings to the localstore.rdf file that will get applied to new default profiles when created.

The code below works with the FF rdf file but not with the TB rdf, both files contain the same text and the only difference I can see is the FF rdf is in Windows format and the TB rdf is in Unix format. If I convert the file to Windows format the code works but that is the only way I can get it to work. My question is how can I do this with AutoIt?, also, does anybody know why the files are encoded differently?, most of the files, setting and setup are pretty similiar?.

I have tried creating a new file, writing the contents of the rdf into it and closing it but that does not work.

Thanks

Dim $localstore = @ProgramFilesDir & '\Mozilla Thunderbird\defaults\profile\localstore.rdf'

$lines = _FileCountLines($localstore)
$last_line = FileReadLine($localstore, $lines)
_FileWriteToLine($localstore, $lines, '  <RDF:Description RDF:about=' & '"' & 'chrome://messenger/content/messenger.xul#messengerWindow' & '"', 1)
;MsgBox(0, '', @error); Returns 1, File has less lines than $iLine
_FileWriteToLine($localstore, ($lines + 1), '                  sizemode=' & '"' & 'maximized' & '"' & ' />', 1)
_FileWriteToLine($localstore, ($lines + 2), '  <RDF:Description RDF:about=' & '"' & 'chrome://messenger/content/messenger.xul' & '"' & '>', 1)
_FileWriteToLine($localstore, ($lines + 3), '   <NC:persist RDF:resource=' & '"' & 'chrome://messenger/content/messenger.xul#messengerWindow' & '"' & '/>', 1)
_FileWriteToLine($localstore, ($lines + 4), '  </RDF:Description>', 1)
_FileWriteToLine($localstore, ($lines + 5), $last_line, 1)
Sleep(500)


Func _FileCountLines($sFilePath)
    Local $N = FileGetSize($sFilePath) - 1
    If @error Or $N = -1 Then Return 0
    Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1
EndFunc  ;==>_FileCountLines

Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0)
    If $iLine <= 0 Then
        SetError(4)
        Return 0
    EndIf
    If Not IsString($sText) Then
        SetError(6)
        Return 0
    EndIf
    If $fOverWrite <> 0 And $fOverWrite <> 1 Then
        SetError(5)
        Return 0
    EndIf
    If Not FileExists($sFile) Then
        SetError(2)
        Return 0
    EndIf
    Local $filtxt = FileRead($sFile, FileGetSize($sFile))
    $filtxt = StringSplit($filtxt, @CRLF, 1)
    If UBound($filtxt, 1) < $iLine Then
        SetError(1)
        Return 0
    EndIf
    Local $fil = FileOpen($sFile, 2)
    If $fil = -1 Then
        SetError(3)
        Return 0
    EndIf
    For $i = 1 To UBound($filtxt) - 1
        If $i = $iLine Then
            If $fOverWrite = 1 Then
                If $sText <> '' Then
                    FileWrite($fil, $sText & @CRLF)
                Else
                    FileWrite($fil, $sText)
                EndIf
            EndIf
            If $fOverWrite = 0 Then
                FileWrite($fil, $sText & @CRLF)
                FileWrite($fil, $filtxt[$i] & @CRLF)
            EndIf
        ElseIf $i < UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i] & @CRLF)
        ElseIf $i = UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i])
        EndIf
    Next
    FileClose($fil)
    Return 1
EndFunc  ;==>_FileWriteToLine
Link to comment
Share on other sites

Hi benners,

Use @LF for line ending rather then @CRLF for thunderbird. See how that goes for you. $line_ending is last parameter so use all parameters to use @LF in calling _FileWriteToLine().

Dim $localstore = @ProgramFilesDir & '\Mozilla Thunderbird\defaults\profile\localstore.rdf'

$lines = _FileCountLines($localstore)
$last_line = FileReadLine($localstore, $lines)
_FileWriteToLine($localstore, $lines, '  <RDF:Description RDF:about=' & '"' & 'chrome://messenger/content/messenger.xul#messengerWindow' & '"', 1)
;MsgBox(0, '', @error); Returns 1, File has less lines than $iLine
_FileWriteToLine($localstore, ($lines + 1), '                   sizemode=' & '"' & 'maximized' & '"' & ' />', 1)
_FileWriteToLine($localstore, ($lines + 2), '  <RDF:Description RDF:about=' & '"' & 'chrome://messenger/content/messenger.xul' & '"' & '>', 1)
_FileWriteToLine($localstore, ($lines + 3), '    <NC:persist RDF:resource=' & '"' & 'chrome://messenger/content/messenger.xul#messengerWindow' & '"' & '/>', 1)
_FileWriteToLine($localstore, ($lines + 4), '  </RDF:Description>', 1)
_FileWriteToLine($localstore, ($lines + 5), $last_line, 1)
Sleep(500)


Func _FileCountLines($sFilePath)
    Local $N = FileGetSize($sFilePath) - 1
    If @error Or $N = -1 Then Return 0
    Return StringLen(StringAddCR(FileRead($sFilePath, $N))) - $N + 1
EndFunc  ;==>_FileCountLines

Func _FileWriteToLine($sFile, $iLine, $sText, $fOverWrite = 0, $line_ending = @CRLF)
    ; Custom UDF with added $line_ending support
    If $iLine <= 0 Then
        SetError(4)
        Return 0
    EndIf
    If Not IsString($sText) Then
        SetError(6)
        Return 0
    EndIf
    If $fOverWrite <> 0 And $fOverWrite <> 1 Then
        SetError(5)
        Return 0
    EndIf
    If Not FileExists($sFile) Then
        SetError(2)
        Return 0
    EndIf
    Local $filtxt = FileRead($sFile, FileGetSize($sFile))
    $filtxt = StringSplit($filtxt, $line_ending, 1)
    If UBound($filtxt, 1) < $iLine Then
        SetError(1)
        Return 0
    EndIf
    Local $fil = FileOpen($sFile, 2)
    If $fil = -1 Then
        SetError(3)
        Return 0
    EndIf
    For $i = 1 To UBound($filtxt) - 1
        If $i = $iLine Then
            If $fOverWrite = 1 Then
                If $sText <> '' Then
                    FileWrite($fil, $sText & $line_ending)
                Else
                    FileWrite($fil, $sText)
                EndIf
            EndIf
            If $fOverWrite = 0 Then
                FileWrite($fil, $sText & $line_ending)
                FileWrite($fil, $filtxt[$i] & $line_ending)
            EndIf
        ElseIf $i < UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i] & $line_ending)
        ElseIf $i = UBound($filtxt, 1) - 1 Then
            FileWrite($fil, $filtxt[$i])
        EndIf
    Next
    FileClose($fil)
    Return 1
EndFunc  ;==>_FileWriteToLine

:)

Edit:

I only updated _FileCountLines(), so you need to still fix the function calls to suit.

Edited by MHz
Link to comment
Share on other sites

Thanks MHz, it works a treat, on both rdf files, wish I had posted 1 day ago :">

I assume the additions are needed due to the different formats?, does the Unix format not recognize Chr(13)?. Is it just my situation or would the _FileWriteToLine function benefit from your additional code?.

Thanks again

Link to comment
Share on other sites

Chr(13) is @CR which MacIntosh use AFAIK. *unix uses @LF and Windows uses @CRLF. Mixing up the line endings will make the file perhaps fail with a program that reads it and expect certain line endings from start to finish. A program may read any line ending if it checks for them but changing line endings half way through a file will probably in in disaster.

IIRC, 1 or more of the STD UDFs had changes done for a line ending option I thought. An addition would make sense if others agreed also.

:)

Edited by MHz
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...