Jump to content

Filewriteline creating extra lines


Go to solution Solved by BrewManNH,

Recommended Posts

I'm trying to do a few queries and have them record the results into a CSV file but I'm running into an unusual error. When I had my code write a line at the end of all the queries, I got a ton of duplicated data, so I tried to tweak when it writes to the file to eliminate redundancies. I changed the code so now it doesn't do a @CRLF until the end of the set of queries and it looks perfect on the console output, but when I open the file itself, it has all sorts of spacing.

Here is the code:

$sqlRs2.open ($Query2, $sqlCon)
While Not $sqlRs2.EOF
    $Field1= $sqlRs2.Fields ('MPR').Value
    $Field2 = $sqlRs2.Fields ('ENT').Value
    $Field3 = $sqlRs2.Fields ('ROD').Value
    $Field4 = $sqlRs2.Fields ('ONTR').Value
    $Field5 = $sqlRs2.Fields ('OCAL').Value
    $EndDate = $sqlRs2.Fields ('End').Value
    $StartDate = $sqlRs2.Fields ('Start').Value
    ConsoleWrite($Field1 & "|" & $Field2 & "|" & $Field3 & "|" & $Field4 & "|" & $Field5 & "|" & DTFormat($StartDate) & "|" & DTFormat($EndDate) & "|") ; Write results to file
    FileWriteLine($fOutFile, $Field1 & "|" & $Field2 & "|" & $Field3 & "|" & $Field4 & "|" & $Field5 & "|" & DTFormat($StartDate) & "|" & DTFormat($EndDate) & "|") ; Write results to file
    $sqlRs6.open ($Query6, $sqlCon)
    While Not $sqlRs6.EOF
        $ID = $sqlRs6.Fields ('ERS').Value
        $Type = $sqlRs6.Fields ('ERS1').Value
        $DBRelationship = $sqlRs6.Fields ('ERT').Value
        DBRelationship()
        If $Type = 'F' Then
            $sqlRs7.open ($query7, $sqlCon)
            $Value1 = StringReplace($sqlRs7.Fields ('RIN' ).Value," ","")
            $Value2 = StringReplace($sqlRs7.Fields ('R70' ).Value," ","")
            $Value3 = StringReplace($sqlRs7.Fields ('OM' ).Value," ","")
            $Other = $Value & " " & $Value2 & " " & $Value3
            ConsoleWrite($Other & " " & $DBRelationship & "; ") ; Write results to file
            FileWriteLine($fOutFile, $Other & " " & $DBRelationship & "; ") ; Write results to file
            $sqlRs7.close
        EndIf
        $sqlRs6.MoveNext
    WEnd
    ConsoleWrite(@CRLF) ; Write results to file
    FileWriteLine($fOutFile, @CRLF)
    $sqlRs6.close
    $sqlRs2.MoveNext

The console out looks like this:

 

Example|Data|Is|Easy|Guys|1/1/2014|1/2/2014|This Should Work OWNER; So Should This NONOWNER; 

 

and in the file itself, this is what I get:

 

Example|Data|Is|Easy|Guys|1/1/2014|1/2/2014|

This Should Work OWNER; 

So Should This NONOWNER;

 

 

I tried opening it in notepad++ and I can confirm that there is a @CRLF at the end of each of the lines.

Does filewriteline only create a new line? How would I get my file to look like the console output?

Link to comment
Share on other sites

  • Solution

FileWriteLine automatically adds the @CRLF to every line you write with it. It's in the help file. If you want to combine the lines into one, you should only write it after you've combined the text and then write it once instead of every line.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

FileWriteLine automatically adds the @CRLF to every line you write with it. It's in the help file. If you want to combine the lines into one, you should only write it after you've combined the text and then write it once instead of every line.

I had actually realized that right as you posted (I came on to post a facepalm, but you had already gotten to it :P)

Thanks!

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...