2 Replies Latest reply on May 30, 2006 7:37 AM by ~LivingProof~

    Check if certain files exist

    Cognethos Level 1
      Hi all,

      I am writing a reference guide and wondering if I can check whether some files exist from a page.
      If it exists then I can forward the user to one page.
      And if not they will see another page.
      Grateful if someone points me to the right direction.

      Thanks,
      Karen
        • 1. Re: Check if certain files exist
          Cognethos Level 1
          Hi all,

          Further investigating to it, I tried the following:

          <script language=JavaScript
          type="text/javascript">var fso = new ActiveXObject("Scripting.FileSystemObject");

          if(fso.FileExists("C:\Program Files\Common Files\My Directory\My file.txt"))
          {
          document.write("File Exist");
          }
          else
          {
          document.write("File not exist.");
          }</script>

          I always get "File not exist" message but I don't know why.
          Can anyone know where it is wrong?

          Thanks,
          Karen
          • 2. Check if certain files exist
            ~LivingProof~ Level 1
            Hello Cognethos,

            You need to use double backslashes in your file path reference. Your script is correct except this line:
            if(fso.FileExists("C:\Program Files\Common Files\My Directory\My file.txt"))

            It should read this:
            if(fso.FileExists("C:\\Program Files\\Common Files\\My Directory\\My file.txt"))

            The technical reason for this is that there are special characters that you can not directly use in a string. In this case, your path is the string. Backslash happens to be one of the special characters that you can not directly use in a string. To get around this, you need to do something called an escape sequence. In this case, the escape sequence for \ is \\.