...

View Full Version : Check for file existence



tdavis
06-30-2012, 04:32 PM
Can javascript check for the existence of a file or folder on the local hard drive? I need to modify the following script so that I have six locations rather than three; that is, if a file exists, use these three, else use these three (per the window.open functions in the code below). I found that ASP can do this, but I am not a regular coder, so I am not sure. Any help is appreciated!



<%
dim fs
set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists("c:\asp\introduction.asp")=true then
response.write("File c:\asp\introduction.asp exists!")
else
response.write("File c:\asp\introduction.asp does not exist!")
end if
set fs=nothing
%>




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0042)http://intapps.corp.cox.com/apps/icomsgui/ -->
<HTML><HEAD><TITLE>ICOMS GUI Home Page</TITLE>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type><LINK
rel=stylesheet type=text/css href="icomsgui_files/styles.css">
<SCRIPT language=JavaScript type=text/JavaScript>
<!--
<!-- Opens windows -->

function open_win1()
{
window.open("http://data1gui.corp.cox.com","Data1_Database","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=800, height=600")
}

function open_win2()
{
window.open("http://data2gui.corp.cox.com","Data1_Database","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=800, height=600")
}

function open_win3()
{
window.open("http://centralgui.corp.cox.com","Data1_Database","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=800, height=600")
}



//-->
</SCRIPT>

<META name=GENERATOR content="MSHTML 8.00.6001.19258"></HEAD>
<BODY bgColor=#ffffff>
<TABLE border=0 cellSpacing=0 cellPadding=0 width=786 align=center>
<TBODY>
<TR vAlign=bottom>
<TD height=36 colSpan=3><IMG src="icomsgui_files/header.jpg" width=786
height=35></TD></TR>
<TR>
<TD vAlign=top width=13><IMG src="icomsgui_files/borderlft.jpg" width=10
height=388></TD>
<TD vAlign=top width=760>
<TABLE border=0 cellSpacing=0 cellPadding=0 width=760>
<TBODY>
<TR>
<TD vAlign=top width=399><IMG src="icomsgui_files/coxlogo.jpg"
width=399 height=94></TD>
<TD vAlign=top width=361><IMG src="icomsgui_files/header2.jpg"
width=361 height=22></TD></TR>
<TR vAlign=top>
<TD colSpan=2>
<DIV align=center><IMG src="icomsgui_files/icomslogo.jpg" width=431
height=141></DIV></TD></TR>
<TR>
<TD colSpan=2>
<DIV align=center>
<P class=txtStandard><BR>Please make a selection from the list
below</P><!-- form table -->
<TABLE border=0 cellSpacing=0 cellPadding=0 width=400>
<TBODY>
<TR>
<TD vAlign=top>
<FORM method=post name=form1 action=icomsgui.html>
<TABLE width=400>
<TBODY>
<TR class=txtStandard>

<TD><INPUT onclick=open_win1() value=Data1 type=radio
name=databases> <A
href="javascript:open_win1();">Data1</A> </TD>

<TD><LABEL><INPUT onclick=open_win2() value=Data2
type=radio name=databases> <A
href="javascript:open_win2();">Data2</A></LABEL></TD>

<TD><LABEL><INPUT onclick=open_win3() value=Central
type=radio name=databases> <A
href="javascript:open_win3();">Central</A></LABEL></TD>

</TR>

</TBODY></TABLE></FORM></TD></TR></TBODY></TABLE></DIV></TD></TR></TBODY></TABLE></TD>
<TD vAlign=top width=16><IMG src="icomsgui_files/borderrgt.jpg" width=16
height=388></TD></TR>
<TR vAlign=top>
<TD height=17 colSpan=3><IMG src="icomsgui_files/borderbtm.jpg" width=786
height=17></TD></TR></TBODY></TABLE>
<P align=center>&nbsp;</P></BODY></HTML>

Philip M
06-30-2012, 05:00 PM
Can javascript check for the existence of a file or folder on the local hard drive?


Simple asnwer - (generally) no.

JavaScript running in a browser is a client-side language. JavaScript does not have any commands for reading or writing files. Modern browsers can read files on the server using an Ajax call, but otherwise JavaScript has no capability to read from, write to, modify or delete a file (except a cookie), communicate with the server, access a database, the client's operating system or the Windows registry, or alter the default behaviour of the browser.

Reading from an arbitrary location would be a huge security hole.

It is possible using FileSystemObject (if you use Windows Scripting Host or Internet Explorer in a trusted environment). That makes it suitable only for intranet applications.



All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

tdavis
06-30-2012, 05:22 PM
This is an intranet page. one that directs users to this server or that server. The issue is one version of the software versus another. we want to install in groups rather than everyone at once. But since javascript cannot do it, which is what I thought, I will have to explore other ideas. I do not know what windows scripting is. what about PHP? Can I incorporate the ASP code into the page and use that?

tdavis
06-30-2012, 07:26 PM
what about this?



<script type="text/javascript">
// initialize ActiveXObject and create an object of Scripting.FileSystemObject.
var fso = new ActiveXObject("Scripting.FileSystemObject");

// if condition to check whether the specified file exists or not.
if (fso.FileExists("C:\\Temp\\myFolder\\file.txt")) {
document.write("File.txt exists.");
}
else {
document.write("File.txt does not exist.");
}

fso = null;
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum