Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Why does Writeline leave a blank line at the end?

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,40783
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Why does Writeline leave a blank line at the end?
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Why does Writeline leave a blank line at the end? - 12/12/2006 1:15:12 AM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline

      

Run that and you get the two lines of text as expected but there's a 3rd blank line there.  I must say I've never noticed it before because it's never been an issue.  However, now it is ;)  I don't want it there.

The behaviour seems counter-intuitive to me.  Surely Writeline should be intelligent enough to add text after the last line without having to add a carriage return?

< Message edited by ginolard -- 12/12/2006 1:26:32 AM >


_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm
 
 
Post #: 1
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 1:24:00 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
I actually get an error when I run that because I don't have a file C:\Temp\TestOut.txt. When I create an empty file, I only get two lines in it running the code.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ginolard)
 
 
Post #: 2
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 1:26:49 AM   
  ehvbs

 

Posts: 2222
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi ginolard,

because .WriteLine -  as opposed to .Write - does write the Text (if given) and a LineEnding
(vbCrLf). This could be an issue, if you use .ReadAll/Split( .., vbCrLf ) to process a text file.
Split() (and Join()) use a separator approach: (item)<sep>(item); using .WriteLine implies
a delimiter strategy: (item)<delim>(item)<delim>. (Compare it to the question, whether
to put ";" after or between statements in different programming languages.)

I agree it's a pain; but - in my opinion -  not because one approach is more natural/intuitive
than the other, but because both methods coexists and you have to be aware of them.

(in reply to ginolard)
 
 
Post #: 3
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 1:27:09 AM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline
My mistake.  Code has been corrected.

What I get is

adsasdadadA
jhdfjhajdksfhjkadf
<blank line here>

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to ebgreen)
 
 
Post #: 4
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 1:30:30 AM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline
It's bloody annoying.

I want to read in a list of machines, scan them for something and then write out the offline machines to same file (so that the list of targets is gradually reduced as machines are scanned).  However, that blank line thing is making my script bomb out.

Grrr.

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to ginolard)
 
 
Post #: 5
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 1:46:29 AM   
  ehvbs

 

Posts: 2222
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Hi ginolard,

(1) would it be possible to

             (a) read the file into a collection (dictionary/array)
 
             (b) mark or delete 'bad' items

             (c) write not marked/remaining items to file

(2) if not, because you want access the information (from
      other processes) while the scan isn't completely done

      would you consider using a Database?

(in reply to ginolard)
 
 
Post #: 6
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 1:50:05 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
Well it is a hack, but you can trim the last line after you write the file:

Set ObjFSO=CreateObject("Scripting.FileSystemObject")
Set OutputFile=ObjFSO.CreateTextFile("C:\temp\testout.txt", True)
OutputFile.Write "adsasdadadA" & VbCrLf
OutputFile.Write "jhdfjhajdksfhjkadf" & VbCrLf
OutputFile.Close
strText = Left(objFSO.OPenTextFile("C:\temp\testout.txt").ReadAll(), Len(objFSO.OPenTextFile("C:\temp\testout.txt").ReadAll())-2)
Set OutputFile=ObjFSO.CreateTextFile("C:\temp\testout.txt", True)
OutputFile.Write strText
OutputFile.Close()
i = 0
For Each strLine In Split(objFSO.OPenTextFile("C:\temp\testout.txt").ReadAll(), vbLf)
   i = i + 1
   WScript.Echo i & ")" & strLine
Next

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ginolard)
 
 
Post #: 7
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 1:50:39 AM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline
That's all possible.  However, I have just chosen to cheat and use

For i = 0 to Ubound(myarray)-1
.
.
.
Next

So, it'll never read that pesky blank line at the end.  Let it not be said that I am a lazy coder ;)

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to ehvbs)
 
 
Post #: 8
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 2:04:04 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
Nevermind...stupid question.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ginolard)
 
 
Post #: 9
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 2:20:32 AM   
  ehvbs

 

Posts: 2222
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
Now let's think about christmas and the (un)fairness of the world in general.
Some less counter-intuitive program at ginolard's work place writes the
file with the persons who'll get a pay rise for christmas (will be taken back
for eastern; but that's IT business).

ebgreen<vbCrLf>
ehvbs<vbCrLf>
ginolard

Now ginolard's script will do the processing:

output:

      

code:

      

Dot you think that this is fair?

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 2:23:10 AM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline
Payrise?  What's a payrise?

Yes, I know I can do all that.  However, given that this is a one-off scan of all machines in the park I'm quite happy to leave it as a hack for now.

That doesn't change the fact that I think WriteLine's behaviour is stupid ;)

Of course, if I had any gumption at all today, I'd do it all in Powershell.

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to ehvbs)
 
 
Post #: 11
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 2:38:04 AM   
  TNO


Posts: 1399
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
I'm only half awake right now, but wouldn't it be easier to write all the results to a variable separated by vbnewline, then write() it all at once to the file?

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ginolard)
 
 
Post #: 12
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 2:39:33 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: online
I don't think WriteLine's behavior is stupid. It does exactly what it is supposed to do. It writes the string it is given and appends VbCrLf to it. How is it supposed to know that this is the last line that you intend to write to the file?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ginolard)
 
 
Post #: 13
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 2:45:04 AM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline
I didn't think it was stupid until it didn't do what I wanted it to do.

That makes it stupid.

I'm not in a very forgiving mood today.  I have pnuemonia.

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to ebgreen)
 
 
Post #: 14
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 3:02:58 AM   
  TNO


Posts: 1399
Score: 16
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
You could always use JScript, that way you could use writeline, get pissed off that it does the same thing. Delete it, then prototype the exact same function into object so it works the way you want using 1,000 lines of extra code, then forget that you did that and wonder why nothing else is working right when you use that function later (I swear, this never happened to me......)

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to ginolard)
 
 
Post #: 15
 
 RE: Why does Writeline leave a blank line at the end? - 12/12/2006 3:04:52 AM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline
I have pneumonia (and bronchitis).  That makes me physically ill.

I'd have to be mentally ill to use Jscript

;)

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to TNO)
 
 
Post #: 16
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Why does Writeline leave a blank line at the end? Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts