p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

proasp_codeclinic thread: Problem using FileSystemObject in a Function call


Message #1 by "John Duckett" <john.duckett@i...> on Mon, 7 May 2001 13:51:51
You are passing a string with the value 'objFile'.  You should pass a
reference to objFile...

snotx(objFile)

Also, you may want to change the name of the variable within the function to
prevent scope conflicts with the name objFile (not required, but good
practice).

Michael Earls

-----Original Message-----
From: John Duckett [mailto:john.duckett@i...]
Sent: Monday, May 07, 2001 6:52 AM
To: Code Clinic
Subject: [proasp_codeclinic] Problem using FileSystemObject in a
Function call


Gurus,
My application does a lot of string processing, thus my choice of 
JScript.  The following code snippit is where selected process results are 
written to a file using the FileSystemObject (FSO).

This code works OK.

<%
var ForReading = 1;
var ForWriting = 2;
var ForAppending = 8;
var strPhysicalPath;
var objFSO;
var objFile;
var sometext;
strPhysicalPath = 'C:\\InetPub\\wwwroot\\test\\zzzzzzz.js'; 
objFSO = new ActiveXObject("Scripting.FileSystemObject");
objFile = objFSO.CreateTextFile(strPhysicalPath, true, false);
   sometext = "A whole lot of lkjjljlkjlk stuff";
   objFile.WriteLine(sometext);
   objFile.Close();

%>


The problem.  Everything works fine as long as I instantiate the FSO and 
do the processing immediately following, as above.   However, I need to 
instantiate the FSO, then call the writeline in a conditional loop as a 
function call.  In this case, it seems that the FSO is instantiated OK, 
but when I attempt to reference the objFile object it is "undefined"  The 
function snotx() will be called a lot so it needs to have spefific 
functionality.
I did try to pass the objFile when I call the function like this: function 
snotx('objFile'), but that did not work either.
I would appreciate any thoughts.  Thanks

<%
var ForReading = 1;
var ForWriting = 2;
var ForAppending = 8;
var strPhysicalPath;
var objFSO;
var objFile;
var sometext;
strPhysicalPath = 'C:\\InetPub\\wwwroot\\test\\zzzzzzz.js'; 
objFSO = new ActiveXObject("Scripting.FileSystemObject");
objFile = objFSO.CreateTextFile(strPhysicalPath, true, false);
%>

<Script language=JScript>
function snotx(){
  sometext = "The selected strings are written to the file here";
  objFile.WriteLine(sometext);
  objFile.Close();
}
</Script>

The runnable test code follows:




<%@ LANGUAGE = "JScript" %>
<%
var ForReading = 1;
var ForWriting = 2;
var ForAppending = 8;
var strPhysicalPath;
var objFSO;
var objFile;
var sometext;
strPhysicalPath = 'C:\\InetPub\\wwwroot\\test\\zzzzzzz.js'; 
objFSO = new ActiveXObject("Scripting.FileSystemObject");
objFile = objFSO.CreateTextFile(strPhysicalPath, true, false);

%>

<html>
<head>
<title>Untitled Document</title>
<Script language=JScript>

function snotx(){
 sometext = "A whole lot of lkjjljlkjlk stuff";
objFile.WriteLine(sometext);
objFile.Close();

}
</Script>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<img src="test.gif" onClick="snotx()"; width="143" height="146"> 
</body>
</html> 

  Return to Index