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

File Handling 1

Status
Not open for further replies.

tanneddevil

Programmer
Apr 20, 2004
18
CA
Hi everyone,

I have come up with a problem. I need a way to generate a counter that increments by 1 and be able to save that number. I will neeed to do this without any web server and only using IE 6.0. On way that i thought might make sense is to read and write to a text file. Do i need to use an Active X control. Could i also connect to a database where i can write to it without a web server? From my readings, I found that it was possible to do this with Active X. could anyone please help.
So basically, I need a way to generate a number when a user fills out a form that is unique. Keep in mind no web server.

ps. This would be on a network drive.

THanks in Advance
 
Can you use cookies? John/Diokles offers a great cookie sample at this post. Check it out!

--Dave
 

Here's code to do it reading/writing from a text file:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		var fsObj = null;						// holds a pointer to the FileSystemObject
		var counter = 0;						// holds the counter value
		var counterFile = 'C:\\jsCounter.txt';	// holds the path and filename to the counter text file

		// returns 0 if no counter or NaN is found, the counter value otherwise
		function readCounter() {
			try {
				if (!fsObj.FileExists(counterFile))	return(0);
				var tempValue = fsObj.OpenTextFile(counterFile, 1).ReadLine();
				if (isNaN(tempValue) || tempValue == '' || tempValue == null) return(0);
				return(tempValue);
			}
			catch (err) {
				return(0);
			}
		}

		// returns false if write failed, true otherwise
		function writeCounter(counterValue) {
			try {
				//alert(counterValue);
				var counterFileObj = fsObj.OpenTextFile(counterFile, 2, true);
				counterFileObj.WriteLine(counterValue);
				counterFileObj.Close();
				return(true);
			}
			catch (err) {
				return(false);
			}

		}

		function init() {
			try {
				this.fsObj = new ActiveXObject('Scripting.FileSystemObject');
			}
			catch (err) { }

			if (!this.fsObj) {
				alert('You must allow the ActiveX object to load for the counter to work');
			} else {
				counter = readCounter();
				alert('Current counter: ' + counter);
				counter++;
				if (!writeCounter(counter)) alert('There was an error writing the counter');
			}
		}
	//-->
	</script>
</head>

<body onload="init();">
</body>
</html>

It should be documented enough that you can make it read/write from a network text file.

Hope this helps,
Dan
 
thanks dan,

I know this is what i need, but now it says it needs the active x controls to be loaded. Im working off a network, would i need to set the browser to accept active x controls and if so, how??

thanks
 

I assume you'd already worked those issues out when you asked for an ActiveX solution... sorry ;o)

You need to set your browser's security settings for each PC you want to run this on. A global "push-out" of security settings would be best if you want to do this for lots of PCs (check with your sysadmin about this).

In IE6, you click "Tools", "Internet Options", "Security" and change the levels accordingly.

Dan
 
Sorry my mistake..
its a monday..i new how to do that..im just under some pressure from the "corporation" I will want to take the contents of that file and add it to a variable where the variable will be passed to the subject line of the mail to function.

 
To add it to a message subject, after the following line:

Code:
if (!writeCounter(counter)) alert('There was an error writing the counter');

add this line:

Code:
document.location = 'mailto:your@address.here?subject=Your counter is currently at: ' + counter + '. Woohoo.';

Hope this helps ;o)

Dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top