Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Remove extra line in Text file using Autoit

Posted on 2014-04-08
5
Medium Priority
?
1,916 Views
Last Modified: 2014-04-09
I using the Autoit program to create my own program. I have been trying to figure out how how to remove a blank line in a text file. We have a program that outputs a text file. The problem is that it adds a blank line at the end of the file.

when I use the command _FileCountLines
it comes back and says there is 10 lines but when you open the text file you can click on the 11th line. The problem with that is that the company we are uploading this file to says we have to remove that blank line.
0
Comment
Question by:AccordJ35
  • 2
  • 2
5 Comments
 
LVL 9

Expert Comment

by:dustock
ID: 39986329
What command are you using the write the lines?
0
 

Author Comment

by:AccordJ35
ID: 39986410
The program uses a sql store procedure to create the .txt file. It just has a blank line at the end. Trying to figure out how to get rid of it.
0
 
LVL 9

Expert Comment

by:dustock
ID: 39987216
It's been awhile since I last used AutoIT, but I think you should try StringReplace to remove the last line.

Something like:

yourstring = StringReplace(yourstring, @CRLF, '', -1)

If I remember correctly the -1 will remove the last occurrence of what ver you are trying to replace.  You may also want to search for just @CR or @LF if @CRLF doesn't work.  Also you may have to mix functions like StringReplace(StringRight(yourstring, 1), @CRLF, '') but I can't be sure since I am not near a PC to try.
0
 
LVL 29

Accepted Solution

by:
matrixnz earned 2000 total points
ID: 39988344
Here are two examples:
1. Using an Array
2. Using StringStripWs

Cheers

#include <File.au3>

Global $hFILENAME = 'Original.txt'
Global $hFILETEMP = 'Updated.txt'

FNC_EXAMPLE1()

FNC_EXAMPLE2()

Func FNC_EXAMPLE1()
Local $aFILENAME, $hFILEOPEN, $x
	 _FileReadToArray($hFILENAME, $aFILENAME)
		If @error Then
			MsgBox(64, 'Error', 'Error reading: ' & $hFILENAME)
			Exit
		EndIf
	Global $hFILEOPEN = FileOpen($hFILETEMP, 2)
		If $hFILEOPEN = -1 Then
			MsgBox(64, 'Error', 'Error creating: ' & $hFILETEMP)
			Exit
		EndIf

	For $x = 1 To $aFILENAME[0]
		If $x = $aFILENAME[0] Then
			FileWrite($hFILEOPEN, $aFILENAME[$x])
		Else
			FileWrite($hFILEOPEN, $aFILENAME[$x] & @CRLF)
		EndIf
	Next
	FileClose($hFILEOPEN)
EndFunc

Func FNC_EXAMPLE2()
	Local $vFILENAME, $hFILEOPEN
	$vFILENAME = FileRead($hFILENAME)
		If @error Then
			MsgBox(64, 'Error', 'Error reading: ' & $hFILENAME)
			Exit
		EndIf
	$hFILEOPEN = FileOpen($hFILETEMP, 2)
		If $hFILEOPEN = -1 Then
			MsgBox(64, 'Error', 'Error creating: ' & $hFILETEMP)
			Exit
		EndIf
	$vFILENAME = StringStripWS($vFILENAME, 2)
	FileWrite($hFILEOPEN, $vFILENAME)
EndFunc

Open in new window

0
 

Author Closing Comment

by:AccordJ35
ID: 39988686
Thanks Function 2 worked for me. :-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question