PDA

Ver la Versión Completa : Buscador de archivos remoto


Carlex
28-10-2005, 17:36:11
Muy buenas, estoy recien metiendome a fondo en este tema del javascript y me aparecio un problema, encontre el sgte codigo:


<HTML>
<head>
<TITLE>Search... with sub-folders!</TITLE>
</head>

<style type="text/css">

.formItem {
color: #000000;
border: 1px solid #aaaaaa;
background-color: #eeeeee;
}

.find {
color: #0000ff;
font: 10px Arial;
}

.title {
background-color: #dddddd;
color: #000000;
font: 12px arial;
font-weight: bold;
text-align: center;
}

A {
color: blue;
text-decoration: none;
}

A:hover {
text-decoration: underline;
}

</STYLE>

<script LANGUAGE="JavaScript">

// Establish a few environment variables.
var fso = new ActiveXObject( "Scripting.FileSystemObject" );
var result = new String( );
var FileName = new String( );
var Extention = new String( );


// Converts file & folder byte size values to computer
// metric (bytes, KB, MB, GB or TB). returns a string.
function toMetric( bytes ) {

// Check for Terabytes (TB).
if( bytes >= 1099511627776 ) { return ( Math.floor( bytes / 1099511627776 ) + ' TB' ); }

// Check for Gigabytes (GB).
if( bytes >= 1073741824 ) { return ( Math.floor( bytes / 1073741824 ) + ' GB' ); }

// Check for Megabytes (MB).
if( bytes >= 1048576 ) { return ( Math.floor( bytes / 1048576 ) + ' MB' ); }

// Check for Kilobytes (KB).
if( bytes >= 1024 ) { return ( Math.floor( bytes / 1024 ) + ' KB' ); }

// The file is less than one KB, just return size as 1 KB like Windows does.
return '1 KB';
}


// Show the contents of a clicked sub-folder.
function subFolder( path ) {

// Update the txtPath field with the new search folder.
frmSearch.txtPath.value = unescape( path );

// Restart a new search with the new folder.
scan( );
}



// Scans the given path for files matching the given mask.
function FindFile( searchPath ) {

// Extablish a table color toggle.
var toggle = true;

// If chkShowFolders is checked, display the sub-folders.
if( frmSearch.chkShowFolders.checked ) {

// Check to see if the current folder is the drive root.
if( fso.GetParentFolderName( frmSearch.txtPath.value ).length > 0 ) {

// Add the parent folder to the table result string.
result +=
'<tr' + ( ( toggle ) ? '' : ' BGCOLOR="#f0f0f0"' ) + '>' +
'<td NOWRAP><FONT CLASS="find"> <A HREF="#" onclick="subFolder( \'' +
escape( fso.GetParentFolderName( frmSearch.txtPath.value ) ) +
'\' ); return false;">..</A> </FONT></td>' +
'<td NOWRAP><FONT CLASS="find"> Parent folder </FONT></td>' +
'<td NOWRAP ALIGN="right"><FONT CLASS="find"> </FONT></td></tr>';

// Toggle the color toggle variable.
toggle = !toggle;
}

// Establish enumerator to step from folder to folder in the SubFolders collection.
var folderEnum = new Enumerator( searchPath.SubFolders );

// Iterate through the folders in the collection.
for( var i = 0; !folderEnum.atEnd( ); folderEnum.moveNext( ) ) {

// Use a variable to hold the current file object to shorten the code below.
var folder = folderEnum.item( );

// Add the folder to the table result string.
result +=
'<tr' + ( ( toggle ) ? '' : ' BGCOLOR="#f0f0f0"' ) + '>' +
'<td NOWRAP><FONT CLASS="find"> <A HREF="#" ' +
'onclick="subFolder( \'' + escape( folder.Path ) + '\' ); return false;">' + folder.name +
'</A> </FONT></td>' +
'<td NOWRAP><FONT CLASS="find"> ' + folder.type + ' </FONT></td>' +
'<td NOWRAP ALIGN="right"><FONT CLASS="find"> </FONT></td></tr>';

// Toggle the color toggle variable.
toggle = !toggle;
}
}

// Establish enumerator to step from item to item in the folder contents.
var fileEnum = new Enumerator( searchPath.Files );

// Iterate through the files in the collection. Scan for files fitting the file mask.
for( var i = 0; !fileEnum.atEnd( ); fileEnum.moveNext( ) ) {

// Use a variable to hold the current file object to shorten the code below.
var file = fileEnum.item( );

// Validate current file against search filename parameter.
if( FileName == "*" || file.name.slice( 0,
file.name.lastIndexOf( "." ) ).toLowerCase( ).indexOf( FileName ) > -1 ) {

// Validate current file extention against search file extention parameter.
if( Extention == "*" || file.name.slice(
file.name.lastIndexOf( "." ) + 1 ).toLowerCase( ).indexOf( Extention ) > -1 ) {

// Add the file to the table result string.
result +=
'<tr' + ( ( toggle ) ? '' : ' BGCOLOR="#f0f0f0"' ) + '>' +
'<td NOWRAP><FONT CLASS="find"> <A TARGET="_blank" HREF="' +
file.Path + '">' + file.name +
'</A> </FONT></td>' +
'<td NOWRAP><FONT CLASS="find"> ' + file.type + ' </FONT></td>' +
'<td NOWRAP ALIGN="right"><FONT CLASS="find"> ' +
toMetric( file.size ) + ' </FONT></td></tr>';

// Toggle the color toggle variable.
toggle = !toggle;
}
}
}
}



// Validates path and filename and initiates the file scan.
function scan( ) {

// Parse filename and extention from the given mask.
FileName = ( frmSearch.txtMask.value.lastIndexOf( "." ) > -1 ) ?
frmSearch.txtMask.value.slice( 0, frmSearch.txtMask.value.lastIndexOf( "." ) ) :
( frmSearch.txtMask.value.length > 0 ) ? frmSearch.txtMask.value.toLowerCase( ) : "*";
Extention = ( frmSearch.txtMask.value.lastIndexOf( "." ) > -1 ) ?
frmSearch.txtMask.value.slice( frmSearch.txtMask.value.lastIndexOf( "." ) + 1 ).toLowerCase( ) : "*";

// Validate the given path.
if( frmSearch.txtPath.value.length > 0 && fso.FolderExists( frmSearch.txtPath.value ) ) {

// Path exists. Generate table headder.
result =
'<table BORDER="0" WIDTH="100%" CELLPADDING="0"><tr>' +
'<td WIDTH="60%" CLASS="title">Name</td>' +
'<td WIDTH="25%" CLASS="title">Type</td>' +
'<td WIDTH="15%" CLASS="title">Size</td></tr>';

// Collect valid filenames.
FindFile( fso.GetFolder( frmSearch.txtPath.value ) );

// Close and display search results table.
outPut.innerHTML = result + "</table>";

} else {

// Path is invalid. Alert user.
alert( "Please enter a valid Path before proceeding." );
}
}

</script>

<body onload="frmSearch.txtMask.focus( ); frmSearch.txtMask.select( )"
BGCOLOR="#ffffff" TOPMARGIN="0" LEFTMARGIN="0">

<form ID="frmSearch" NAME="frmSearch">

<table BORDER="0" CELLPADDING="0" STYLE="border-collapse: collapse;" CELLPADDING="2">
<tr>
<td><FONT FACE="Arial" SIZE="2"><B> Mask : </B></FONT></td>
<td><input TYPE="text" VALUE="*.*" ID="txtMask" NAME="txtMask" CLASS="formItem" STYLE="width:600;"></td>
</tr>
<tr>
<td><FONT FACE="Arial" SIZE="2"><B> Path : </B></FONT></td>
<td><input TYPE="text" VALUE="C:\" ID="txtPath" NAME="txtPath" CLASS="formItem" STYLE="width:600;"></td>
</tr>
<tr>
<td> </td>
<td>
<input TYPE="submit" VALUE="Search" CLASS="formItem" STYLE="width:150;"
onclick="scan( ); frmSearch.txtMask.focus( ); frmSearch.txtMask.select( ); return false;">

<input TYPE="checkbox" CHECKED ID="chkShowFolders" NAME="chkShowFolders"
><LABEL FOR="chkShowFolders">Show sub-folders</LABEL>
</td>
</tr>
<tr>
<td COLSPAN="2">
<BR> <FONT FACE="arial" SIZE="2"><B> Search Result: </B></FONT>
<HR>
<DIV ID="outPut"></DIV>
</td>
</tr>
</table>

</form>

</body>
</HTML>


Lo arregle a mi gusto para q busque solo en una determinada carpeta pero al ponerlo en linea no busca d manera remota solo local, alguien me puede dar una idea como modificar este codigo para q busque en el servidor y no asi en el cliente, esto lo hago para q sea mas facil subir archivos al server y no tener q meterlos a una base d datos....help

jachguate
28-10-2005, 17:50:46
Hola.

A menos que estes intentando implementar algo de AJAX (cosa que no creo), veo que tenes un problema de concepto aqui:

javascript se ejecuta en el cliente, por tanto, no tiene forma de saber lo que hay en el servidor. Bueno, si la tiene, pero será mucho mas complicado: podes conectarte via ftp a tu servidor y obtener la lista de carpetas, por ejemplo.

Si queres una lista de carpetas del servidor, será mejor que realices un script del lado del servidor (por ejemplo con php) o un cgi (por ejemplo con delphi) que te devuelva la lista de carpetas disponibles.

Hasta luego.

;)