VBS-How to remove blank/null lines from a text file

I create a text file which has blank/null lines in it.  I would like to be able to remove these lines from the file with a VB script. A sample of text file is attached.
 Please advise,
Thank you
Text-File-sample.doc
mbroad02Asked:
Who is Participating?
 
Bill PrewConnect With a Mentor Commented:
Okay, here's a quick VBS script that should work.  Save as a VBS and then run passing it either one filename or two as command line parms.  If one filename passed, it will overwrite that file with the changes.  If two files passed, it will save the changed version to the second file (helpful for testing).

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

' Remove unwanted blank lines
Do While Instr(sData, vbCrLf & vbCrLf) > 0
  sData = Replace(sData, vbCrLf & vbCrLf, vbCrLf)
Loop 
Do While Instr(sData, vbCrLf & """""" & vbCrLf) > 0
  sData = Replace(sData, vbCrLf & """""" & vbCrLf, vbCrLf)
Loop 

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
Wscript.Quit

Open in new window

~bp
0
 
mbroad02Author Commented:
While I appreciate your link, I need a little help in creating this script.
I copied the script (below) from the link and inserted my input and output file names (in bold).  I am getting an error
in running:  "File not found.  LIne 5".  That would be the objfile.  The file in fact DOES exist, in the same directory as the script and is spelled exactly as it is in this script (see attached screen print of file name).
Your help is appreciated.


Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("FTR_picklist_viviano.ttx", 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("FTR_picklist_viviano2.ttx", ForWriting)
objFile.Write strNewContents
objFile.Close

directory.doc
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Alan_WhiteCommented:
Can you try putting the full path in?
0
 
mbroad02Author Commented:
I did this and it got rid of the error.  Thank you.
However the output file (for writing) is getting a NOT FOUND error.  

Doesn't the script CREATE the output file if it does not exist (which it does not)...?

If I put a filename in the output file (For Writing) which does not exist, it gets an error.  If I put the filename of input file in the output file, it does not change anything in the file (like removing the blank lines).

Your help is appreciated.
0
 
Bill PrewCommented:
Your sample attachment is NOT a text file, did you post the correct file?

~bp
0
 
Alan_WhiteConnect With a Mentor Commented:
Change the line that was

Set objFile = objFSO.OpenTextFile("FTR_picklist_viviano2.ttx", ForWriting)

to

Set objFile = objFSO.OpenTextFile("FTR_picklist_viviano2.ttx", ForWriting, True)

obvouisly replacing the filename with your full path again.

The extra "True" tells it to create the file if it doesn't exist.

0
 
mbroad02Author Commented:
We are getting there.  The script change did create the output file as I desire.

However, the script is NOT removing the blank lines as it is supposed to do.

See attached.  This is the output file AFTER the script runs.  As you can see, the blank lines are not removed.

Your help is appreciated.
text-file-with-blank-lines.doc
0
 
Alan_WhiteCommented:
As billprew says, your text file is not a text file.  It's actually a word document.

Are you just pasting the contents of your text file into Word before posting here or are you actually trying to remove the lines from a Word doc?
0
 
mbroad02Author Commented:
These are print-screen samples of the output text file.  It is a TTX file which I cannot post here due to
confidentiality.  Thank you.
0
 
Bill PrewCommented:
Of course, for what it's worth, you could do this very simply at a command prompt via:

for /F %A in (in_file.txt) do @echo %A >>out)file.txt

Open in new window

that will remove any blank lines.

~bp
0
 
Bill PrewCommented:
In the DOC file you provided, it looks like the TXT file actually doesn't have "blank" lines, but rather lines with just """" on them, is that true?  

~bp
0
 
mbroad02Author Commented:
To answer your question above, the file has blank or null lines, whichever....in any case, it has extra "blank" lines between each data line.

I appreciate the suggested script above.  I attempted to use it with unsuccessful results.  See attached word doc.
The output file does not look any different.   Am I doing something wrong??
Thank you
Problems-with-VBS-run.doc
0
 
Bill PrewCommented:
It could be that your lines are not terminated with the normal carriage return, line feed pair.  Can you do a hex display of the lines and post  a bit (since you can't share the file).

~bp
0
 
Bill PrewCommented:
I'm assuming no errors were generated from the VBS script execution?

~bp
0
 
mbroad02Author Commented:
I did not receive any errors during VBS execution.

Attached is a partial hex dump of out put file.

Thanks
hex-dump.doc
0
 
Bill PrewCommented:
Hmm, I just tried a test here with a file that looked similar to yours and with the exception of the very first line it worked okay.  I'll keep looking...

~bp
0
 
Bill PrewCommented:
I don't see why it wouldn't work on your file, can you try it there on this file and see what happens, this file worked for me.

~bp s.txt
0
 
mbroad02Author Commented:
Here is a display of hex dump.  I will test your file and see what happens.
Thanks
hex-dump.doc
0
 
mbroad02Author Commented:
Ok here is the status:

(1)  I tried it with your test file and it worked great.  

(2)  I then tried it on my file and it worked.  I don't know why it wasn't working for me yesterday.  :)

This works great but I wonder if this can be tweaked to ALSO remove the blank/null first line (even if a separate script)??

Thank you
0
 
Bill PrewCommented:
See if this takes care of that for you.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Get input file name from command line parm, if 2 parms entered
' use second as new output file, else rewrite to input file
If (WScript.Arguments.Count > 0) Then
  sInfile = WScript.Arguments(0)
Else
  WScript.Echo "No filename specified."
  WScript.Quit
End If
If (WScript.Arguments.Count > 1) Then
  sOutfile = WScript.Arguments(1)
Else
  sOutfile = sInfile
End If

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Read entire input file into a variable and close it
Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
sData = oInfile.ReadAll
oInfile.Close
Set oInfile = Nothing

' Remove unwanted control characters
Do While Instr(sData, """""" & vbCrLf) > 0
  sData = Replace(sData, """""" & vbCrLf, "")
Loop 
Do While Instr(sData, vbCrLf & vbCrLf) > 0
  sData = Replace(sData, vbCrLf & vbCrLf, vbCrLf)
Loop 
If Left(sData, 2) = vbCrLf Then
   sData = Right(sData, Len(sData) - 2)
End If

' Write file with any changes made
Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
oOutfile.Write(sData)
oOutfile.Close
Set oOutfile = Nothing

' Cleanup and end
Set oFSO = Nothing
Wscript.Quit

Open in new window

~bp
0
 
mbroad02Author Commented:
Awesome!!  It worked beautifully!  I have one further question before I close this.

Is there a way to remove extraneous quotes (") in the file?

See attached screen print (in a word doc).

Your help is greatly appreciated.

Thanks.
quotes-in-data-line.doc
0
 
mbroad02Author Commented:
And I guess there is one other thing I need to know (it IS 500 points):

I have several scripts I am running to clean up this text file (remove THIS character and THAT and blank lines).  Is there a way to append script1 with Script2 etc??

i.e

(all in one script file)
Script1
Script2
Script3

??

Your help is greatly appreciated and I will close this out after this question....
0
 
Bill PrewCommented:
With regard to the quotes, do you want to remove ALL double quotes from the file, or only certain ones?

With regard to running multiple scripts, that's easy enough to do.  I'd suggest two approaches, you could simply create a smal BAT batch file that does something like:

cscript //nologo script1.vbs
cscript //nologo script2.vbs
cscript //nologo script3.vbs

or you could have one "driver" VBS script that in turn does a call to the three scripts.

~bp
0
 
mbroad02Author Commented:
Thank you.  I DO wish to remove ALL double quotes as there are only 2 in each line of data (one at beginning and one at end).
0
 
mbroad02Author Commented:
I got it to work.   Thank you VERY much for all of your help!
0
 
Bill PrewCommented:
Always happy to help, glad it was useful.

~bp
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.

All Courses

From novice to tech pro — start learning today.