rossnixon Posted December 12, 2013 Share Posted December 12, 2013 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 More sharing options...
Danp2 Posted December 12, 2013 Share Posted December 12, 2013 You should also post your script (use the code button in the toolbar so that the text is formatted properly). Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
rossnixon Posted December 12, 2013 Author Share Posted December 12, 2013 (edited) #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 December 12, 2013 by rossnixon Link to comment Share on other sites More sharing options...
Danp2 Posted December 12, 2013 Share Posted December 12, 2013 1) You should check the variable text to see if it is empty before appending the @CRLF in your While loop. 2) Have you considered reading the entire file into an array, manipulating the array, and then writing the contents back to the file? Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
rossnixon Posted December 12, 2013 Author Share Posted December 12, 2013 Danp2, thanks for the hint. I got it working with some "If Then ElseIf" stuff. Didn't really want to throw it away and do the manipulating an array thing. Link to comment Share on other sites More sharing options...
kylomas Posted December 12, 2013 Share Posted December 12, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now