[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2007-11-19
7
Medium Priority
?
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 12

Accepted Solution

by:
chandru_sol earned 1500 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

650 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