if file exist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zhisol

    if file exist

    how can i check if file exist in current directory (in javascript)?

    --
    pozdr;)
    zhisol
  • kaeli

    #2
    Re: if file exist

    In article <Xns93FB54D6BFF 91zhisolo2pl@19 5.136.250.206>, zhisol@o2.pl
    enlightened us with...[color=blue]
    > how can i check if file exist in current directory (in javascript)?
    >
    >[/color]

    This isn't really possible with normal JS in a normal security
    environment.

    Did you mean on the server or on the client?
    Is this for a web page or an intranet application, CD-ROM, etc...?


    -------------------------------------------------
    ~kaeli~
    Hey, if you got it flaunt it! If you don't, stare
    at someone who does. Just don't lick the TV screen,
    it leaves streaks.


    -------------------------------------------------

    Comment

    • zhisol

      #3
      Re: if file exist

      kaeli <infinite.possi bilities@NOSPAM att.net> wrote in
      news:MPG.19d4b3 1f2af131a598986 2@nntp.lucent.c om:
      [color=blue]
      > This isn't really possible with normal JS in a normal security
      > environment.
      >
      > Did you mean on the server or on the client?
      > Is this for a web page or an intranet application, CD-ROM, etc...?[/color]

      it's for web page. i mean exactly that:

      if("cover.jpg" exist)
      document.write( '<img src='cover.jpg' >')
      else
      //display nothing

      and i don't know how can i realise "if("cover. jpg" exist)"

      for example i have address: http://mysite.com/show.html
      and if in root on server is a file "cover.jpg" , make this visable,
      otherwise not.

      normally in html if "cover.jpg" doesn't exist - client see empty box, but
      i want that he'll see nothing

      ps: sorry for my english:)
      --
      pozdr;)
      zhisol

      Comment

      • Hywel Jenkins

        #4
        Re: if file exist

        In article <Xns93FB54D6BFF 91zhisolo2pl@19 5.136.250.206>, zhisol@o2.pl
        says...[color=blue]
        > how can i check if file exist in current directory (in javascript)?[/color]

        Do you have server-side JavaScript or ASP with JavaScript facilities?
        If you do, have a look at http://www.4guysfromrolla.com/
        --
        Hywel I do not eat quiche


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: if file exist

          zhisol <zhisol@o2.pl > writes:
          [color=blue]
          > if("cover.jpg" exist)
          > document.write( '<img src='cover.jpg' >')
          > else
          > //display nothing[/color]

          This will not work, since checking whether it exists will be
          asynchroneous. You will not be able to wait for the result before
          writing the image tag.
          [color=blue]
          > and i don't know how can i realise "if("cover. jpg" exist)"[/color]

          What you can do is write the image tag with a src pointing to an empty
          image, and then change the src if cover.jpg exists.

          function setIfExists(id, src) {
          var img = new Image();
          img.onload = function () {
          document.images[id].src = src;
          };
          img.onerror = function () {
          // do something if images doesn't exist.
          }
          img.src = src;
          }

          [color=blue]
          > for example i have address: http://mysite.com/show.html
          > and if in root on server is a file "cover.jpg" , make this visable,
          > otherwise not.
          >
          > normally in html if "cover.jpg" doesn't exist - client see empty box, but
          > i want that he'll see nothing[/color]

          You can also start the image out hidden (either visibility:hidd en or
          display:none) and change it to something visible if the image exists.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          • Mark Space

            #6
            Re: if file exist

            Lasse Reichstein Nielsen wrote:[color=blue][color=green]
            >>if("cover.jpg " exist)
            >> document.write( '<img src='cover.jpg' >')[/color][/color]
            [color=blue]
            > This will not work, since checking whether it exists will be
            > asynchroneous. You will not be able to wait for the result before
            > writing the image tag.[/color]

            Huh? As it happens, I was going to ask almost exactly the same
            question. Your answer makes no sense to me. I mean, in vbscript I can
            read files, directories, etc. But it doesn't work in JavaScript? Wow.
            You don't mean client side vs. server side, do you?

            I need a way to discover the path of the running script's file (on the
            server), read the directory said script resides in, and manipulate the
            files there in a fairly simple way. This is all server side, of course.

            In vbscript I can do something like this:

            Set objFSO = Server.CreateOb ject("Scripting .FileSystemObje ct")
            pathXlat = Request.ServerV ariables("PATH_ TRANSLATED")
            pathXlatRoot = Left( pathXlat, InStrRev( pathXlat, "\" ) )
            Response.Write "My Path: " & pathXlatRoot & "index.html <br>"

            and the path shows up just fine. I can also step through the files or
            folders in my subdirectory like so:

            Set objFolder = objFSO.GetFolde r( pathXlatRoot )
            Set colFolders = objFolder.SubFo lders
            For Each objSubFolder in colFolders
            If FileExists( objSubFolder, "a_name" ) Then
            call do_something()
            End If
            Next

            No problemo. Anyone know the javascript equivalent? I'm fairly new,
            the above was painstakingly hacked out, and I've only got VB references
            handy. Web searches aren't really helping.


            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: if file exist

              Mark Space <mark_space44@h otmail.com> writes:
              [color=blue]
              > Huh? As it happens, I was going to ask almost exactly the same
              > question. Your answer makes no sense to me. I mean, in vbscript I
              > can read files, directories, etc. But it doesn't work in JavaScript?
              > Wow. You don't mean client side vs. server side, do you?[/color]

              Yes. As I read the question (which can be incorrect, it is not very
              precisely stated), the original poster wants to check whether an image
              exists in the root of the server, and if so, insert an img tag with
              document.write. You can only use document.write on the client, so
              the check must be performed on the client and check the existence of
              a file on the server. For that, my answer is correct.

              If you are talking server side scripting, which is probably ASP then,
              I would recommend an ASP-specific group, since it is not really about
              Javascript.
              [color=blue]
              > In vbscript I can do something like this:
              >
              > Set objFSO = Server.CreateOb ject("Scripting .FileSystemObje ct")
              > pathXlat = Request.ServerV ariables("PATH_ TRANSLATED")
              > pathXlatRoot = Left( pathXlat, InStrRev( pathXlat, "\" ) )
              > Response.Write "My Path: " & pathXlatRoot & "index.html <br>"[/color]
              [color=blue]
              > and the path shows up just fine. I can also step through the files or
              > folders in my subdirectory like so:
              >
              > Set objFolder = objFSO.GetFolde r( pathXlatRoot )
              > Set colFolders = objFolder.SubFo lders
              > For Each objSubFolder in colFolders
              > If FileExists( objSubFolder, "a_name" ) Then
              > call do_something()
              > End If
              > Next
              >
              > No problemo. Anyone know the javascript equivalent?[/color]

              I'll assume that the ASP objects work the same. If that is true, then
              the following should work in JScript:

              var objFSO = Server.CreateOb ject("Scripting .FileSystemObje ct");
              var pathXlat = Request.ServerV ariables("PATH_ TRANSLATED");
              var pathXlatRoot = pathXlat.substr ing(0,pathXlat. lastIndexOf("/")+1);
              Response.Write( "My Path: " + pathXlatRoot + "index.html <br>");

              and

              var objFolder = objFSO.GetFolde r(pathXlatRoot) ;
              var colFolders = objFolder.subFo lders;
              for (var index in colFolders) {
              var objSubFolder = colFolders[index];
              if (FileExists(obj SubFolder, "a_name")) {
              do_something();
              }
              }

              This would be the equivelent JScript code.
              Maybe the FileExists function isn't global in JScript. I would expect
              it to be a method of the file system object.
              Maybe the "for(...in. ..)" will include too many properties, and
              it would be better to do
              for (var index = 0; index < colFolders.leng th ; i++) {
              That depends on how the colFolders object is designed. If it is an array,
              the simple version should work.

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • Stephen

                #8
                Re: if file exist

                zhisol wrote:[color=blue]
                > [...snip...]
                >
                > it's for web page. i mean exactly that:
                >
                > if("cover.jpg" exist)
                > document.write( '<img src='cover.jpg' >')
                > else
                > //display nothing
                >
                > and i don't know how can i realise "if("cover. jpg" exist)"
                >[/color]

                The HTTPRequest object could be useful to see if file is there or not.
                Combine this with the idea of starting out showing an "empty" image or
                maybe an invisible image.

                Send a HEAD request for the "cover.jpg" file. In the handler that is
                triggered when the response comes back, check for 200 response code &
                then make the image visible at that point.
                See:

                HTH
                Stephen
                [color=blue]
                > for example i have address: http://mysite.com/show.html
                > and if in root on server is a file "cover.jpg" , make this visable,
                > otherwise not.
                >
                > normally in html if "cover.jpg" doesn't exist - client see empty box, but
                > i want that he'll see nothing
                >
                > ps: sorry for my english:)[/color]

                Comment

                • Mark Space

                  #9
                  Re: if file exist

                  Lasse Reichstein Nielsen wrote:[color=blue]
                  > Mark Space <mark_space44@h otmail.com> writes:
                  > You can only use document.write on the client, so
                  > the check must be performed on the client and check the existence of
                  > a file on the server. For that, my answer is correct.[/color]

                  Oh, right. It's been a while since I've done this, I missed the
                  difference between document.write and response.write.
                  [color=blue]
                  >
                  > If you are talking server side scripting, which is probably ASP then,
                  > I would recommend an ASP-specific group, since it is not really about
                  > Javascript.[/color]

                  That was actually my next question. Which is better for server side
                  scripting, VBScript or Javascript? It seemed to me that Javascript
                  actually had more standards that recognized it, and I wondered if it was
                  more popular than MS only VBScript. I've only done VBScript so far.
                  (CGI like Perl or PHP really aren't an option for this project.)
                  [color=blue]
                  > That depends on how the colFolders object is designed. If it is an array,
                  > the simple version should work.[/color]

                  Just FYI, colFolders is a "collection ", which is a built in data type in
                  VBScript. It's kinda like an associative array. It associates
                  key/value data pairs.
                  [color=blue]
                  >
                  > /L[/color]

                  Um, shouldn't that be "</L>"? ;)



                  Comment

                  • Laurent Bugnion, GalaSoft

                    #10
                    Re: if file exist

                    Hi Mark,

                    Mark Space wrote:
                    [color=blue]
                    > Lasse Reichstein Nielsen wrote:[color=green]
                    >>
                    >> If you are talking server side scripting, which is probably ASP then,
                    >> I would recommend an ASP-specific group, since it is not really about
                    >> Javascript.[/color]
                    >
                    >
                    > That was actually my next question. Which is better for server side
                    > scripting, VBScript or Javascript? It seemed to me that Javascript
                    > actually had more standards that recognized it, and I wondered if it was
                    > more popular than MS only VBScript. I've only done VBScript so far.
                    > (CGI like Perl or PHP really aren't an option for this project.)[/color]

                    VBScript is probably more used than JavaScript on ASP. I don't know the
                    actual figures though. Most examples you will find on the Web for ASP
                    development are also written in VBScript.

                    Which doesn't matter much, actually, since the API is exactly the same
                    for VBScript and JScript (just like the API in .NET is exactly the same
                    if you programe in VB.NET or C#, or any other supported language).

                    For this reason, I rather recommend using JScript on ASP. It has the
                    great advantage to allow you to use the same language, same syntax for
                    server-side and client-side scripting. Besides, and though it's a
                    controversial point, the JScript syntax is C-like, and IMHO makes more
                    sense than the VB syntax. If you know JScript, you will feel comfortable
                    in C, C++, Java, C#. If you know VB, you'll feel comfortable in... VB ;-)

                    Of course it's a personal choice in the end. Technically, there are no
                    differences. It's more a question of gut feeling, and of what languages
                    you already know, and plan to use in the future.

                    HTH,

                    Laurent
                    (currently involved in a .NET project in VB.NET and C#, and constantly
                    reminded that C# is nicer ;-)
                    --
                    Laurent Bugnion, GalaSoft
                    Webdesign, Java, JavaScript: http://www.galasoft-LB.ch
                    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
                    Support children in Calcutta: http://www.calcutta-espoir.ch

                    Comment

                    • zhisol

                      #11
                      Re: if file exist

                      Stephen <ssansom@austin .rr.com> wrote in
                      news:oQ_ab.5535 8$834.54901@twi ster.austin.rr. com:
                      [color=blue]
                      > Send a HEAD request for the "cover.jpg" file. In the handler that is
                      > triggered when the response comes back, check for 200 response code &
                      > then make the image visible at that point.[/color]

                      <script language="JavaS cript">
                      var xmlHttp = new ActiveXObject(" Microsoft.XMLHT TP");
                      xmlHttp.open("H EAD", "cover.jpg" , false);
                      xmlHttp.send();
                      document.write( xmlHttp.statusT ext);
                      </script>

                      is that correct to see return code on the browser screen?
                      i see nothing :(
                      [color=blue]
                      > http://jibbering.com/2002/4/httprequest.html[/color]
                      it seems, this web page doesn't exist

                      --
                      pozdr;)
                      zhisol

                      Comment

                      • Stephen

                        #12
                        Re: if file exist

                        zhisol wrote:
                        [color=blue]
                        > Stephen <ssansom@austin .rr.com> wrote in
                        > news:oQ_ab.5535 8$834.54901@twi ster.austin.rr. com:
                        >
                        >[color=green]
                        >>Send a HEAD request for the "cover.jpg" file. In the handler that is
                        >>triggered when the response comes back, check for 200 response code &
                        >>then make the image visible at that point.[/color]
                        >
                        >
                        > <script language="JavaS cript">
                        > var xmlHttp = new ActiveXObject(" Microsoft.XMLHT TP");
                        > xmlHttp.open("H EAD", "cover.jpg" , false);
                        > xmlHttp.send();
                        > document.write( xmlHttp.statusT ext);
                        > </script>
                        >
                        > is that correct to see return code on the browser screen?
                        > i see nothing :(
                        >[/color]

                        Should work. Note:

                        xmlHttp.status should give, e.g., "200"
                        xmlHttp.statusT ext should give the associated text, e.g., "OK"

                        This works for me in my environment, just as you coded it above (except
                        for substituting an image name I know is on my system).

                        Server must be configured to allow HEAD requests. Most probably are, but
                        you'll have to make sure in your case.

                        You probably also want to check for other possible response codes that
                        indicate the requested entity is present, e.g., 304.

                        Regards,
                        Stephen
                        [color=blue]
                        >[color=green]
                        >>http://jibbering.com/2002/4/httprequest.html[/color]
                        >
                        > it seems, this web page doesn't exist
                        >[/color]

                        Comment

                        • Stephen

                          #13
                          Re: if file exist

                          zhisol wrote:
                          [color=blue]
                          > Stephen <ssansom@austin .rr.com> wrote in
                          > news:oQ_ab.5535 8$834.54901@twi ster.austin.rr. com:
                          >
                          >[color=green]
                          >>Send a HEAD request for the "cover.jpg" file. In the handler that is
                          >>triggered when the response comes back, check for 200 response code &
                          >>then make the image visible at that point.[/color]
                          >
                          >
                          > <script language="JavaS cript">
                          > var xmlHttp = new ActiveXObject(" Microsoft.XMLHT TP");
                          > xmlHttp.open("H EAD", "cover.jpg" , false);
                          > xmlHttp.send();
                          > document.write( xmlHttp.statusT ext);
                          > </script>
                          >
                          > is that correct to see return code on the browser screen?
                          > i see nothing :(
                          >[/color]

                          Should work. Note:

                          xmlHttp.status should give, e.g., "200"
                          xmlHttp.statusT ext should give the associated text, e.g., "OK"

                          This works for me in my environment, just as you coded it above (except
                          for substituting an image name I know is on my system).

                          Server must be configured to allow HEAD requests. Most probably are, but
                          you'll have to make sure in your case.

                          You probably also want to check for other possible response codes that
                          indicate the requested entity is present, e.g., 304.

                          Regards,
                          Stephen

                          P.S.: need document.close( ) after the document.write( ...)?? Or try
                          alert instead of document.write( )...?
                          S.
                          [color=blue]
                          >[color=green]
                          >>http://jibbering.com/2002/4/httprequest.html[/color]
                          >
                          > it seems, this web page doesn't exist
                          >[/color]


                          Comment

                          Working...