Link to home
Start Free TrialLog in
Avatar of mbroad02
mbroad02Flag for United States of America

asked on

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
Avatar of theruck
theruck
Flag of Slovakia image

Avatar of mbroad02

ASKER

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
Can you try putting the full path in?
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.
Avatar of Bill Prew
Bill Prew

Your sample attachment is NOT a text file, did you post the correct file?

~bp
SOLUTION
Avatar of Alan_White
Alan_White
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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?
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.
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
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
I'm assuming no errors were generated from the VBS script execution?

~bp
I did not receive any errors during VBS execution.

Attached is a partial hex dump of out put file.

Thanks
hex-dump.doc
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
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
Here is a display of hex dump.  I will test your file and see what happens.
Thanks
hex-dump.doc
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
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
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
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....
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
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).
I got it to work.   Thank you VERY much for all of your help!
Always happy to help, glad it was useful.

~bp