Solved

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

Posted on 2007-11-19
7
469 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

825 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