Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Search Subfolders

    Hello Guys,

    I'm a total newbie into javascript and I was wondering if someone could help me solve this one, we have web search to find pdf files and dwf files it works. But we need it to search all the sub folders:

    This searchs for a file in a specific folder:
    FName = "C:/Users/deh/Desktop/DWF/" + szDir + "/" + mainForm.Path.value;

    The szDir is:
    szDir = szFile.substr(0,1);

    But could I get it to search the folder DWF + all subfolders for the file name?

    [CODE]

    <HTML>

    <HEAD>

    <meta name="TEST" content="NotePad">

    <TITLE>TEST Drawings viewer</TITLE>



    <SCRIPT LANGUAGE="JavaScript">

    <!--

    function LaunchWindow()

    {

    self.moveTo(0,0);

    self.resizeTo(screen.availWidth/3,screen.availHeight);

    posi = "left=" + screen.availWidth/3;

    posi = posi + ",top=0, width=";

    posi = posi + screen.availWidth/3*2;

    posi = posi + ",height=";

    posi = posi + screen.availHeight;

    szFile = mainForm.Path.value;

    szDir = szFile.substr(0,1);

    //Searchs on location + folder (ex. p01, searches in folder "p" for file "p01")

    FName = "C:/Users/deh/Desktop/DWF/" + szDir + "/" + mainForm.Path.value;

    fso = new ActiveXObject("Scripting.FileSystemObject");

    suffix="0";

    if (fso.FileExists(FName+".pdf")){

    suffix=".pdf";

    }

    if (fso.FileExists(FName+".dwf")){

    suffix=".dwf";

    }

    //alert(FName + suffix);

    if(suffix != 0){

    win = window.open(FName + suffix, "kim", posi, scrolling="auto", resize="Yes" );}

    else {

    win = window.open("NotFound.htm", "kim", posi, scrolling="auto", resize="Yes" );

    }

    }

    </SCRIPT>

    <script language="vbscript" id="clientEventHandlersVBS">

    Sub Window_OnLoad

    mainForm.Path.focus

    End Sub

    </SCRIPT>

    <style type="text/css">

    .style1 {

    font-family: Arial, Helvetica, sans-serif;

    }

    .style2 {

    font-size: x-small;

    }

    .style3 {

    font-family: Arial, Helvetica, sans-serif;

    font-size: x-small;

    }

    </style>

    </HEAD>

    <h3 class="style1">Tegnings viewer</h3>

    <p><span class="style3">Indtast tegningsnummer:</span><br>

    <BODY>

    <FORM onsubmit="LaunchWindow();" NAME="mainForm">

    <INPUT TYPE="TEXT" NAME="Path">

    <INPUT TYPE="submit" value= "Vis" id="Submit1">

    </FORM>

    <br>

    <span class="style1"><span class="style2">Indtast tegningsnummer, og tryk derefter på &quot;Enter&quot;

    <br>eller &quot;Vis&quot; knappen. </span></span> </p>

    <p>&nbsp;</p>

    <p>&nbsp;</p>

    <p class="style3">PRINT: Højreklik på tegning og vælg print</p>

    <p>&nbsp;</p>

    <p>&nbsp;</p>

    <p>&nbsp;</p>

    <p>&nbsp;</p>

    <p><img src="JW.png" width="212" height="40"></p>

    </body>

    </html>
    [CODE]

  • #2
    Regular Coder
    Join Date
    Mar 2011
    Posts
    148
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Hi,
    From what I know, i think that JavaScript cannot read the content of a folder, for security reasons.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by MarPlo View Post
    Hi,
    From what I know, i think that JavaScript cannot read the content of a folder, for security reasons.
    Correct. JavaScript running in a browser is a client-side language. You can not access the server's system from a client's browser. 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. And for security reasons JavaScript cannot access anything beyond the domain of the current page. This is known as the "same origin policy" and prevents a document or script loaded from one origin from getting or setting properties of a document from a different origin.

    Be aware that <script language=javascript> is long deprecated. Use <script type = "text/javascript"> instead (in fact also deprecated but still necessary for IE<9). The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    The Moving Finger writes; and, having writ,
    Moves on: nor all your piety nor wit
    Shall lure it back to cancel half a line,
    Nor all your tears wash out a word of it.

    -- Omar Khayyam
    Last edited by Philip M; 07-05-2012 at 03:46 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    All Philip said *WOULD* be true, except that BillaBong is using MSIE-only code and using an ActiveX control that *does* allow him to do what he wants to do.

    Yes, BillaBong, assuming this is for inTRAnet use only, you can easily do what you want to do. It's called "recursion".

    I do have to wonder, though: Since you have SOME of your scripting in VBScript, why not do it all in VBScript? But oh well...
    Code:
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var startPath = "C:\\Users\\deh\\Desktop\\DWF\\" + szDir + "\\";
    var startFile = mainForm.path.value;
    
    var foundFile = findFileIn( fso, startPath, startFile );
    if ( foundFile == null ) { foundFile = "NotFound.htm"; }
    win = window.open(foundFile, "kim", posi, scrolling="auto", resize="Yes" );}
    
    function findFileIn( path, name )
    {
        var fullName = path + name + ".pdf";
        if (fso.FileExists(fullName))
        {
            return fullName;
        }
        fullName = path + name + ".dwf";
        if (fso.FileExists(fullName))
        {
            return fullName;
        }
        var curfolder = fso.getFolder(path);
        var fc = new Enumerator(curfolder.SubFolders);
        var   s = "";
        for (; !fc.atEnd(); fc.moveNext())
        {
            var subfPath = fc.item().Path + "\\";
            var subName = findFileIn( subfPath, name )
            if ( subName != "" ) return subName
        }
        return null;
    }
    Untested, especially the enumerator usage, but I think it is correct.

    But easier to do in VBScript.
    Last edited by Old Pedant; 07-05-2012 at 10:56 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •