?
Solved

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

Posted on 2011-10-12
27
Medium Priority
?
5,806 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:mbroad02
  • 12
  • 11
  • 3
  • +1
27 Comments
 

Author Comment

by:mbroad02
ID: 36956166
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
 
LVL 10

Expert Comment

by:Alan_White
ID: 36956448
Can you try putting the full path in?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mbroad02
ID: 36956705
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36956714
Your sample attachment is NOT a text file, did you post the correct file?

~bp
0
 
LVL 10

Assisted Solution

by:Alan_White
Alan_White earned 300 total points
ID: 36956737
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
 

Author Comment

by:mbroad02
ID: 36956860
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
 
LVL 10

Expert Comment

by:Alan_White
ID: 36956925
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
 

Author Comment

by:mbroad02
ID: 36957027
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36957455
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36957471
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
 
LVL 59

Accepted Solution

by:
Bill Prew earned 1700 total points
ID: 36957541
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
 

Author Comment

by:mbroad02
ID: 36958134
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36958167
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36958171
I'm assuming no errors were generated from the VBS script execution?

~bp
0
 

Author Comment

by:mbroad02
ID: 36958227
I did not receive any errors during VBS execution.

Attached is a partial hex dump of out put file.

Thanks
hex-dump.doc
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36958329
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36958676
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
 

Author Comment

by:mbroad02
ID: 36961428
Here is a display of hex dump.  I will test your file and see what happens.
Thanks
hex-dump.doc
0
 

Author Comment

by:mbroad02
ID: 36961567
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36961731
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
 

Author Comment

by:mbroad02
ID: 36961881
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
 

Author Comment

by:mbroad02
ID: 36962387
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
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36963615
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
 

Author Comment

by:mbroad02
ID: 36964072
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
 

Author Comment

by:mbroad02
ID: 36964297
I got it to work.   Thank you VERY much for all of your help!
0
 
LVL 59

Expert Comment

by:Bill Prew
ID: 36964684
Always happy to help, glad it was useful.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

755 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