Solved

VB Script help removing empty lines from .csv files

Posted on 2014-10-16
3
711 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
[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
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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

729 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