Solved

Remove extra line in Text file using Autoit

Posted on 2014-04-08
5
1,644 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 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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
maxMirror challenge 10 188
Python Encoding Problem \u2013 4 134
mapBully challenge 6 171
Not seen Link button 5 71
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

680 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