473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to use js to figure out if a file exists?

I am writing an HTML JavaScript app to dynamically update reports that have been generated by our report server. The reports are in HTML. The problem comes when there are empty pages. I get an error 404, which we all know that the page doesn't exist. I am trying to write the code to skip the pages that don't exist. Any help would be appreciated. This is all internal to our network, so Active X all the way open is ok.

PLEASE HELP!!!!!!!!!!

Below is my code...

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <!--
  3. SlideShow v1.0
  4.  
  5. Simply define your "slides" in the javascript slides[] array below.
  6. -->
  7. <html>
  8. <head>
  9. <title>SiteShow 1.0</title>
  10. <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  11.  
  12. <style>
  13. /* Change body background-color to change fade out color. */
  14. body.siteshow { margin:0; padding:0; background-color:#000000; }
  15. #menu
  16. {
  17.     font-family:Arial;
  18.     font-size:9pt;
  19.     display:none;
  20.     opacity:0.00;
  21.     -mozopacity:0.00;
  22.     filter:alpha(opacity=0);
  23.     position:absolute;
  24.     top:10px;
  25.     left:1100px;
  26.     padding:5px;
  27.     background-color:#000000;
  28.     color:#FFFFFF;
  29.     border:3px dotted #999999;
  30. }
  31. #menu a { color:#ffffff; }
  32. #menu a:hover { text-decoration:none; }
  33. #title { font-size:11pt; font-weight:bold; letter-spacing:2; }
  34. #slides { font-size:9pt; line-height:16pt; }
  35. .button { width:60px; font-size:9pt; letter-spacing:1; }
  36. </style>
  37.  
  38. <script type="text/javascript">
  39. var current_idx = 0;
  40. var slides = new Array();
  41. var menuwin;
  42. var show_timer;
  43. var menu_timer;
  44. var menu;
  45. var content;
  46. var loaded = true;
  47. var time = 4;
  48. var html = "";
  49. var x = 1;
  50.  
  51. // Define your "slides". 3 values for each are:
  52. //      1. Duration in seconds.
  53. //      2. Title to be used in menu.
  54. //      3. Source URL. Can be full URI or a relative URL.
  55.  
  56. var fso  = new ActiveXObject("Scripting.FileSystemObject"); 
  57. var fh = fso.CreateTextFile("X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID.htm", true); 
  58. fh = fopen(getScriptPath("X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID.htm"), 0); // Open the file for reading 
  59. if(fh!=-1) {
  60.    slides[x] = new Array(time, "Slide 1", "X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID.htm");
  61.    x=x+1;
  62. }
  63.  
  64. fh = fopen(getScriptPath("X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID1.htm"), 0); // Open the file for reading 
  65. if(fh!=-1) {
  66.    slides[x] = new Array(time, "Slide 1", "X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID1.htm");
  67.    x=x+1;
  68. }    
  69.  
  70. fh = fopen(getScriptPath("X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID2.htm"), 0); // Open the file for reading 
  71. if(fh!=-1) {
  72.    slides[x] = new Array(time, "Slide 1", "X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID2.htm");
  73.    x=x+1;
  74. }    
  75.  
  76. fh = fopen(getScriptPath("X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID3.htm"), 0); // Open the file for reading 
  77. if(fh!=-1) {
  78.    slides[x] = new Array(time, "Slide 1", "X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID3.htm");
  79.    x=x+1;
  80. }    
  81.  
  82. fh = fopen(getScriptPath("X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID4.htm"), 0); // Open the file for reading 
  83. if(fh!=-1) {
  84.    slides[x] = new Array(time, "Slide 1", "X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID4.htm");
  85.    x=x+1;
  86. }    
  87.  
  88. fh = fopen(getScriptPath("X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID5.htm"), 0); // Open the file for reading 
  89. if(fh!=-1) {
  90.    slides[x] = new Array(time, "Slide 1", "X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID5.htm");
  91.    x=x+1;
  92. }    
  93.  
  94. fh = fopen(getScriptPath("X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID6.htm"), 0); // Open the file for reading 
  95. if(fh!=-1) {
  96.    slides[x] = new Array(time, "Slide 1", "X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID6.htm");
  97.    x=x+1;
  98. }    
  99.  
  100. fh = fopen(getScriptPath("X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_IDLast.htm"), 0); // Open the file for reading 
  101. if(fh!=-1) {
  102.    slides[x] = new Array(time, "Slide 1", "X:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_IDLast.htm");
  103.    x=x+1;
  104. }    
  105.  
  106.  
  107.  
  108.  
  109.  
  110. function MenuInit()
  111. {
  112.     var html = "";
  113.     for(idx=1; idx<slides.length; idx++) {
  114.         html += '<a href="javascript:Navigate('+idx+')">' +
  115.             slides[idx][1] + "</a><br />\n";
  116.     }
  117.     document.getElementById("slides").innerHTML = html;
  118.     menu.style.display = "block";
  119. }
  120.  
  121. function MenuShow()
  122. {
  123.    clearTimeout(menu_timer);
  124.     opacity('menu', 0, 90, 500);
  125.     menu_timer = setTimeout("MenuHide()", 3500);
  126. }
  127.  
  128. function MenuHide()
  129. {
  130.     opacity('menu', 90, 0, 500);
  131. }
  132.  
  133. function Pause()
  134. {
  135.     clearTimeout(show_timer);
  136.     document.getElementById('play').style.display = "block";
  137.     document.getElementById('pause').style.display = "none";
  138. }
  139.  
  140. function Navigate(slide_idx)
  141. {
  142.     clearTimeout(show_timer);
  143.     if (current_idx == 0) {
  144.         if (!slide_idx) { slide_idx = 1; }
  145.         current_idx = slide_idx;
  146.         content.src = slides[current_idx][2];
  147.         document.getElementById('play').style.display = "none";
  148.         document.getElementById('pause').style.display = "block";
  149.         show_timer = setTimeout("Navigate()", slides[current_idx][0]*1000);
  150.         return;
  151.     }
  152.  
  153.     if (slide_idx) {
  154.         current_idx = slide_idx;
  155.         content.src = slides[current_idx][2];
  156.         document.getElementById('play').style.display = "block";
  157.         document.getElementById('pause').style.display = "none";
  158.         return;
  159.     }
  160.  
  161.     loaded = false;
  162.     current_idx++;
  163.     if ( current_idx == slides.length) { current_idx = 1; }
  164.     opacity('content', 100, 0, 500);
  165.     document.getElementById('play').style.display = "none";
  166.     document.getElementById('pause').style.display = "block";
  167.     show_timer = setTimeout("Navigate()", slides[current_idx][0]*1000);
  168.     return;   
  169. }
  170.  
  171. function opacity(id, opacStart, opacEnd, millisec)
  172. {
  173.     //speed for each frame
  174.     var speed = 0;
  175.     // Math.round(millisec / 50);
  176.     var timer = 0;
  177.  
  178.     //determine the direction for the blending, if start and end are the same nothing happens
  179.     if(opacStart > opacEnd) {
  180.         for(i = opacStart; i >= opacEnd; i--) {
  181.             setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
  182.             timer++;
  183.         }
  184.         if (opacEnd == 0) { setTimeout("FadeOutTrigger('"+id+"')",((timer-1) * speed));; }
  185.         //if (opacEnd == 0) { FadeOutTrigger(id); }
  186.     } else if(opacStart < opacEnd) {
  187.         if (opacStart == 0) { FadeInTrigger(id); }
  188.         for(i = opacStart; i <= opacEnd; i++)
  189.             {
  190.             setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
  191.             timer++;
  192.         }
  193.     }
  194. }
  195.  
  196. //change the opacity for different browsers
  197. function changeOpac(opacity, id)
  198. {
  199.     var object = document.getElementById(id).style;
  200.     object.opacity = (opacity / 100);
  201.     object.MozOpacity = (opacity / 100);
  202.     object.KhtmlOpacity = (opacity / 100);
  203.     object.filter = "alpha(opacity=" + opacity + ")";
  204. }
  205.  
  206. function FadeOutTrigger(id)
  207. {
  208.     //alert('FadeOut: '+id);
  209.     switch(id) {
  210.     case "menu":
  211.         document.getElementById(id).style.display = "none";
  212.         break;
  213.     case "content":
  214.         content.src = slides[current_idx][2];
  215.         //setTimeout("opacity('content', 0, 100, 500)", 1000);
  216.         break;
  217.     default:
  218.         break;
  219.     }
  220. }
  221.  
  222. function FadeInTrigger(id)
  223. {
  224.     //alert('FadeIn: '+id);
  225.     switch(id) {
  226.     case "menu":
  227.         document.getElementById(id).style.display = "block";
  228.         break;
  229.     case "content":
  230.         //opacity('content', 0, 100, 500);
  231.         break;
  232.     default:
  233.         break;
  234.     }
  235. }
  236.  
  237. function FadeInContent()
  238. {
  239.     Html2Load();
  240.     if (!loaded) {
  241.         opacity('content', 0, 100, 500);
  242.         loaded = true;
  243.     }
  244. }
  245.  
  246. function LoadTrigger()
  247. {
  248.     //self.resizeTo(1366,768);
  249.     menu = document.getElementById('menu');
  250.     var html = "";
  251.     for(idx=1; idx<slides.length; idx++) {
  252.         html += '<a href="javascript:Navigate('+idx+')">' +
  253.             slides[idx][1] + "</a><br />\n";
  254.     }
  255. //    checkUrl(html);
  256.     content = document.getElementById('content');
  257.     Navigate();
  258.     MenuInit();
  259.     MenuShow();
  260. }
  261.  
  262. window.onload = LoadTrigger;
  263.  
  264. </script>
  265.  
  266. </head>
  267. <body class="siteshow">
  268. <iframe id="content" name="content" style="width:100%; height:100%;" frameborder="no" scrolling="auto" src="" onmouseover="MenuShow();" onload="FadeInContent();" ></iframe>
  269. <div id="menu">
  270.     <div id="title">Display Menu</div>
  271.     <div id="slides">
  272.     </div>
  273.     <p>
  274.         <input id="pause" class="button" style="display:block;" type="button" value="pause" onclick="Pause()" />
  275.         <input id="play" class="button" style="display:none;" type="button" value="play" onclick="Navigate()" />
  276.     </p>
  277. </div>
  278. </body>
  279. </html>
Jan 4 '11 #1
11 2300
Dormilich
8,658 Expert Mod 8TB
not sure what ActiveX has in stock, but in JavaScript you can make an AJAX HEAD request (to the server), which only returns the file response headers (which hold the 404 status).
Jan 4 '11 #2
Is that something I can put in my code that was provided on the first post. Can you help?
Jan 4 '11 #3
Rabbit
12,516 Expert Mod 8TB
The Scripting.FileSystemObject object has a FileExists method.
Jan 4 '11 #4
Is this how I would use the code?


Expand|Select|Wrap|Line Numbers
  1. var x = 1;
  2. var fso  = new Scripting.FileSystemObject("Scripting.FileSystemObject"); 
  3. var fh = fso.FileExists("I:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID.htm", true); 
  4.  if(fh!=-1) {
  5.    slides[x] = new Array(time, "Slide 1", "I:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID.htm");
  6.    x=x+1;
  7. }
Jan 4 '11 #5
I have tried what I think is everything. I keep getting an error "Automation server can't create object" any ideas what I need to do?
Jan 4 '11 #6
Rabbit
12,516 Expert Mod 8TB
myObject = new ActiveXObject("Scripting.FileSystemObject");
Jan 4 '11 #7
This is what I have and I am still getting this error "Automation server can't create object" Do I not have my Internet Explorer setting set properly for ActiveX? Or is there something I am missing?

Expand|Select|Wrap|Line Numbers
  1. var myObject = new ActiveXObject("Scripting.FileSystemObject"); 
  2. if (myObject.FileExists("I:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID.htm")) {
  3.     slides[x] = new Array(time, "Slide 1", "I:\\CrystalReports\\CurrentLaborByWO\\CurrentLaborByWO_ID.htm");
  4.     x=x+1;
  5. }
Jan 4 '11 #8
Rabbit
12,516 Expert Mod 8TB
That's possible. Check your ActiveX settings and see if something is blocking it. I normally have no problems doing what you're trying to do. Except I use VBScript instead of JavaScript.
Jan 4 '11 #9
Rabbit,

Thanks for you help ... end of the day today so I will start up in the morning.
Jan 4 '11 #10
Which is best VBScript of JavaScript for this situation?
Jan 4 '11 #11
Rabbit
12,516 Expert Mod 8TB
VBScript is easier, JavaScript is more powerful and flexible. It's up to you really, whichever one you're more comfortable with. If it can be done in VBScript, then it can be done in JavaScript. It's probably just some setting preventing it from being run. Are you trying to do this from an HTML file? Change the extension to an HTA file and see if that works.
Jan 4 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Geiregat Jonas | last post by:
is using if(open("file",O_EXCL) != -1){ printf("File does exists")}else{printf("file does not exists"); } a good way of checking if a file exists or not, if not how should I do it ?
18
by: Dan | last post by:
I have code like the following to test for existence of a file. I know the file is there, but File.Exists returns FALSE. The problem appears to be that the file is in a directory beneath "My...
2
by: Zeno Lee | last post by:
I'm using File.Exists to test a file on my C: drive. My program was strongly named and had caspol -af run on it to allow it to run from the network. There are 3 ways I am doing this: 1) Run...
2
by: Chris Fink | last post by:
I am using the System.IO.File class to determine if a file exists on a network share. The File.Exists method keeps returning false, even though the file does exist. The MSDN documentation...
11
by: sur | last post by:
Hello, My problem is that File.Exists works fine if my file is on my local drive but returns false if its on any other drive. I think that the issue is probably file permissions and so I have...
4
by: DEWright_CA | last post by:
I am trying to see if a file exists in a virtual directory, and if so run a method. I try doing File.Exists and the method runs but the file isn't there. Is there a web version of File.Exists or...
2
by: MadCrazyNewbie | last post by:
He Group, I would like to be able to Modify the follwoing code: Private Sub btnCentrexLinesReports_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles...
5
by: sword | last post by:
How can I check whether a file exists?
26
by: Army1987 | last post by:
Is this a good way to check wheter a file already exists? #include <stdio.h> #include <stdlib.h> int ask(const char *prompt); typedef char filename; int main(int argc, char *argv) { FILE...
7
by: sprash | last post by:
Newbie question: I'm trying to determine if a file physically exists regardless of the permissions on it Using File.Exists() returns false if it physically exists but the process does not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.