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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

opening files?

Status
Not open for further replies.

brownie124

Programmer
Sep 19, 2002
61
0
0
US
Hi,

Very new to VBScript and ASP. I am trying to open a file in my ASP and it isn't working. Here is the code:
Code:
Dim strFileName

strFileName = "foo.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set datafile = fso.OpenTextFile(strFileName, ForReading)

' More code here

I am not getting past the OpenTextFile() method call. So, I have a couple of questions:
1) Where are ASP errors logged?
2) Why can I not open the file?

The file resides in c:\inetpub\ I have tried putting the full path in, a relative path and no path.

Thanks,
- Michael
 
And Server.CreateObject

Hope This Help
PH.
 
didn;t even see that, nice catch PHV. you still need the mappath though.
 
Guys thanks for the quick responses, however, it still isn't working. Here is the code:
Code:
Dim filepath
Dim strFileName
Dim strRecord

If strTesting = "1" Then
    Select Case strType
        Case TYPE_ONE
            strFileName = "file.dat"
        Case TYPE_TWO
            strFileName = "file2.dat"
    End Select

    filepath = Server.MapPath(strFileName)
		
    Response.Write(&quot;filepath = &quot; & filepath & &quot;<br>&quot;)

    Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
    Response.Write(&quot;1<br>&quot;)
    Set objDataFile = objFSO.OpenTextFile(filepath, ForReading)
    Response.Write(&quot;2<br>&quot;)
End If

When I write out the contents of filepath, it looks right. It is c:\inetpub\
Yet it still isn't opening. 1 displays but 2 doesn't.

Thanks,
- Michael
 
Sure spazman, but I guess this:
Code:
strFileName = Server.Mappath(&quot;/exmc/foo.txt&quot;)


Hope This Help
PH.
 
A slight typo: the results of filepath is c:\inetpub\wwwroot\mysubdir\file.dat.

- Michael
 
Just in case ... Have you a Option Explicit ?
You can also, for debugging, use a On Error Resume Next and play with the Err object.

Hope This Help
PH.
 
Thanks for the &quot;On Error...&quot; tip. Although, I should know better. I have done tons of VBA.

Anyway, I got it to work. It did not like ForReading. I had no idea what that value was but assumed it was 1 and put that in -- it worked. A sample that I saw online used ForReading. Any ideas why it doesn't work? Is there some file that I have to include?

Thanks,
- Michael
 
The constanT ForReading is not defined... you must either define it yourself or add a reference to the dll that contains those constants... in this case it is the scripting runtime...

You could add this (see metadata tag below) to your asp page or in your global.asa or..

if you are using MS interdev...

you can click the project menu (next to file, edit,and view) then click &quot;project references&quot; then add &quot;microsoft scripting runtime&quot;... and it will add the metadata tag to your gloabal asa for you... once the metatag is added the constant names will be available... the same goes for the ADO constants like adParamInput... you would just add the metadata tag for ado... I copied the metatags for you... just in case you are not using interdev


<!--METADATA TYPE=&quot;TypeLib&quot; NAME=&quot;Microsoft Scripting Runtime&quot; UUID=&quot;{420B2830-E718-11CF-893D-00A0C9054228}&quot; VERSION=&quot;1.0&quot;-->

<!--METADATA TYPE=&quot;TypeLib&quot; NAME=&quot;Microsoft ActiveX Data Objects 2.7 Library&quot; UUID=&quot;{EF53050B-882E-4776-B643-EDA472E8E3F2}&quot; VERSION=&quot;2.7&quot;-->
 
A very similar tip for WSH scripts is to create .WSF files instead of .VBS files, and use the <reference... /> tag.

This is documented in the Scripting CHM file, and either progIDs or GUIDs can be used that reference .TLBs, .OLBs, .DLLs, or .OCXs.

Examples:

<reference object=&quot;Scripting.FileSystemobject&quot;/>
<reference object=&quot;ADODB.Recordset&quot; version=&quot;2.7&quot;/>
 
And as usual, allways put a
Code:
 Option Explicit
instruction as the first line in your script.

Hope This Help
PH.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top