Solved

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

Posted on 2007-11-19
7
462 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

758 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

14 Experts available now in Live!

Get 1:1 Help Now