Jump to content

Determine Newline From FileReadLine


Recommended Posts

Is there a way to determine Newline (@CR, @LF, or @CRLF) from FileReadLine?  I have a script that reads a file via FileReadLine, and writes out another file via FileWriteLine.  I want to preserve the Newline character from the original file in the new file.

Opt("MustDeclareVars", 1)   ;0 = no, 1 = require pre-declare

#include <File.au3>
#include <Array.au3>

Local $gInPath = $CmdLine[1]
Local $NumberOfLines = $CmdLine[2]
Local $gInDrive, $gInDir, $gInFName, $gInExt, $gOutPath
Local $gMsgBoxTitle = "Error in " & @ScriptName
Local $InLine
Local $LineCount
Local $oFileIn
Local $oFileOut
Local $FileStringAppend

If FileExists($gInPath) Then
Else
    MsgBox(4096, $gMsgBoxTitle, "This file does not exist" & @CRLF & $gInPath)
    Exit
EndIf

_PathSplit($gInPath, $gInDrive, $gInDir, $gInFName, $gInExt)

If $NumberOfLines >= 1000000 Then
    $FileStringAppend = $NumberOfLines / 1000000 & "M"
ElseIf $NumberOfLines >= 1000 Then
    $FileStringAppend = $NumberOfLines / 1000 & "K"
Else
    $FileStringAppend = $NumberOfLines
EndIf

$gOutPath = _PathMake($gInDrive, $gInDir, $gInFName & "_" & $FileStringAppend, $gInExt)
If FileExists($gOutPath) Then
    MsgBox(4096, $gMsgBoxTitle, "File already exists" & @CRLF & $gOutPath)
    Exit
EndIf

$oFileIn = FileOpen($gInPath, 0)
$oFileOut = FileOpen($gOutPath, 1)

; Check if file opened for reading OK
If $oFileIn = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for read" & @CRLF & $gInPath)
    Exit
EndIf

; Check if file opened for writing OK
If $oFileOut = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for write." & @CRLF & $gOutPath)
    Exit
EndIf

; Read in lines of text until the EOF is reached
$LineCount = 0
While 1
    $InLine = FileReadLine($oFileIn)
    $LineCount += 1
    If @error = -1 Then ExitLoop
    If $LineCount > $NumberOfLines Then ExitLoop
    FileWriteLine($oFileOut, $InLine & @CRLF)
WEnd

FileClose($oFileIn)
FileClose($oFileOut)

 

Link to comment
Share on other sites

Is there a way to determine Newline (@CR, @LF, or @CRLF) from FileReadLine?  I have a script that reads a file via FileReadLine, and writes out another file via FileWriteLine.  I want to preserve the Newline character from the original file in the new file.

Opt("MustDeclareVars", 1)   ;0 = no, 1 = require pre-declare

#include <File.au3>
#include <Array.au3>

Local $gInPath = $CmdLine[1]
Local $NumberOfLines = $CmdLine[2]
Local $gInDrive, $gInDir, $gInFName, $gInExt, $gOutPath
Local $gMsgBoxTitle = "Error in " & @ScriptName
Local $InLine
Local $LineCount
Local $oFileIn
Local $oFileOut
Local $FileStringAppend

If FileExists($gInPath) Then
Else
    MsgBox(4096, $gMsgBoxTitle, "This file does not exist" & @CRLF & $gInPath)
    Exit
EndIf

_PathSplit($gInPath, $gInDrive, $gInDir, $gInFName, $gInExt)

If $NumberOfLines >= 1000000 Then
    $FileStringAppend = $NumberOfLines / 1000000 & "M"
ElseIf $NumberOfLines >= 1000 Then
    $FileStringAppend = $NumberOfLines / 1000 & "K"
Else
    $FileStringAppend = $NumberOfLines
EndIf

$gOutPath = _PathMake($gInDrive, $gInDir, $gInFName & "_" & $FileStringAppend, $gInExt)
If FileExists($gOutPath) Then
    MsgBox(4096, $gMsgBoxTitle, "File already exists" & @CRLF & $gOutPath)
    Exit
EndIf

$oFileIn = FileOpen($gInPath, 0)
$oFileOut = FileOpen($gOutPath, 1)

; Check if file opened for reading OK
If $oFileIn = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for read" & @CRLF & $gInPath)
    Exit
EndIf

; Check if file opened for writing OK
If $oFileOut = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for write." & @CRLF & $gOutPath)
    Exit
EndIf

; Read in lines of text until the EOF is reached
$LineCount = 0
While 1
    $InLine = FileReadLine($oFileIn)
    $LineCount += 1
    If @error = -1 Then ExitLoop
    If $LineCount > $NumberOfLines Then ExitLoop
    FileWriteLine($oFileOut, $InLine & @CRLF)
WEnd

FileClos

from the helpfile:

 

Remarks

Returns the text of the line read, any newline characters ( Chr(10) or @LF ) at the end of a line read in are automatically stripped.

 So, no.  You'll have to use fileread and break it up yourself.

Link to comment
Share on other sites

You can always use:

StringRegExp

before you read by line, by just reading the whole text using :

FileOpen
;and
FileRead

 and find which the file is using, then add that to each new line when using:

FileWriteLine

 

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

This will  copy the actual line termination of every line, whatever it uses (LF, CR, CRLF or none if last line) provided your input file has no more than 16777216 lines.

Opt("MustDeclareVars", 1)   ;0 = no, 1 = require pre-declare

#include <File.au3>
#include <Array.au3>

Local $gInPath = $CmdLine[1]
Local $NumberOfLines = $CmdLine[2]
Local $gInDrive, $gInDir, $gInFName, $gInExt, $gOutPath
Local $gMsgBoxTitle = "Error in " & @ScriptName
Local $hFileOut
Local $FileStringAppend

If FileExists($gInPath) Then
Else
    MsgBox(4096, $gMsgBoxTitle, "This file does not exist." & @CRLF & $gInPath)
    Exit
EndIf

_PathSplit($gInPath, $gInDrive, $gInDir, $gInFName, $gInExt)

If $NumberOfLines >= 1000000 Then
    $FileStringAppend = $NumberOfLines / 1000000 & "M"
ElseIf $NumberOfLines >= 1000 Then
    $FileStringAppend = $NumberOfLines / 1000 & "K"
Else
    $FileStringAppend = $NumberOfLines
EndIf

$gOutPath = _PathMake($gInDrive, $gInDir, $gInFName & "_" & $FileStringAppend, $gInExt)
If FileExists($gOutPath) Then
    MsgBox(4096, $gMsgBoxTitle, "File already exists." & @CRLF & $gOutPath)
    Exit
EndIf

$hFileOut = FileOpen($gOutPath, 1)

; Check if file opened for writing OK
If $hFileOut = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for write." & @CRLF & $gOutPath)
    Exit
EndIf

; Read in lines of text until the EOF is reached
Local $xLines = FileRead($gInPath)

; Check if file read OK
If @error Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to read file." & @CRLF & $gInPath)
    Exit
EndIf

; build array of lines
$xLines = StringRegExp($xLines, "(?m)^.*\R?", 3)

; adjust to max number of lines available, if necessary
$NumberOfLines = $NumberOfLines > UBound($xLines) ? UBound($xLines) : $NumberOfLines

; write lines back
For $i = 0 To $NumberOfLines - 1
    FileWrite($oFileOut, $xLines[$i] & @CRLF)
Next

FileClose($oFileOut)

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

After reading the comments, here's what I came up with. I invite any critiques or recommendations to improve or simplify.

 

Opt("MustDeclareVars", 1)   ;0 = no, 1 = require pre-declare

#include <File.au3>
#include <Array.au3>

Local $gInPath = $CmdLine[1]
Local $NumberOfLines = $CmdLine[2]
Local $gInDrive, $gInDir, $gInFName, $gInExt, $gOutPath
Local $gMsgBoxTitle = "Error in " & @ScriptName
Local $InLine
Local $LineCount
Local $oFileIn
Local $oFileOut
Local $FileStringAppend
Local Const $CHAR_READ_BLOCK = 100
Local $CharsRead = 0
Local $CrFound = 0
Local $LfFound = 0
Local $Newline
Local $InBlock

If FileExists($gInPath) Then
Else
    MsgBox(4096, $gMsgBoxTitle, "This file does not exist" & @CRLF & $gInPath)
    Exit
EndIf

_PathSplit($gInPath, $gInDrive, $gInDir, $gInFName, $gInExt)

If $NumberOfLines >= 1000000 Then
    $FileStringAppend = $NumberOfLines / 1000000 & "M"
ElseIf $NumberOfLines >= 1000 Then
    $FileStringAppend = $NumberOfLines / 1000 & "K"
Else
    $FileStringAppend = $NumberOfLines
EndIf

$gOutPath = _PathMake($gInDrive, $gInDir, $gInFName & "_" & $FileStringAppend, $gInExt)
If FileExists($gOutPath) Then
    MsgBox(4096, $gMsgBoxTitle, "File already exists" & @CRLF & $gOutPath)
    Exit
EndIf

$oFileIn = FileOpen($gInPath, 0)
$oFileOut = FileOpen($gOutPath, 1)

; Check if file opened for reading OK
If $oFileIn = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for read" & @CRLF & $gInPath)
    Exit
EndIf

; Check if file opened for writing OK
If $oFileOut = -1 Then
    MsgBox(4096, $gMsgBoxTitle, "Unable to open file for write." & @CRLF & $gOutPath)
    Exit
EndIf

While $CrFound = 0 And $LfFound = 0
    $CharsRead += $CHAR_READ_BLOCK
    $InBlock = FileRead($oFileIn, $CharsRead)
    If StringRight($InBlock, 1) = @CR Then
        $InBlock = $InBlock & FileRead($oFileIn, $CharsRead)
    EndIf

    $CrFound = StringInStr($InBlock, @CR)
    $LfFound = StringInStr($InBlock, @LF)

    If $CrFound > 0 And $LfFound > 0 Then
        $Newline = @CRLF
    ElseIf $CrFound > 0 Then
        $Newline = @CR
    Else
        $Newline = @LF
    EndIf
WEnd

; Read first line of text
$InLine = FileReadLine($oFileIn, 1)
$LineCount = 1
FileWriteLine($oFileOut, $InLine & $Newline)

; Read in lines of text until the EOF is reached
While 1
    $InLine = FileReadLine($oFileIn)
    $LineCount += 1
    If @error = -1 Then ExitLoop
    If $LineCount > $NumberOfLines Then ExitLoop
    FileWriteLine($oFileOut, $InLine & $Newline)
WEnd

FileClose($oFileIn)
FileClose($oFileOut)

 

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

×
×
  • Create New...