Solved

Remove extra line in Text file using Autoit

Posted on 2014-04-08
5
1,513 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
Comment Utility
What command are you using the write the lines?
0
 

Author Comment

by:AccordJ35
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Function 2 worked for me. :-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 …
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now