• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

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

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
manhoodo
Asked:
manhoodo
  • 4
  • 2
1 Solution
 
chandru_solCommented:
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
 
MorDrakkaCommented:
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
 
manhoodoAuthor Commented:
Can I run this code through VBA i.e. MS Excel??  Sorry I am a bit of a novice.

thanks
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
chandru_solCommented:
You have to save the file as .vbs extension

regards
Chandru
0
 
MorDrakkaCommented:
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
 
chandru_solCommented:
Did you try this?
0
 
chandru_solCommented:
Just curious why Grade B

regards
Chandru
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now