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!

ASP Script Can't Find Text File 1

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

I'm writing a small asp script to read the contents of a text file from a mapped drive to my server.

I'm working with alot of FileSystemObject properties. When I ask if the DriveExists, it does. However, I cannot access my file (that does indeed exist on the server) because my drive is not ready for read/write (I discovered this while performing the IsReady method on my Drive Object.

Is this a server security issue concerning the drive? I'm not a network/system admin, so I don't know if this is something that I'm able to program.

Any help that anyone has on this would be very much appreciated. I sort of a newbie to ASP and I heard that reading/writing text files were easy.

Below is a snippet of my code:

<html>
<head><title>Read Records From Access Log File</title></head>
<body>

<br><br>
<%
dim LogFile, LogFileObj
'Read from a text file
LogFile = &quot;f:\inetpub\ DriveDir = &quot;f:\&quot;
FolderDir = &quot;f:\InetPub\
Set LogFileObj = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)

IF LogFileObj.FileExists(LogFile) THEN
response.write &quot;file exists&quot; & &quot;<br>&quot; 'debug line
Set LogTextFile = LogFileObj.OpenTextFile(LogFile)

'read a line of text file
WHILE NOT LogTextFile.AtEndOfStream
%>
<HR>
<%=LogTextFile.Readline%>
</HR>
<%
WEND
ELSE response.write &quot;file does not exist&quot; & &quot;<br>&quot;
'Close the textfile
'LogTextFile.Close
END IF'Does file exist
%>
<HR>
</font>
</body>
</html>


Thanks,
scripter73
 
Your Internet User account that the web-server runs under doesn't have access to that mapped drive.

I can't think right now of how to get around that other than to find a way to grant access to your that user account.

Someone had this a few days ago and ended up putting IIS on the actual server that hosts the physical hard drive where the information is stored. He then linked to that server to gather information.
 
Thanks for your help alistairpaul,

When you say Internet User account, do you mean <My Name user account?> or do you mean the actual client browser doesn't have access to that mapped drive?

Also, where are the settings set? Is it in IIS Management Console?

Thank you so much for your help,
scripter73
 
The IUSR_XXXXXXX local account on the IIS Server. It doesn't have network rights to access the network share.

Basically, the &quot;user account&quot; that the background web service uses to access files/features doesn't have the permissions to access anything on a remote machine. Technically, it's not even logged onto the network.

I can't seem to find a way to change what account your IIS service logs on as. It seems that you could have IIS logon as a domain level account, therefore having access to network resources, but I just can't see it at the moment.

Perhaps do a search at for IIS logon user, or something similar.
 
OK - GOT IT!

Head to IIS / Internet Service Manager.

Locate the &quot;web directory&quot; in ISM/IIS that holds your script. Right click on that directory, and go to properties, then the &quot;Directory Security&quot; tab.

Press &quot;Edit&quot; on the Anonymous access area - Press edit again on the Anonymous access area. input the DOMAIN\useraccount and password of the user you want to &quot;use network as&quot;.

Back out, and run your script. As long as the domain level account you specify has rights to that resource, it should work.

It worked here on my test system.
 
Hi Alistairpaul!
You're the best. I was finally able to use the Drive Object Properties in my Vbscript code.

However, I was hoping that I could ask another question.
I'm TOTALLY new at IIS. Here's the situation. My file &quot;read.asp&quot; is contained in the following directory - F:\INETPUB\
I did what you suggested, and that worked fine. I'm now able to see the F:\ drive which is my network mapped drive. However, even if I move my textfile (filename.txt) to the F:\ directory, when I try to perform that the file exists, using a statement like If fileObj.FileExists(filename), it continually tells me that my file does not exist. I know its there, though. Oh, incidentally, the advice you gave me was performed on my virtual web server whose local path is set to D:\INETPUB\
I hope I'm not mixing up directory names, etc. My overall goal is just to open the text file.

Thanks for all of your wonderful help. You're great!

Oh, here's my latest code if you need to see it:

<html>
<head><title>Read Records From Access Log File</title></head>
<body>
<font size=&quot;3&quot; face=&quot;Verdana, Helvetica, Sans-Serif&quot;><b>Reading Records From Access-Log File</b>
<br><br>
<%
dim LogFile, LogFileObj
'Read from a text file

LogFile = &quot;f:\nc010501.txt&quot;
DriveDir = &quot;f:\&quot;
FolderDir = &quot; f:\InetPub\
Set LogFileObj = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)

'*******************************************
' DEBUG SECTION TO DISPLAY FILE INFO
'*******************************************
'confirm file object creation

if isobject(logfileobj) then
response.write &quot;LogFileObj created successfully.&quot; & &quot;<br>&quot;
end if

'confirm if drive exists
if LogFileObj.DriveExists(DriveDir) then
response.write &quot;DriveDir &quot; & DriveDir & &quot; exists.&quot; & &quot;<br>&quot;
end if

'confirm if folder exists
if LogFileObj.FolderExists(FolderDir) then
response.write &quot;FolderDir &quot; & FolderDir & &quot; exists.&quot; & &quot;<br>&quot;
else
response.write &quot;FolderDir &quot; & FolderDir & &quot; does not exist.&quot; & &quot;<br>&quot;
end if

'confirm if file exists
if LogFileObj.FileExists(&quot;nc010501.txt&quot;) then
response.write &quot;Logfile &quot; & LogFile & &quot; exists&quot; & &quot;<br>&quot;
else
response.write &quot;Logfile &quot; & LogFile & &quot; does not exist&quot; & &quot;<br>&quot;
end if


'confirm that drive's ready for read/write
set drvObj = LogFileObj.GetDrive(DriveDir)
if drvObj.IsReady Then
response.write &quot;DriveDir &quot; & DriveDir & &quot; is ready for read/write.&quot; & &quot;<br>&quot;
else
response.write &quot;DriveDir &quot; & DriveDir & &quot; is not ready for read/write.&quot; & &quot;<br>&quot;
end if

'*******************************************
' END DEBUG SECTION
'*******************************************

IF LogFileObj.FileExists(LogFile) THEN
response.write &quot;file exists&quot; & &quot;<br>&quot;
Set LogTextFile = LogFileObj.OpenTextFile(LogFile)

'read a line of text file
WHILE NOT LogTextFile.AtEndOfStream
%>
<HR>
<%=LogTextFile.Readline%>
</HR>
<%
WEND
ELSE response.write &quot;<br>&quot; & &quot;file does not exist&quot; & &quot;<br>&quot;
'Close the textfile
'LogTextFile.Close
END IF'Does file exist
%>
<HR>
</font>
</body>
</html>

scripter73
 
in your debug section - you're referencing an actual file name when trying to get the &quot;fileexists&quot;:

if LogFileObj.FileExists(&quot;nc010501.txt&quot;) then

You might want to change it to look for &quot;f:\nc010501.txt&quot;, or look for the variable &quot;LogFile&quot;. It doesn't seem that it knows where to look for that file.

Give that a try and let me know.
 
Hi Alistairpaul,

You're right, I did hard code the LogFileObject.FileExists(&quot;actual filename&quot;), however, I'm afraid that I posted my desperate code when I sent it to you.

In my actual code, however, I have the following:

dim Logfile
...
Set Logfile=&quot;f:\nc010501.txt&quot;
...
Set LogFileObj = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
...
If LogFileObj.FileExists(LogFile) Then
...perform duties
End If


I just put in the actual filename in desperation. :) Sorry for the confusion. However, as I run the ASP code, my script still can't recognize the file. Now I'm trying to test if it will at least see a file on my hard drive (to take out the network element), and unfortunately it's behaving the same way.

Here's the code. Thanks again for taking the time out to help me. I REALLY appreciate it.


<html>
<head><title>Read Records From Access Log File</title></head>
<body>
<font size=&quot;3&quot; face=&quot;Verdana, Helvetica, Sans-Serif&quot;><b>Reading Records From Access-Log File</b>
<br><br>
<%
dim LogFile, LogFileObj
'Read from a text file

LogFile=&quot;c:\nc010501.txt&quot;
DriveDir = &quot;c:\&quot;
FolderDir = &quot;c:\charter\webstat\&quot;


Set LogFileObj = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set FolderObj = LogFileObj.GetFolder(Server.MapPath(&quot;..&quot;))


Set ObjFileCollection = FolderObj.Files


' Lets see what's in the directory:
' this is LogFileObjSet objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
'Set objFolderObject = objFSO.GetFolder(Server.MapPath(IMGS_DIR))
'Set objFSO = Nothing

'Set objFileCollection = objFolderObject.Files
'Set objFolderObject = Nothing


'*******************************************
' DEBUG SECTION TO DISPLAY FILE INFO
'*******************************************
'confirm file object creation

if isobject(logfileobj) then
response.write &quot;LogFileObj created successfully.&quot; & &quot;<br>&quot;
end if

'confirm if drive exists
if LogFileObj.DriveExists(DriveDir) then
response.write &quot;DriveDir &quot; & DriveDir & &quot; exists.&quot; & &quot;<br>&quot;
end if

'confirm if folder exists
if LogFileObj.FolderExists(FolderDir) then
response.write &quot;FolderDir &quot; & FolderDir & &quot; exists.&quot; & &quot;<br>&quot;
else
response.write &quot;FolderDir &quot; & FolderDir & &quot; does not exist.&quot; & &quot;<br>&quot;
end if

'confirm if folder object created successfully
if isobject(folderobj) then
response.write &quot;Folder Object created successfully. &quot; & &quot;<br>&quot;
else
response.write &quot;Folder Object did not create successfully.&quot; & &quot;<br>&quot;
end if

'confirm if file exists
if LogFileObj.FileExists(LogFile) then
response.write &quot;Logfile &quot; & LogFile & &quot; exists&quot; & &quot;<br>&quot;
else
response.write &quot;Logfile &quot; & LogFile & &quot; does not exist&quot; & &quot;<br>&quot;
end if


'confirm that drive's ready for read/write
set drvObj = LogFileObj.GetDrive(DriveDir)
if drvObj.IsReady Then
response.write &quot;DriveDir &quot; & DriveDir & &quot; is ready for read/write.&quot; & &quot;<br>&quot;
else
response.write &quot;DriveDir &quot; & DriveDir & &quot; is not ready for read/write.&quot; & &quot;<br>&quot;
end if

'show what's in the Collections
'response.write &quot;<br>&quot; & ObjFileCollection
'*******************************************
' END DEBUG SECTION
'*******************************************
%>
Available Space = <%=drvObj.AvailableSpace%> <br>
Physical Drive Letter = <%=drvObj.DriveLetter%> <br>
Drive Type = <%=drvObj.DriveType%> <br>
Is Drive Ready = <%=drvObj.IsReady%> <br>
Physical Path of Drive = <%=drvObj.Path%> <br>
Root Folder of Drive = <%=drvObj.RootFolder%> <br>

<%
'DEBUG END

IF LogFileObj.FileExists(LogFile) THEN
response.write &quot;file exists&quot; & &quot;<br>&quot;
Set LogTextFile = LogFileObj.OpenTextFile(LogFile)

'read a line of text file
WHILE NOT LogTextFile.AtEndOfStream
%>
<HR>
<%=LogTextFile.Readline%>
</HR>
<%
WEND
ELSE response.write &quot;<br>&quot; & &quot;file does not exist&quot; & &quot;<br>&quot;
'Close the textfile
'LogTextFile.Close
END IF'Does file exist
%>
<HR>
</font>
</body>
</html>


Current Results of Script

Reading Records From Access-Log File

LogFileObj created successfully.
DriveDir c:\ exists.
FolderDir c:\charter\webstat\ does not exist.
Folder Object created successfully.
Logfile c:\nc010501.txt does not exist
DriveDir c:\ is ready for read/write.
Available Space = 228687872
Physical Drive Letter = C
Drive Type = 2
Is Drive Ready = True
Physical Path of Drive = C:
Root Folder of Drive = C:\

file does not exist



Thanks again for the help,
scripter73
 
okay this worked perfectly for me.

2 things to try -

1. i noticed that the filename is listed as &quot;nc010501.txt&quot;. a more common date format would be &quot;nc050101.txt&quot; - did you by any chance make a typo?

2. is this drive NTFS permissioned, and locked down to certain users? if so, the IUSR account might not have rights to get to that file.

let me know...
 
Hi AlistairPaul!

Well, I tried your two suggestions. Sorry. I wish it was just a typo, but the filenaming is fine.

Also, the FileSystem of my C:\ is FAT.

I also right-clicked on my C:\ and everyone has FULL access to my C:\ drive now.

As I look through all of the permissions, it would seem that my browser would be able to find this file. I don't see any restrictions anywhere. Are you running Win NT 4.0 as your OS? Can I check my settings against yours if so?

Thanks,
scripter73
 
I'm running Win2K Professional, with NTFS on all drives.

perhaps you should try copying this script into a VBS file, and modify the necessary items to make it run under Wscript. That way, you could see if it runs under VBS, but not under ASP/webserver. That would at least eliminate the scripting factor of it.

i'm kind of at a loss for what to say at this point - since it works for me with no modifications, it's almost got to be a permissions or some type of configuration issue.

good luck - and if you have any more questions, post them on up.
 
Hi Alistairpaul,

Thanks for all of your help and hanging in there with me. I'll try this out. If I come up with something, I'll let you know.

Thanks again,
scripter73
 
Hi Alistairpaul,

I thought I'd give you some closure on this text file saga. You know those &quot;debug&quot; reports that I was printing out at the top of my script? Well I thought I'd investigate what environment they were being reported from. It appears that when I told my script C:\, the browser was looking on the hard drive of the server itself instead of on my local drive, which is where I had stored the file and kept telling my script to look. Needless to say that file was never on the server's hard drive, which would explain why I couldn't open it.

After I discovered that faux pas, I was able to manipulate the code by giving it the actual drive letter of where the file sat on the server. I gave up the notion of using my mapped network drive. I did have to make sure that the drive and the folder that I navigate to is completely shared so the browser can find it.

Thanks for all of your great help. :) Whew!
scripter73
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top