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!

Writting and reading a text file! 1

Status
Not open for further replies.

NightWatcher

Programmer
Jul 8, 2001
95
GB
Hi there,
I used the following code in Xitami web server and it worked fine, but now in IIS 5.0 it just doesn't.
The code just writes a text file with a number in it to be retrieved later by another page.

Code to write file:
======================================================
<%
Option Explicit
Response.Expires = -1
Dim FileObject, CounterFile, InPut , OutPut, NewCount, OldCount
Set FileObject = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
CounterFile = Server.MapPath (&quot;counterHOME.txt&quot;)
Set InPut = FileObject.OpenTextFile (CounterFile, 1, False)
OldCount = Trim(InPut.ReadLine)
NewCount = OldCount + 1
Set OutPut = FileObject.CreateTextFile (CounterFile, True)
OutPut.WriteLine(NewCount)
InPut.Close
OutPut.Close
Set FileObject = Nothing
Set InPut = Nothing
Set OutPut = Nothing
%>

Code to read file:
======================================================
<%
Option Explicit
Response.Expires = -1
Dim FileObject, CounterFile, InPut, OldCount
Set FileObject = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
CounterFile = Server.MapPath (&quot;counterHOME.txt&quot;)
Set InPut = FileObject.OpenTextFile (CounterFile, 1, False)
OldCount = Trim(InPut.ReadLine)
InPut.Close
Set FileObject = Nothing
Set InPut = Nothing
%>

Read/Write permissions are set to the file.

Can anyone tell me what's wrong with it?
Is it the object that doesn't exist in IIS?
How do I verify if it exists or not?
Or, do you know of an easy and simple alternative?

Thank you.


NightWatcher
 
Try this way...

<%
Option Explicit

Response.Expires = -1
Dim FileObject, CounterFile, InPut , OutPut, NewCount, OldCount
Set FileObject = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
CounterFile = Server.MapPath (&quot;counterHOME.txt&quot;)
Set InPut = FileObject.OpenTextFile (CounterFile)
OldCount = Trim(InPut.ReadLine)
NewCount = OldCount + 1
InPut.WriteLine(NewCount)
InPut.Close
Set FileObject = Nothing
Set InPut = Nothing
%>

Code to read file:
======================================================
<%
Option Explicit
Response.Expires = -1
Dim FileObject, CounterFile, InPut, OldCount
Set FileObject = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
CounterFile = Server.MapPath (&quot;counterHOME.txt&quot;)
Set InPut = FileObject.OpenTextFile (CounterFile)
OldCount = Trim(InPut.ReadLine)
InPut.Close
Set FileObject = Nothing
Set InPut = Nothing
%>

On my IIS 5.0 works
 
Your code didn't worked either.
Dispite it's similarities it now says 'The RPC server is unavailable'..
Does anyone knows what that might sugest?

How do you have set permissions for the file and site?
ASP is working, the dinamic date is printed correctly in the site.

Thank you.


NightWatcher
 
Ahh.. Just one more thing.. I don't have VBScript 5.5 installed.
Do I really need it?

I believe that a server should be keeped as simple as possible, the more complex it gets, more error prone it will become (mainframe people mentality), but, if someone tells me that it's needed for the counter routine, I'll install it.


NightWatcher
 
No u don't need vb5.5
If u have Windows 2000 (&IIS 5.0) should be worked

The web server is in another computer or it's all in one?
 
The SERVER is another computer just for that, but it's the one that I'm using to develop the code and testing..
Should I use any other client?
I think the results will be the same, as I'm calling the pages from
Another question is, there is any kind of delay, from the time that I change something in IIS config, utill the change is actually active? Or is it active imediatly?

Thank you.


NightWatcher
 
I send to u an global.asa file to see what i use...
& masybe it helps u

<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Application_OnStart
'Application(&quot;WebImg&quot;) =&quot;d:\Inetpub\
' This script executes when the first user comes to the site.

'try to use this combination of Server.MapPath
VisitorCountFilename = Server.MapPath (&quot;/&quot;) + &quot;\visitors.txt&quot;
Set FileObject = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set Out= FileObject.OpenTextFile (VisitorCountFilename, 1, FALSE, FALSE)
' Initialize soft visitor counter here
Application(&quot;visitors&quot;) = Out.ReadLine
' Store physical file name of file containing the visitor count
Application(&quot;VisitorCountFilename&quot;) = VisitorCountFilename
END SUB
</SCRIPT>
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Application_OnEnd
' This script executes when the server shuts down or when global.asa changes.
' Overwrites the existing visitors.txt file
Set FileObject = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set Out= FileObject.CreateTextFile (Application(&quot;VisitorCountFilename&quot;), TRUE, FALSE)
Out.WriteLine(application(&quot;visitors&quot;))
END SUB
</SCRIPT>


<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Session_OnStart
' OnStart Event does four things:
' 1) increment the visitor counter
' 2) check for new or previous visitor to the site
' 3) make connection string for the database and store on the session object
' 4) make sure the user does not jump into the middle of site -- redirect to default.asp if so
' ----------------------------------

'Initializing the authentification code
Session(&quot;authorized&quot;) = 0


' Increase the visitor counter
Application.lock
Application(&quot;visitors&quot;)= application(&quot;visitors&quot;) + 1
t_visitors = application(&quot;visitors&quot;)
Application.unlock
Session(&quot;VisitorID&quot;) = t_visitors

' Periodically, save to file
'If t_visitors MOD 1 = 0 Then
SET FileObject = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set Out= FileObject.CreateTextFile (Application(&quot;VisitorCountFilename&quot;), TRUE, FALSE)
Application.lock
Out.WriteLine(t_visitors)
Application.unlock
'End If

END SUB
</SCRIPT>


<SCRIPT LANGUAGE=VBScript RUNAT=Server>
SUB Session_OnEnd
END SUB
</SCRIPT>

for the IIS server
The changes is active imediatly after u finish with the IIS config (after APPLY or OK)
 
Thanks 'Shaddow'.

I'm very new to ASP (1 year) and IIS (3 months), so I don't really know what to do with the global.asa. I'm using Win2KServer, and have 5 sites hosted in it.

Some questions now:

What is a GLOBAL.ASA?
Where can I locate mine? To compare with yours!
How many of them can I use?

Sorry to put you trought all this, but I really need some help, to get off the ground, at least.

Thank you.


NightWatcher
 
?What is a GLOBAL.ASA?
it's a file witch is executet before any page of your web is accessed...

?Where can I locate mine? To compare with yours!
Global.asa it's located in your \Inetpub\mine is in D:\Inetpub\if u have 5 webs u will find at least five directory in
\Inetpub\in each dir u will find their own global.asa file

?How many of them can I use?
it's used just one for web not more because this is an initialization file for your web variables and aplication as u may see and u couldn't have more..

i send to u an reference to global.asa that i hope to hel you

Global.asa Reference
The Global.asa file is an optional file in which you can specify event scripts and declare objects that have session or application scope. It is not a content file displayed to the users; instead it stores event information and objects used globally by the application. This file must be named Global.asa and must be stored in the root directory of the application. An application can only have one Global.asa file.

Global.asa files can contain only the following:

Application events
Session events
<OBJECT> Declarations
TypeLibrary Declarations
If you include script that is not enclosed by <SCRIPT> tags, or that defines an object that does not have session or application scope, the server returns an error. The server ignores both tagged script that the application or session events do not use, as well as any HTML in the file.

The scripts contained in the Global.asa file may be written in any supported scripting language. If multiple event or object scripts use the same scripting language, they can be combined inside a single set of <SCRIPT> tags.

When you save changes to the Global.asa file, the server finishes processing all of the current application requests before it recompiles the Global.asa file. During that time, the server refuses additional requests and returns an error message stating that the request cannot be processed while the application is being restarted.

After all of the current user requests have been processed, the server deletes all active sessions, calling the Session_OnEnd event for each session it deletes, closes the application, and calls the Application_OnEnd event. The Global.asa file is then recompiled. Subsequent user requests will start the application and create new sessions, and trigger the Application_OnStart and Session_OnStart events.

However, saving changes to a file that is included by the Global.asa file does not cause the server to recompile Global.asa. In order for the server to recognize changes in the included file, you must once again save the Global.asa file.

Procedures declared in the Global.asa file can only be called from one or more of the scripts associated with the Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd events. They are not available to the ASP pages in the ASP-based application.

To share procedures across an application, you can declare the procedures in a separate file, and then use server-side include (SSI) statements to include the file in the ASP pages that call the procedures. You typically give include files an .inc extension.

Note The examples in this document use Microsoft® Visual Basic® Scripting Edition (VBScript) as the primary scripting language. However, ASP scripts can be written in any supported scripting language, such as JScript™. For more information on how to change the primary language, see Working with Scripting Languages.
 
?What is a GLOBAL.ASA?
it's a file witch is executet before any page of your web is accessed...

?Where can I locate mine? To compare with yours!
Global.asa it's located in your \Inetpub\mine is in D:\Inetpub\if u have 5 webs u will find at least five directory in
\Inetpub\in each dir u will find their own global.asa file

?How many of them can I use?
it's used just one for web not more because this is an initialization file for your web variables and aplication as u may see and u couldn't have more..

i send to u an reference to global.asa that i hope to hel you
if u want anything elese u may email me at shaddow11_ro@yahoo.com

Global.asa Reference
The Global.asa file is an optional file in which you can specify event scripts and declare objects that have session or application scope. It is not a content file displayed to the users; instead it stores event information and objects used globally by the application. This file must be named Global.asa and must be stored in the root directory of the application. An application can only have one Global.asa file.

Global.asa files can contain only the following:

Application events
Session events
<OBJECT> Declarations
TypeLibrary Declarations
If you include script that is not enclosed by <SCRIPT> tags, or that defines an object that does not have session or application scope, the server returns an error. The server ignores both tagged script that the application or session events do not use, as well as any HTML in the file.

The scripts contained in the Global.asa file may be written in any supported scripting language. If multiple event or object scripts use the same scripting language, they can be combined inside a single set of <SCRIPT> tags.

When you save changes to the Global.asa file, the server finishes processing all of the current application requests before it recompiles the Global.asa file. During that time, the server refuses additional requests and returns an error message stating that the request cannot be processed while the application is being restarted.

After all of the current user requests have been processed, the server deletes all active sessions, calling the Session_OnEnd event for each session it deletes, closes the application, and calls the Application_OnEnd event. The Global.asa file is then recompiled. Subsequent user requests will start the application and create new sessions, and trigger the Application_OnStart and Session_OnStart events.

However, saving changes to a file that is included by the Global.asa file does not cause the server to recompile Global.asa. In order for the server to recognize changes in the included file, you must once again save the Global.asa file.

Procedures declared in the Global.asa file can only be called from one or more of the scripts associated with the Application_OnStart, Application_OnEnd, Session_OnStart, and Session_OnEnd events. They are not available to the ASP pages in the ASP-based application.

To share procedures across an application, you can declare the procedures in a separate file, and then use server-side include (SSI) statements to include the file in the ASP pages that call the procedures. You typically give include files an .inc extension.

Note The examples in this document use Microsoft® Visual Basic® Scripting Edition (VBScript) as the primary scripting language. However, ASP scripts can be written in any supported scripting language, such as JScript™. For more information on how to change the primary language, see Working with Scripting Languages.
 
Thanks once more 'Shaddow'..

Very detailed explanation, just what I needed, that will make my life a heck of alot easier.

Also thank you for giving me your e-mail, but I prefer to post Q/A here so everybody can benefit from them. If you want to reach me by e-mail here it is: nightwatcher@hotpop.com

I will experiment with the 'global.asa', but I have just noticed that I can't find any in the root of my webs.
There's no 'global.asa' in 'C:\Inetpub\ or in any root of my other sites, is it a hidden file or something?


NightWatcher
 
Yeap it may be hidden and also thanks for your email
U have to set in the MyComputer window -Tools menu- FolderOption - View tab -check the &quot;View hidden files or folders&quot;

But if u want to reach quikly at me you could get Yahoo Messenger or send me an email and we will use Tek to comunicate and so others will see what we discussed...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top