Jump to content

Output begins with a newline - not wanted


 Share

Recommended Posts

Hi all,

I'm not a programmer and can't solve this one.

I have a script that runs through a folder, altering text files, but it is prepending the altered files with a blank line.

Can I stop that happening? I would actually prefer to remove all blank lines completely (may contain a few space chrs).

Could I attach here an example of an input file, and a desired output file?

Thanks

Ross

Link to comment
Share on other sites

#Include <File.au3>
Const $contrast = 'RasterStyle 2 60' & @CRLF
Const $bright   = 'RasterStyle 1 45' & @CRLF
Const $lastline = 'RasterStyle 7 0'

$FileList = _FileListToArray(@ScriptDir, '*.TAB', 1)
If @error = 1 Then
    MsgBox(0, "", "No TAB Files Found.")
    Exit
EndIf

For $i = 1 To $FileList[0]
    ProcessFile($FileList[$i])
Next
MsgBox(0, "", "Updated all TAB files."& @CRLF & "Contrast " & StringTrimLeft($contrast,14) & "Brightness "& StringTrimLeft($bright,14))

Func ProcessFile($filefromlist)
    $file = FileOpen($filefromlist,0)
    Dim $text
    While 1
        $line = FileReadLine($file)
            If @error = -1 Then ExitLoop
        If (StringInStr($line, "RasterStyle") = 0 Or $line <> @CRLF) Then
            $text = $text & @CRLF & $line
        EndIf
    Wend
    $text = $text & @CRLF & $bright & $contrast & $lastline
    $file = FileOpen($filefromlist,2)
    FileWrite($file, $text)
    FileClose($file)
    MsgBox(0, "", "Updated " & $i & " TAB files.",0)
EndFunc   ;==>ProcessFile

Example input file:

!table

!version 300

!charset WindowsLatin1

Definition Table

  File "bc35_5k_0810.ecw"

  Type "RASTER"

  (1849600,5848800) (0,0) Label "Pt 1",

  (1851999.6000000001,5845200.4000000004) (5999,8999) Label "Pt 2",

  (1851999.6000000001,5848800) (5999,0) Label "Pt 3",

  (1849600,5845200.4000000004) (0,8999) Label "Pt 4"

  CoordSys Earth Projection 8, 117, "m", 173, 0, 0.9996, 1600000, 10000000

  Units "m"

Example output file:

Identical to above, but blank lines removed and the following 3 lines added. (If there are already three lines beginning 'RasterStyle', the following should replace them.)

RasterStyle 1 45

RasterStyle 2 60

RasterStyle 7 0

Edited by rossnixon
Link to comment
Share on other sites

rossnixon,

You are declaring a var named $text then setting it with this line

 $text = $text & @CRLF & $line

$text is blank so you are setting $text to a blank line (@CRLF) followed by whatever is in $line.

You also open the same file twice without closing it.  You may be able to get away with that because you are using the same file handle but it is a poor practice.  I added a closefile before the second openfile. 

The following eliminates your blank line problem...

#Include <File.au3>
Const $contrast = 'RasterStyle 2 60' & @CRLF
Const $bright   = 'RasterStyle 1 45' & @CRLF
Const $lastline = 'RasterStyle 7 0'

$FileList = _FileListToArray(@ScriptDir, '*.TAB', 1)
If @error = 1 Then
    MsgBox(0, "", "No TAB Files Found.")
    Exit
EndIf

For $i = 1 To $FileList[0]
    ProcessFile($FileList[$i])
Next
MsgBox(0, "", "Updated all TAB files."& @CRLF & "Contrast " & StringTrimLeft($contrast,14) & "Brightness "& StringTrimLeft($bright,14))

Func ProcessFile($filefromlist)
    $file = FileOpen($filefromlist,0)
    Dim $text
    While 1
        $line = FileReadLine($file)
            If @error = -1 Then ExitLoop
        If (StringInStr($line, "RasterStyle") = 0 Or $line <> @CRLF) Then
            $text &= $line & @crlf
        EndIf
    Wend
    $text &= $bright & $contrast & $lastline
    fileclose($file)                            ; added
    $file = FileOpen($filefromlist,2)
    FileWrite($file, $text)
    FileClose($file)
    MsgBox(0, "", "Updated " & $i & " TAB files.",0)
EndFunc   ;==>ProcessFile

To replace existing raster statement we need to know more about the input, such as:

- Is there a one to one correspondence to the replacement?

- Do they always follow each other in the source?

- Are there alway 3 "raster" statements in the source (not 4 or 2)

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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