...

View Full Version : Search Subfolders



BillaBong
07-05-2012, 10:01 AM
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]

MarPlo
07-05-2012, 12:42 PM
Hi,
From what I know, i think that JavaScript cannot read the content of a folder, for security reasons.

Philip M
07-05-2012, 04:12 PM
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

Old Pedant
07-05-2012, 11:45 PM
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...


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum