Solved

VB Script help removing empty lines from .csv files

Posted on 2014-10-16
3
666 Views
Last Modified: 2014-10-17
Hello Everyone,
Trying to write a vb script to delete all blank lines from a collection of .csv files in a folder (e.g. a.csv, b.csv, c.csv, all containing a single letter a,b,c, respectively, and some with blank lines). Here is my code, it runs without error but is not deleting the lines like it should. Suggestions?

sFolder = "C:\Users\akhoshkar\Documents\MyJabberFiles"
Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each sFilename In objFSO.GetFolder(sFolder).Files
  If UCase(objFSO.GetExtensionName(sFilename.Name)) = "CSV" Then

	set objInFile = objFSO.OpenTextFile(sFilename, 1, true, 0)

	Do Until objInFile.AtEndOfStream

		strLine = objInFile.Readline
		strLine = Trim(strLine)
		If Len(strLine) > 0 Then
			strNewContents = strNewContents & strLine & vbCrLf
		End If

	Loop

	objInFile.Close

	set objOutFile = objFSO.OpenTextFile(sFilename, 2, true, 0)

	objOutFile.Write strNewContents
	objOutFile.Close

End If
Next

Open in new window


I have attached a sample environment for those who want to verify the code. 3 .csv files and the script inside a folder. Extract to C:\ for it to work properly (or just change the working directory inside the script). Thanks in advance everyone!
TEST.zip
0
Comment
Question by:Sir Learnalot
3 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 40385445
You could do this with the grep program super simple.

grep . {name of your file}

or send to a file like so

grep . {name of your file} > {name of your output file}


obviously, don't use { or }

You can find that here:
http://gnuwin32.sourceforge.net/packages/grep.htm
0
 
LVL 38

Accepted Solution

by:
Gerwin Jansen, EE MVE earned 500 total points
ID: 40385476
Try this:
sFolder = "C:\Temp\teest"
Set objFSO = CreateObject("Scripting.FileSystemObject")

For Each sFilename In objFSO.GetFolder(sFolder).Files
  If UCase(objFSO.GetExtensionName(sFilename.Name)) = "CSV" Then

	set objInFile = objFSO.OpenTextFile(sFilename, 1, true, 0)

        strNewContents = ""

	Do Until objInFile.AtEndOfStream

		strLine = objInFile.Readline
		strLine = Trim(strLine)
		If Len(strLine) > 0 Then
			strNewContents = strNewContents & strLine & vbCrLf
		End If

	Loop

	objInFile.Close

	set objOutFile = objFSO.OpenTextFile(sFilename, 2, true, 0)

	objOutFile.Write strNewContents
	objOutFile.Close

End If
Next

Open in new window

Note that your code always writes contents of a line + CrLf so last line will be an empty line.
0
 
LVL 6

Author Closing Comment

by:Sir Learnalot
ID: 40386627
Thank you very much this worked perfectly!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

839 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