Jump to content

Recommended Posts

Posted (edited)

Hallo forum members,
 
I'm looking for a function that will add @CRLF  EOL only if not exist @CRLF
 
test.txt
line
line
line
line < no @CRLF then @CRLF

this is no option FileWriteLine($file, @CRLF & GUICtrlRead($Input1))
if there is already CRLF EOL it will create a blank line
 
example scrip does work but makes a blank line in the begin:
 
test.txt
    < blank line
line
line
line
line
     <  @CRLF
 
 

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Const $filename = @ScriptDir & "\Test.txt" 

$Form1 = GUICreate("Test", 623, 298, 192, 124)
$Input1 = GUICtrlCreateInput("", 32, 40, 329, 21)
$Button1 = GUICtrlCreateButton("Ok", 392, 38, 65, 25)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _WriteText()
    EndSwitch
WEnd

Func _WriteText()
    Local $file, $line, $output
    $file = FileOpen($filename, 0)

    ; Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    ; Read in lines of text until the EOF is reached
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        If $line <> @CRLF And $line And $line <> @LF Then
            $output = $output & @CRLF & $line
        EndIf
    WEnd
    FileClose($file)

    $file = FileOpen($filename, 2); Write mode erase previous contents

    ; Check if file opened for writing OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    FileWriteLine($file, $output)
    FileClose($file)

    $file = FileOpen($filename, 1) ; Write mode (append to end of file)

    FileWriteLine($file, GUICtrlRead($Input1))
    FileClose($file)

EndFunc

Edit:

wrong comment

$file = FileOpen($filename, 1) ; Write mode erase previous contents  Write mode (append to end of file)

Thanks

Edited by Mecano
Posted

To enhance performance I would:

#include <Constants.au3>

Local Const $sFile = "test.txt"
Local $hFile = FileOpen($sFile, 0) ; Open the file to read
FileSetPos($hFile, -2, $FILE_END) ; Set file position 2 characters before the end of the file
Local $sLastChrs = FileRead($hFile) ; Read the last 2 characters of the file
If $sLastChrs <> @CRLF Then
    ; Add @CRLF to the end of the file
Endif

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Or v*$

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Yes it's working

Thank you both :thumbsup:

Now I gone test it, on big files

StringRegExpReplace is my new goal to learn, the patterns is for me :sweating:

water,  mikell  & guinness

Thanks again

Posted

I use this to convert EOL characters to CRLF.

#include <Constants.au3>

; Convert all line endings to @CRLF.
Local $sString = StringEOLToCRLF("This is a sentence " & @CR & Chr(12) & "with " & Chr(13) & "whitespace." & @CRLF)

; Display the converted line endings.
MsgBox($MB_SYSTEMMODAL, "", $sString)

; Visually display that @CR and @LF have converted to @CRLF.
$sString = StringReplace($sString, @CRLF, '@CRLF')
MsgBox($MB_SYSTEMMODAL, "", $sString)

Func StringEOLToCRLF($sString) ; Regular expression by Melba23 and modified By guinness.
    Return StringRegExpReplace($sString, '((?<!\r)\n|\r(?!\n))', @CRLF)
    ; Return StringRegExpReplace($sString, '\R', @CRLF) ; By Ascend4nt
EndFunc   ;==>StringEOLToCRLF

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

  • 3 years later...
Posted (edited)

Hi,

I resume this old thread because I'm not able to add @CRLF EOL if not exist

I tried the @water method

#include <Constants.au3>

Local Const $sFile = "C:\Users\user\Desktop\Testo.txt"
Local $hFile = FileOpen($sFile, 0) ; Open the file to read
FileSetPos($hFile, -2, $FILE_END) ; Set file position 2 characters before the end of the file
Local $sLastChrs = FileRead($hFile) ; Read the last 2 characters of the file
If $sLastChrs <> @CRLF Then
    ; Add @CRLF to the end of the file
Endif

and the @mikell one, but they don't work.

I attach a test text file

Testo.txtFetching info...

Edited by trof
Posted (edited)

The reason it doesn't work is because the file does not end with a line break character of any kind. The code above is intended to convert single line break characters to the combination CR and LF. This works fine with your example.

Local $hFile = FileOpen("C:\Users\user\Desktop\Testo.txt")
Local $sText = FileRead($hFile)
FileClose($hFile)

If StringRight($sText, 2) <> @CRLF Then
    $hFile = FileOpen("C:\Users\user\Desktop\Testo.txt", 1)
    FileWrite($hFile, @CRLF)
    FileClose($hFile)
EndIf

You may want to used both methods: converting single characters (as shown by guinness and mikell) and adding an extra CRLF at the end if need be.

Edited by czardas
Posted

You don't need to read the whole file in a string and you don't need to open the file two times (one for read, one for write).

This should be more efficient:

Global $s_FilePath = "C:\Test.txt"
Global $s_EndChars = @CRLF

Global $hFile = FileOpen($s_FilePath, 1)
FileSetPos($hFile, -StringLen($s_EndChars), 1)

If FileRead($hFile) <> $s_EndChars Then FileWrite($hFile, $s_EndChars)
FileClose($hFile)

 

Posted

I see. The bug in my code is that the file gets opened in read-mode only.

My UDFs and Tutorials:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

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