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

HowTo: use FSO.FileExists to validate a file

Status
Not open for further replies.

cxb

Programmer
Jul 9, 2003
14
US
Please help! I've been trying all the ways I can think of to figure out why this code is not working right. If any one can help me, I would really really apprecidate it!!!

function getfile()
{
var strLoc = document.frmMIO.Loc.options[frmMIO.Loc.selectedIndex].value;
var strWeek = document.frmMIO.Week.options[frmMIO.Week.selectedIndex].value;
var strFilePath="//I don't know why it's showing 2 ";" here, but I have only 1 in my code.
var strFile = "plu";
var strFileSuf = ".pdf";
var strLocRef = strFilePath + strWeek + "/" + strFile + strLoc + strFileSuf;
var strEDir="E:\\InetPub\\ + strWeek +"\\" + strFile + strLoc + strFileSuf ;
//E is my physical drive on the web server.

var FSO = new ActiveXObject("Scripting.FileSystemObject");
if (FSO.FileExists(strEDir)== true)
{
window.open (strLocRef,"_self");
}
else
{
alert ("Sorry, the file you're looking for is not available.");
}


}

Thanks!
 
Code looks OK, without seeing for myself how your directories are structured. When you say "not working right" what do you actually mean... Expected outcomes vs Actual outcomes... that sort of thing?
 
I'm guesing that you run this from browser.
If so due to the security isues you cant create ActiveXObjects unless you want to enable it and make your computer vulnerable.

________
George, M
 
Thanks guys, for replying to my message.

To dwarfthrower, I want to check the existence of the file first. If the file exists, open the file; otherwise, pop up the sorry message box. However, somehow even if the file does exist, it would give me the message box. It looks like the line (FSO.FileExists(strEDir)== true) can not recognize strEDir right. My question is, in Javascript, is "\" handled in a special way? I tried using alert ("E:\anyfolder\anyfile.pdf"), and it would only display "E:anyfolderanyfile.pdf". That is why I used double "\". But, is it right to do it this way?

To George, ActiveXObjects are enabled on my computer (for some reason, my company does enable all the settings in Internet options.) Still, the code doesn't work.... :(

Any suggestions?
 
Yes, you're right to use \\ in JavaScript strings. I'd be concerned about the line (FSO.FileExists(strEDir)== true).

Since FileExists returns true or false have you tried simply if(FSO.FileExists(strEDir)){?

Also is it picking up files on your own local system OK? AS shadow says... if your security settings are done up too tight the FSO objects won't run properly.

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
I did try if(FSO.FileExists(strEDir)){ and it didn't work. I'm trying to check the files on a web server on the network, not local machine. Does this script have to be run at the server? I'm using <script language=&quot;javascript&quot;></script> to enclose the script...

Thanks!
 
You want to acess a shared resource?
It's not posible with FSO without using a Drive Mapping trick.

________
George, M
 
I forgot to add this code earlier, maybe it helps.
This shows how to acess a shared resource over network with FSO object.
Code:
<script language=javascript>
var fso=new ActiveXObject(&quot;Scripting.FileSystemObject&quot;)
var net=new ActiveXObject(&quot;Wscript.Network&quot;)
net.MapNetworkDrive(&quot;Z:&quot;,&quot;\\\\server\\path&quot;,false,&quot;username here&quot;,&quot;password here&quot;)
if(fso.FileExists(&quot;z:\\filename.here&quot;) 
{
	//take action
}
net.RemoveNetworkDrive(&quot;Z:&quot;,true,false)
</script>

________
George, M
 
I tried the following block of code,but got this error: &quot;This network connection does not exist&quot;. What did I do wrong? The file is on \\me01sql03\e$\InetPub\ All intranet users have read permission to it. The data folder is a shared folder known as webdata.

var fso=new ActiveXObject(&quot;Scripting.FileSystemObject&quot;)
var net=new ActiveXObject(&quot;Wscript.Network&quot;)
net.RemoveNetworkDrive(&quot;Z:&quot;,true,false);

net.MapNetworkDrive(&quot;Z:&quot;,&quot;\\\\me01sql03\\e$\\InetPub\\ if(fso.FileExists(&quot;Z:\\mio\\price\\week01\\plu1.pdf&quot;) )
{
alert(&quot;Yes&quot;)
}
net.RemoveNetworkDrive(&quot;Z:&quot;,true,false);

Thanks!
 
I think the error is that you acess a default share name witch you dont have Administrator permission to it.
If you say the sharename it's webdata use it there
net.MapNetworkDrive(&quot;Z:&quot;,&quot;\\\\me01sql03\\webdata&quot;,false)


________
George, M
 
I did try the sharename, but still didn't work....
 
Oh, I forgot to mention, I do have administrator right to the folder as well as the web server.
 
I got same error also but finaly i got it working.
Try to test the share first by using
Start->Run->\\servername\sharename
if this works then this should work also

net.MapNetworkDrive(&quot;N:&quot;,&quot;\\\\servername\\sharename&quot;)

________
George, M
 
THANK YOU SOOOO MUCH! It finally worked! I removed net.RemoveNetworkDrive(&quot;Z:&quot;,true,false); this line before net.MapNetworkDrive(&quot;Z:&quot;,&quot;\\\\me01sql03\\webdata&quot;) and it worked. However, my concern is what if Z dirve has been used? How can I check and remove any connection that has been mapped to Z before I map webdata to Z?
 
You can check with the FSO object what drive letter you can use.

________
George, M
 
Thank you for all your tips! I'm going to give it a try...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top