Click to See Complete Forum and Search --> : Remove file


belebala
January 21st, 2009, 02:01 PM
I have a temporary xml file is saved in a directory. I would like to delete this temp file after it's read. But it is not deleting from my code. What am I doing wrong? Thanks.

<SCRIPT language="javascript">
function removeTempFile()
{
String fileName = "C:\\Temp\\temp.xml";
File f = new File(fileName);
if(f.exists())
{
f.delete()
}
else{
alert("no file exists.");
}

}
</SCRIPT>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>

<script type="text/javascript">
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.load("temp.xml");
var xsl = new ActiveXObject("Microsoft.XMLDOM");
xsl.async = false;
xsl.load("<%=Stylesheet%>");
document.write(xml.transformNode(xsl));
document.write(removeTempFile());
</script>
</body>
</html>

PeejAvery
January 21st, 2009, 03:14 PM
Use f.remove(), not f.delete().

belebala
January 21st, 2009, 03:45 PM
It is not even calling the removeTempFile() function. I tried deleting the last line and write:
<body onbeforeunload="removeTempFile()"> instead. It didn't get in to the function because it is not printing my message box

PeejAvery
January 21st, 2009, 05:08 PM
You can't write a function like...

document.write(removeTempFile());

You simply call it.

removeTempFile();

belebala
January 21st, 2009, 05:36 PM
also tried your suggestion on my JSP page, but the function didn't get call.

Now I changed my code to:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>


<script type="text/javascript">
function removeTempFile()
{
alert("test");

var strFileName = "C:\\temp\\temp.xml";
alert("strFileName: " + strFileName);

var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.DeleteFile("strFileName",true);

alert("file deleted.");
}
</script>
</head>

<body onbeforeunload="removeTempFile()">
<script type="text/javascript">
var xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.load("temp.xml");
var xsl = new ActiveXObject("Microsoft.XMLDOM");
xsl.async = false;
xsl.load("<%=Stylesheet%>");
document.write(xml.transformNode(xsl));
removeTempFile();
</script>
</body>
</html>

"test" and "strFileName" got printed, but not the last alert box (file deleted). What's wrong with these two lines?
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.DeleteFile("strFileName",true);

Thank you.

PeejAvery
January 21st, 2009, 05:44 PM
Please start using [code] tags. Thanks!

Are you 100% sure that the file is really there? Test it by creating a blank text file on the C: drive and then use JavaScript to delete it.

belebala
January 22nd, 2009, 09:45 AM
Yes, the file is exists in the right location. The code is not getting down to the var fso line, it is not printing my alert box.


var fso = new ActiveXObject("Scripting.FileSystemObject");
alert ("test");

PeejAvery
January 22nd, 2009, 10:02 AM
There are only 2 reasons why that line will not work.


You are not using Internet Explorer
Internet Explorer's security setting are blocking it

belebala
January 22nd, 2009, 12:18 PM
I'm not allow to change the IE security settings.
How can I use Java's delete function instead of Javascript then?

PeejAvery
January 22nd, 2009, 01:28 PM
You could also try VBScript. Either way, since you are working with the client-side, the browser will require user interaction to obtain security allowance.