DoctorKennyG

Determine Newline From FileReadLine

5 posts in this topic

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)

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

 

Share this post


Link to post
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)

Share this post


Link to post
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)

 

Share this post


Link to post
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

  • Similar Content

    • MrSparty1995
      By MrSparty1995
      I have a good handle on how to read a file line by line, and search for a given string.
      Basically using, FileOpen, FileReadLine, and StringInStr.
      I have been trying to figure out why my search keeps coming back with no match.
      For example, if my string line was "Where is Waldy", and I was searching for Waldy that comes back fine.
      But in my file, Let's say it is
      Where is "Waldy"
      So i read that line in and do a search for Waldy, but i comes back with no match since there is quotes around it.
      Is there a way to resolve this?
      Here is my actual example
      The line comes back like this
          <JposEntry logicalName="LineDisp_iSC480">
      I'm searching for LineDisp_iSC480, but its coming back with no match due to the quotes.
      Thanks in advance, James
    • Duck
      By Duck
      I'm attempting to read each line of a word document and assign the line to a variable. Similarly to how you can read a line from a text file (.txt or .csv) using FileReadLine(). So far i have been unsuccessful in reading from a .doc/.docx file, nor have i found any documentation that has helped.

      In searching for a solution i did find a function to convert the word doc to a text file, however my script is for (PCI) auditing purposes and i do not want to create a new file on the HDD.  I have also read through the _Word UDF help files... Unless im not understanding the _Word UDF correctly, I did not see anything that functions similarly to the FileReadLine function.
      Any help/advice is greatly appreciated!  
       
      Here is what i have been attempting to do(doesn't work): 
       
      #include <file.au3> #include <Array.au3> #include <LuhnCheck.au3> #include <Excel.au3> #include <Word.au3> Global $sPath = 'C:\Users\' Global $filePath Global $pii = @ScriptDir & '\pii_CreditCard.csv' Global $filesArray = _FileListToArrayRec($sPath , '*.txt;*.csv;*.doc;*.docx;*.xls;*.xlsx',1,1,0,2) For $i = 1 to $filesArray[0] ;Loop through file extensions and add files to the fileArray ;Assign the position in the filesArray to filePath (filePath is set to full path in FileListToArrayRec) $filePath = $filesArray[$i] readFile($filePath) Next Func readFile($file) If StringInStr($file, '.txt') Or StringInStr($file, '.csv') Then ; .txt file readTxtFile($file) ElseIf StringInStr($file, '.doc') Then ; .doc & .docx files ;============================================== part that does not work========================= Local $oWord = _Word_Create() ;$openFile = FileOpen($file, 0); While 1 Local $line = FileReadLine(_Word_DocOpen($oWord, $file, Default, Default, True)) If @error = -1 Then ExitLoop ;lookForCreditCardNumbers($line) MsgBox(0,0, $line) WEnd FileClose($openFile) ;============================================== part that does not work========================== EndIf EndFunc Func readTxtFile($fileToOpen) $openFile = FileOpen($fileToOpen, 0); open file for reading and assing it to the openFile variable While 1 Local $line = FileReadLine($openFile) If @error = -1 Then ExitLoop lookForCreditCardNumbers($line) WEnd FileClose($openFile) EndFunc Func lookForCreditCardNumbers($evaluateString) $aResult = StringRegExp($evaluateString, '[4|5|3|6][0-9]{15}|[4|5|3|6][0-9]{3}[-| ][0-9]{4}[-| ][0-9]{4}[-| ][0-9]{4}', $STR_REGEXPARRAYMATCH) If Not @error Then Local $newString1 = StringReplace($aResult[0], ' ', '') ;remove spaces Local $newString2 = StringReplace($newString1, '-', '') ;remove dashes Local $bool = _LuhnValidate($newString2) ; Check possible CC number against the Luhn algorithm If $bool = 'True' Then Local $piiCSV = FileOpen($pii, 1) ;open text file for appending/writing, 1 FileWriteLine($piiCSV, $filePath & ', ' & $newString2) FileClose($piiCSV) EndIf EndIf EndFunc  
    • Reizvoller
      By Reizvoller
      Hello!
      When utilizing GUICtrlSetData to edit what is displayed in a GUICtrlCreateEdit field, is there a way to just make it write new lines rather than clearing what's there and writing the new information?
      For example ... 
      GUICtrlSetData ($editField, "Line one"&@CRLF , "") GUICtrlSetData ($editField, "Line two"&@CRLF , "") GUICtrlSetData ($editField, "Line three"&@CRLF , "") What happens now if those three things run one after another, the Edit field displays "Line One" , then that is replaced with "Line Two" and so on with the previous information disappearing and the subsequent taking its place. I'd like for it to say all three, one after another. Any documents on how I may accomplish this?
      Thanks!
      -Reiz
    • DoubleMcLovin
      By DoubleMcLovin
      I've been having trouble reading lines from my ini file - here is my code below. The code below successfully creates and appends a line to the file, it even successfully counts the lines - but when my For loop tries to read the lines it just prints blank lines in my output. Why?
       
      #include <StaticConstants.au3> #include <File.au3> Local $listFileLocation = @ScriptDir & "\thisFile.ini" ;OpenFile $listFile = FileOpen($listFileLocation, 9) LogCheck($listFile,-1,"Failed to open file :"&$listFileLocation) ;WriteLine $status = FileWriteLine($listFile,"This line!"&@CRLF) LogCheck($status,0,"Failed to write to $listfile:"&$listFile) ;CountLines Local $fileLines= _FileCountLines($listFileLocation) LogCheck(@error,1,"Can't count lines for:"&$listFile) LogCheck($fileLines,0,"No lines found for counting in:"&$listFile) Local $string ;Read Lines For $i=1 to $fileLines $string = FileReadLine($listFile,$i) LogCheck(@error,1,"Cannot read line #"&$i) ConsoleWrite($i&":" & $string & @CRLF) Next ;CloseFile $status = FileClose($listFile) LogCheck($status,0,"Failed to save $listfile::"&$listFile) ;Used for consoleWriting errors. Func LogCheck($sendCode,$passValue,$errorString) Local $negate, $exit $negate = StringRegExp($passValue,"^\!.*") ? 0:1 Switch $negate Case 0 ;Negated statement $passValue = StringTrimLeft($passValue,1) If NOT($sendCode=$passValue) Then $exit=1 EndIf Case 1;Positive statement If $sendCode=$passValue Then $exit=1 EndIf EndSwitch If $exit=1 Then ConsoleWrite($errorString&@CRLF) If NOT ($sendCode = "") Then ConsoleWrite(" Passed code: " & $sendCode & @CRLF) EndIf EndIf EndFunc  
    • LetsAuto
      By LetsAuto
      Hi everyone.. not quite sure how to get a list box to save its strings into a single .log or .txt file (either would work for me), and then later call each individual string..
      the list box has up to 30 items in it, the user will be able to deselect certain items, or select items... i just need a way to save each string into an external file so i can later call them back as variables and display them into a disabled list box.. if anyone could help that would be extremely appreciated. reply if my objective is confusing.
      THANKS!