javascript获得客户机目录
在javas
不说了直接上例子:
<div>
<div id="div_folders"></div>
<div id="div_files"></div>
</div>
<style>
#div_folders
{
border:1px solid #0000FF;
width:400px;
float:left;
}
#div_files
{
border:1px solid #00FF00;
float:left;
}
.folderLink
{
cursor:pointer;
border-bottom:1px solid #0033CC;
color:#0000FF;
}
</style>
<script>
//显示folder目录中的信息,如果folder是个不存在的目录显示我的电脑中的信息
function disFolder(folder)
{
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var div_folders=document.getElementById("div_folders");
div_folders.innerHTML="";
//获得当前目录的父目录
var parentFolder=folder.substring(0,folder.lastIndexOf("\\"));
if(objFSO.FolderExists(parentFolder))
{
//添加父目录链接
div_folders.appendChild(createFolderLink("..",parentFolder));
//添加子目录链接
var objRoot=objFSO.GetFolder(folder);
var colAllFolders = new Enumerator(objRoot.SubFolders);
for(;!colAllFolders.atEnd();colAllFolders.moveNext())
{
var objFolder1 = colAllFolders.item();
div_folders.appendChild(createFolderLink(objFolder1.Name,objFolder1.Path));
}
}
else
{
var colDrives = new Enumerator(objFSO.Drives);
for (;!colDrives.atEnd(); colDrives.moveNext())
{
objDrive = colDrives.item();
div_folders.appendChild(createFolderLink(objDrive.DriveLetter+":\\",objDrive.DriveLetter+":\\"));
}
return;
}
//显示文件
var colFiles = new Enumerator(objRoot.Files);
var div_files=document.getElementById("div_files");
div_files.innerHTML="<label style=color:#cccccc>"+objRoot.Path+"</label>";
for (; !colFiles.atEnd(); colFiles.moveNext())
{
objFile = colFiles.item()
div_files.appendChild(createFileLink(objFile.Name,objFile.Path));
}
}
//建立文件夹链接
function createFolderLink(name,path)
{
var div_folder=document.createElement("li");
div_folder.innerHTML=name;
div_folder.path=path;
div_folder.className="folderLink";
div_folder.attachEvent("on
return div_folder;
}
//文件链接点击事件
function div_on
{
var div_folder=window.event.srcElement;
disFolder(div_folder.path);
}
//建立文件链接
function createFileLink(name,path)
{
var div_file=document.createElement("div");
div_file.innerHTML=name;
div_file.path=path;
return div_file;
}
disFolder("忆风");
</script>
例中的两个div,一个显示文件夹,一个显示文件。
下面是对一些关于FileSystemObject的属性和方法的介绍和使用。
FileSystemObject简介及应用:
方法或者属性 描述
BuildPath()
生成一个文件路径
CopyFile() 复制文件
CopyFolder() 复制目录
CreateFolder() 创建新目录
CreateTextFile() 生成一个文件
DeleteFile() 删除一个文件
DeleteFolder() 删除一个目录
DriveExists() 检验盘符是否存在
Drives 返回盘符的集合
FileExists() 检验文件是否存在
FolderExists 检验一个目录是否存在
GetAbsolutePathName() 取得一个文件的绝对路径
GetBaseName() 取得文件名
GetDrive() 取得盘符名
GetDriveName() 取得盘符名
GetExtensionName() 取得文件的后缀
GetFile() 生成文件对象
GetFileName() 取得文件名
GetFolder() 取得目录对象
GetParentFolderName 取得文件或目录的父目录名
GetSpecialFolder() 取得特殊的目录名
GetTempName() 生成一个临时文件对象
MoveFile() 移动文件
MoveFolder() 移动目录
OpenTextFile() 打开一个文件流
//驱动盘的一些属性
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
var colDrives = new Enumerator(objFSO.Drives);
for (;!colDrives.atEnd(); colDrives.moveNext()) {
objDrive = colDrives.item();
document.write('DriveLetter: ' + objDrive.DriveLetter + '<BR>');
document.write('DriveType: ' + objDrive.DriveType + '<BR>');
if (objDrive.DriveType == 3)
if (objDrive.IsReady)
document.write('Remote drive with ShareName: ' +
objDrive.ShareName + '<BR>')
else
document.write('Remote drive - IsReady property returned False<BR><BR>');
else if (objDrive.IsReady) {
document.write('Local drive with VolumeName: ' +
objDrive.VolumeName + '<BR>');
document.write('FileSystem: ' + objDrive.FileSystem + '<BR>');
document.write('SerialNumber: ' + objDrive.SerialNumber + '<BR>');
document.write('AvailableSpace: ' + objDrive.AvailableSpace + ' bytes<BR>');
document.write('FreeSpace: ' + objDrive.FreeSpace + ' bytes<BR>');
document.write('TotalSize: ' + objDrive.TotalSize + ' bytes<P>');
}
}
//文件的属性
var colFiles = new Enumerator(objRoot.Files);
for (; !colFiles.atEnd(); colFiles.moveNext()) {
objFile = colFiles.item()
document.write(objFile.Path+'<br />');
/*document.write('Name:' + objFile.Name + ' <br />');
document.write('ShortName: ' + objFile.ShortName + ' ');
document.write('Size: ' + objFile.Size + ' bytes ');
document.write('Type: ' + objFile.Type + '<BR>');
document.write('Path: ' + objFile.Path + ' ');
document.write('ShortPath: ' + objFile.ShortPath + '<BR>');
document.write('Created: ' + objFile.DateCreated + ' ');
document.write('Accessed: ' + objFile.DateLastAccessed + ' ');
document.write('Modified: ' + objFile.DateLastModified + '<P>');
}
更多消息http://dhjdhja.blog.163.com/blog/