Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB Script help removing empty lines from .csv files

Posted on 2014-10-16
3
Medium Priority
?
802 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

926 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