读书人

怎么通过vba获取一个目录下的所有文件

发布时间: 2012-02-23 22:01:35 作者: rapoo

如何通过vba获取一个目录下的所有文件夹名称、大小、文件夹中所有文档数量?
如何通过vba获取一个目录下的所有文件夹名称、大小、文件夹中所有文档数量?
例如:
vb-vba1
-1.txt
-2.txt
-vba2
-3.txt
-vba3

需要的结果是:
文件夹名称 文件夹大小 文件夹文档数量
vba1 2
vba2 1
vba3 0


[解决办法]
<hta>
<head>
<META HTTP-EQUIV= "pragma " CONTENT= "no-cache ">
<META HTTP-EQUIV= "Cache-Control " CONTENT= "no-cache, must-revalidate ">
<META HTTP-EQUIV= "expires " CONTENT= "0 ">
<title> 命名程序 </title>
</head>
<body scroll= "auto " style= "border:none; color:green; " bgcolor=black topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0 >
<textarea id=show rows=10 style= "width:100%; "> </textarea>
<textarea id=dofile rows=20 style= "width:100%; " title=请在这输入处理文件的方法> function (path){

请在这输入处理文件的方法,其它不变。

} </textarea>

源文件夹 <input id= "sourcefolder " type=file> <br> <input type=button value=获取全部文件夹 onclick= "getallfolder(sourcefolder.value) ">

<input type=button value=处理所有文件 onclick= "doallfile() ">
<input type=button value=清除 onclick= "showtxt( '已清除所有定时 '); clearTimeout(fsotime) ">
<SCRIPT language= "javascript ">
<!--

var fso = new ActiveXObject( "Scripting.FileSystemObject ");
var runtime = 1;
var foldersarray;
var fsotime;
var nowi;
var fp;
var theno;
var mydofile;

function showtxt(txt)
{
show.focus();
show.value += txt + "\n ";
}

function reportError(msg,url,line)
{
alert( "当前页的脚本发生错误: \n\n信息: " + msg + "\n\n行: " + line);
return true;
}

window.onerror = reportError;

function getallfolder(path)
{
 if ( fso.folderExists(path) == false)
   {
showtxt( "出错啦!\n[ " + path + "] 文件夹不存在! ");
return false;
}

if(fso.DriveExists(path))
{
path += "\\ ";
}

mygetfolder(path);
}

function mygetfolder(path){//自己递归时没参数

if(path != undefined)
{
clearTimeout(fsotime);
fp = null;
nowi = -1;
theno = 1;
foldersarray = new Array(fso.getFolder(path).path);
showtxt( "开始获取文件夹...\n " + (theno++) + ": " + fso.getFolder(path).path);
}

try
{
if(fp == null)


{
fp = new Enumerator(fso.getFolder(foldersarray[++nowi]).subFolders);
}
}catch(e)
{
showtxt( "取数组出错,原因是数组下标超出数组长度\n处理完成 ");
theno = fp = nowi = null;
return clearTimeout(fsotime);
}

if(!fp.atEnd())
{
showtxt( (theno++) + ": " + (foldersarray[foldersarray.length] = fp.item().path) );
fp.moveNext();
}
else
{
fp = null;
}

fsotime = setTimeout( "mygetfolder() ",runtime);

}

function doallfile(start){

try
{
foldersarray.length < 1;
}catch(e)
{
      return alert( "请先点[获取全部文件夹]获取要处理的文件夹对象 ");
}

if(start == undefined)
{
nowi = 0;
fp = null;
theno = 1;
try
{
eval( "mydofile = " + dofile.value);
}catch(e)
{
alert( "自定义方法出错,请重新编写!详细错误请瞧信息框 ");
showtxt(e.description);
}
}

if(fp == null)
{
try
{
fp = new Enumerator(fso.getfolder(foldersarray[nowi++]).files);
}catch(e)
{
showtxt( "处理文件完成 ");
theno = fp = nowi = null;
return clearTimeout(fsotime);
}
}

if(!fp.atEnd())
{
showtxt( (theno++) + ": " + fp.item().path );
try
{
mydofile( fp.item().path );
}catch(E)
{
showtxt( "执行自定义处理文件方法时出错: " + E.description);
}
fp.moveNext();
}
else
{
fp = null;
}

fsotime = setTimeout( "doallfile(1) ",runtime);

}

//-->
</SCRIPT>
</BODY>
</hta>

这个是我用JS写的,可以读所有的文件夹的文件 有一个接口给如何处理拿到的文件

读书人网 >VBA

热点推荐