Solved

Remove extra line in Text file using Autoit

Posted on 2014-04-08
5
1,686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
constructor while deserilizing object 16 62
wordsFront challenge 8 121
Adjust the codes 3 63
Problem to macro 5 22
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 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