Solved

I would like to delete empty lines in text files........

Posted on 2007-11-19
7
465 Views
Last Modified: 2012-08-13
I am looking for a solution to formatting loads of .txt files in a batch conversion type way.  I want to delete any empty lines from within the files.  They are all .TXT files.

Please help.
0
Comment
Question by:manhoodo
  • 4
  • 2
7 Comments
 
LVL 12

Accepted Solution

by:
chandru_sol earned 500 total points
ID: 20312872
Hi ..

Try this..........

This is for only one files, we can modify all file in a folder

regards
Chandru
Const ForReading = 1

Const ForWriting = 2
 

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)
 

Do Until objFile.AtEndOfStream

    strLine = objFile.Readline

    strLine = Trim(strLine)

    If Len(strLine) > 0 Then

        strNewContents = strNewContents & strLine & vbCrLf

    End If

Loop
 

objFile.Close
 

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)

objFile.Write strNewContents

objFile.Close

Open in new window

0
 
LVL 6

Expert Comment

by:MorDrakka
ID: 20312890
Hi

There are two ways you can do this. (Vbscript wise)

One is through the use of a regular expression, which I am not very knoweledgeable off.

Second is attached below just cycly through the tekst file and when you see an empty line do not write it to the output.

Hope this helps

M
SET objInputfile = objFSO.OpenTextFile("TEKSTFILE.txt")

SET objOutputfile = objFSO.CreateTextFile("OUTPUTTEKSTFILE", True)
 

DO WHILE NOT objInputfile.AtEndOfStream

strContent = objInputfile.readline
 

If strContent = "" then

wscript.echo "omitting line"

Else

objOutputfile.writeline strContent

End if

Loop

Open in new window

0
 

Author Comment

by:manhoodo
ID: 20313048
Can I run this code through VBA i.e. MS Excel??  Sorry I am a bit of a novice.

thanks
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 12

Expert Comment

by:chandru_sol
ID: 20313068
You have to save the file as .vbs extension

regards
Chandru
0
 
LVL 6

Expert Comment

by:MorDrakka
ID: 20313078
Hi,

The way you run this code is as follows:

Copy the code into a empty tekst file and name it Test.vbs.
Open a Command prompt
Go to folder where Test.vbs is and run:  cscript test.vbs

Do not forget to put the script in the folder with the textfiles, and to enter a tekstfile name in my script that must be modified.

Regards,

M
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20387457
Did you try this?
0
 
LVL 12

Expert Comment

by:chandru_sol
ID: 20411303
Just curious why Grade B

regards
Chandru
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
This video teaches the viewer how to align pictures around text while keeping the text properly aligned in the document.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

919 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

16 Experts available now in Live!

Get 1:1 Help Now