Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output to file

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
What is the syntax in vbscript for
open for input as #1....

I want to use asp pages to output text to a file
 
Code:
Sub GetSourceFile()
   Dim fso
Code:
'As FileSystemObject
Code:
   Dim fil
Code:
'As File
Code:
   Dim TxtIn
Code:
'As TextStream
Code:
   Dim tmpStr
Code:
'As String
Code:
   Set fso = CreateObject("Scripting.FileSystemObject")
   If fso.FileExists("your_Fullpath_Filename") Then
      Set fil = fso.GetFile("your_Fullpath_Filename")
      Set TxtIn = fil.OpenAsTextStream(1)
Code:
 '1 = open file for read
Code:
      Do While Not TxtIn.AtEndOfStream
         tmpStr = TxtIn.ReadLine
Code:
         'Do some stuff
Code:
      Loop
      TxtIn.Close
   End If
End Sub
 
BTW -

"Magic" numbers are considered bad form.

You should normally have several constants defined when using FSO, for example:
Code:
Const ForReading = 1 
Const ForWriting = 2 
Const ForAppending = 8
 
A cleaner way to do stand-alone scripting these days (non-web scripting) is to make Windows Script Files (.wsf) instead of VBScript (.vbs) or JScript (.js) files.

For one thing you can mix VBScript, JScript, or anything else you might have installed - say Perl or Rexx.

For another you can declare objects so you don't need to CreateObject them, and this gives your script a reference to the type library, which among other things brings in all the constants for the object. Great for using ADO, which uses LOTS of constants.

These objects don't need to be set to Empty before exiting either. No more memory leaks!

Example:
Code:
<job>
<object id=&quot;fso&quot; progid=&quot;Scripting.FileSystemObject&quot;/>
<script language=&quot;VBScript&quot;>

Dim f

Set f = fso.CreateTextFile(&quot;c:\\testfile.txt&quot;, true)
f.WriteLine &quot;This is a test.&quot;
f.Close

</script>
</job>
Check the online Help, there is a LOT of good information in there.
 
Yes, Dilettante is right. It is considered bad form to hard code these &quot;magic&quot; numbers.

I took this from some existing code, where the filename, and the (read, write, append) constant were passed into the routine. If I would have posted the routine, as it existed, it would not have made much sense.

In the future, I will try to use a little more effort when responding to a question.

Steve
 
for the txtin.readfile - is there txtin.writefile or something of hte sort
 
Yes. Here's an example from the MSDN Libarary.

Function TextStreamTest
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, f, ts
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
fso.CreateTextFile &quot;test1.txt&quot; ' Create a file.
Set f = fso.GetFile(&quot;test1.txt&quot;)
Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault)
ts.Write &quot;Hello World&quot;
ts.Close
Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
TextStreamTest = ts.ReadLine
ts.Close
End Function

When opening a TextStream the tristate values are optional.
The default is 0. There meanings are:
TristateUseDefault, -2, Opens the file using the system default.
TristateTrue, -1, Opens the file as Unicode.
TristateFalse, 0, Opens the file as ASCII.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top