| Perhaps I should have been more explicit. The material is written to the
| disk when the script is closed or there is some other break in the
| action. Your test script "cheats" because it uses the "sleep" method.
| Try it without that operation.
|
That still isn't right. If I remove WScript.Sleep I get
all the same writes. It just does them all at the same
time. In other words, every time I call WriteLine it writes
to disk, whether that's 10 time in 100 ms or once every
second. (And it does them all before the file is closed.)
Where did you get the information you're basing this
on? The idea of writing to a buffer doesn't make sense.
The idea that scrrun puts the entire file into a vbCrLf-
delimited array when the file is opened is even more crazy.
Scrrun doesn't know that you plan to read the file by line!
And what if you have a Unix file that's vbCr-delimited?
But aside from all that, why would it be so incredibly
wasteful? If you don't believe me, try this:
Dim FSO, TS
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TS = FSO.OpenTextFile("C:\Windows\desktop\test2.txt", 1)
TS.Close
Set TS = Nothing
Set FSO = Nothing
This is the result in Filemon:
3:02:36 PM wscript.exe:908 OPEN C:\Windows\desktop\test2.txt SUCCESS
Options: Open Access: All
3:02:36 PM wscript.exe:908 CLOSE C:\Windows\desktop\test2.txt SUCCESS
The file was opened and then closed. To say that opening
includes a full read doesn't make sense. (If I skip TS.Close
I still get the same result.)
You don't have to take my word for all this. You can
run the tests yourself. I just can't imagine how you arrived
at your conclusions in the first place. Maybe "The
Scripting Guys"? I can imagine "The Scripting Guys" could
have said something that could have been misconstrued,
but I can't think where else you might have seen that info.
(The Scripting Guys should be ignored in general, from my
point of view. From what I've seen they tend to assume
stupidity on the part of their audience, then they say
what they think the audience should hear, rather than
stating the facts clearly... I don't know why it is that
most official Microsoft bloggers seem to be smug and
condescending, but in my experience they are. There's
a tendency to tell the audience only what they think the
audience should hear -- which may be partial truth or
even falsehood.
One of my favorite examples is the WinSxS folder on
Vista/7. According to MS President Sinofsy there are
almost no actual files in WinSxS:
"nearly every file in the WinSxS directory is a "hard link" to the physical
files elsewhere on the system"
http://blogs.msdn.com/b/e7/archive/2008/11/19/disk-space.aspx
Acording to the "Windows Server Core Team", everything
is in WinSxS and nowhere else:
"All of the components in the operating system are found in the WinSxS
folder - in fact we call this location the component store."
http://blogs.technet.com/b/askcore/archive/2008/09/17/what-is-the-winsxs-directory-in-windows-2008-and-windows-vista-and-why-is-it-so-large.aspx
Who's right? Who knows! I just wanted to know how to
clean out unnecessary WinSxS files safely. Silly me, I tried
to get that information from Microsoft, and they don't think
we should worry our pretty little heads poking around in
WinSxS. :)
It reminds me of a friend I used to have who was an MD.
One of his favorite sayings was, "a little knowledge is a
dangerous thing". His point was that if "laypeople" tried
to understand health issues they'd only get themselves into
trouble. ...Oddly enough, he once asked me for advice about
vitamins. It turned out that he didn't think vitamins was
a topic relevant to an MD's job!)
Fortunately, with issues like Textstream ops, we don't have to
trust the "experts". We can all easily test what's true and
what isn't.
| I didn't close either text stream explicitly because they are closed and
| released when the script closes, which is immediately after execution.
I wouldn't depend on that. At best it's a bad habit to
depend on WScript to clean up one's mess. But also,
in most actual cases, at least in my scripts, it doesn't
make sense to leave files open. It's sloppy. It uses more
memory. It's hard to keep track. There are cases where I
might open 100 large files, one at a time. Is WScript
closing each one before I open the next? Maybe. Either
way it's a problem. If WScript is closing the last file when
I reuse the TS variable then it's second guessing me when
it should raise an error. If it's not closing the file then it's either
going to cause an error or cause 100 files to be stuck in
memory.
Alternatively, I might be opening 10 files and writing back
to one or more. To not close after a read or write would be
asking for trouble. Try this:
Dim FSO, TS
Set FSO = CreateObject("Scripting.FileSystemObject")
Set TS = FSO.OpenTextFile("C:\Windows\desktop\test2.txt", 1)
s = TS.ReadLine
'-- Do lots of stuff here.
TS.WriteLine "okay"
It's another example of the limitations of your
initial example. Writing after CreateTextFile is
a simple operation, but if you open an existing file
you have to specify the mode. The above causes
an error with WriteLine because the file was opened
for reading.
If I'm doing anything more involved than one read
and write I usually write short subs for ReadFile and
WriteFile. The Textstream can then be created and
released within the sub and I can safely deal with
file content, not needing to keep track of what's open
in what mode.
| I'm a lousy typist. I would much rather type
| "mid(strng,n)" than
| "right(strng,len(strng)+1-n)"
| also, what happens when string length less than n is encountered?
It causes an error. I don't have a problem with that.
Errors are informative and can be trapped.
But as I said, I'm not arguing with your method. It's
perfectly "legal". I just don't like it in my own code
because I find it ambiguous. I think Mid should mean
Mid.... just my personal quirk.