Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you remove blank lines from a text file using VB and/or macros? 1

Status
Not open for further replies.

MPBCHM

IS-IT--Management
Feb 6, 2002
20
0
0
US
I have a ReportWriter program that exports data into a "fixed-width" text file.

During the export, the ReportWriter inserts a blank line at every page break.
"This is a bug in the program."

"I need to remove these blank lines."

I have been using a blankline remover (generic) program but I have to use it manually by openning each text file one by one. I have about 100 files to do this to...

I need to automate this process somehow.

Please Help.
 
Your "soloution" appears to be just adding CFUD (Confusion, Fear, Uncertanity and Doubt) to the overall process.

Why don't you FIX the "BUG"? And get on w/ life?

On the side of repairing the damage already created by letting "Bugs" go by, you can always get the list of list of the existing files w/ the problem and then call your blank line removal process for each. IF the set of problem childern (Bug as the Father - YOU as the mother?) is 'regular', you can assemble the list wit the Dir function.

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Sub clean_blank_lines()
Dim x As String

Close

'substitute your path and file names in next 2 lines

Open ("c:\text1.txt") For Input As #1
Open ("c:\text2.txt") For Output As #2


Do While Not EOF(1)
Line Input #1, x
If x <> &quot;&quot; Then Print #2, x
Loop

Close
End Sub

end sub
 
Hey ETID,

Thanks for the help. Your code worked out great. But...
I need to do this for about 100 text files. Is there an easier way to process this many files without having to write out the code for every file in my list. i.e.

Open (&quot;C:\MyFilePath\MyTextFile.txt&quot;) For Input As #1
Open (&quot;C:\MyFilePath\MyTextFile.txt&quot;) For Output As #2

Do While Not EOF(1)
Line Input #1, x
If x <> &quot;&quot; Then Print #2, x
Loop

Close

Open (&quot;C:\MyFilePath\MyTextFile2.txt&quot;) For Input As #1
Open (&quot;C:\MyFilePath\MyTextFile2.txt&quot;) For Output As #2

Do While Not EOF(1)
Line Input #1, x
If x <> &quot;&quot; Then Print #2, x
Loop

Close

End Sub

So on and on.....

Thanks for the help you've given me so far. Even if I have to type out the code for every file, it's still a big time saver...

Thanks again ETID.

Mark
 
Mark,
there is a facility in Access to search for Files in a Folder. This snippet of code could help with your task.


With Application.FileSearch
.LookIn = FileLoc
.SearchSubFolders = False
.FileName = &quot;*.txt&quot;
If .Execute() > 0 Then
J = .FoundFiles.Count
For I = 1 To J
ReDim Preserve Fnames(I)
Fnames(I) = .FoundFiles(I)
Next I
Else
MsgBox &quot;There were no files found.&quot;
End If
End With

The File Names are stored in an array, once there you can loop round your code selecting the files from the array. This would be best if all your files are in the same folder.

Good Luck. Sandy
 
Hi Sandy,
I got an error when I tried to run the code below...


Option Compare Database
Option Explicit

Private Sub Command0_Click()
With Application.FileSearch
.LookIn = &quot;C:\RptDbase\Encdata\Outfile\&quot;
.SearchSubFolders = False
.FileName = &quot;*.txt&quot;
If .Execute() > 0 Then
J = .FoundFiles.Count
For I = 1 To J
ReDim Preserve Fnames(I)
Fnames(I) = .FoundFiles(I)
Next I
Else
MsgBox &quot;There were no files found.&quot;
End If
End With

End Sub


The error is Compile Error:
Variable not defined

The debugger high-lights the J = part of the code.

Any suggestions???

Thanks for your help.

Mark
 
Access inserts Option Explicit into modules by default (unless you change your preferences, which most people don't).

This will be at the very top of the code module in the General area.

This means all variables must be explicitly delcared.

Add Dim J as Integer to the beginning of your procedure (After the &quot;Private Sub . . .&quot; line and before the &quot;With . . .&quot; line) and that should clear up the problem. _________
Rott Paws
 
Thanks for all the help you guys gave me. I meant to close this thread out weeks ago.

The code to remove blank lines from text files worked out great. Thanks again.


Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top