allfiles.vbs 显示子目录下的所有文件的修改时间、大小、文件名、扩展名等

这篇文章主要介绍了allfiles.vbs之显示子目录下的所有文件的修改时间、大小、全限定名定名等信息,需要的朋友可以参考下

有的时候将子目录下的所有文件的修改时间、大小、全限定名等信息导出到Excel表格中。

尝试过命令行,但不太好用――

1.对于“dir /s >1.txt”,当前目录与文件列表是分开显示的,合并起来太麻烦,而且没有文件的全限定名。
2.对于“dir /b /s >1.txt”,只有全限定名,没有修改时间、大小等详细信息。
3.对于“tree /f >1.txt”,只有目录树,没有修改时间、大小等详细信息。

在网上找了几个导出文件列表的工具,但都不太好用。于是决定自己编写。

用什么编程工具开发呢?考虑到以后可能经常改进输出内容的格式,所以用VBScript脚本来写是最方便的。

全部代码如下――

 ' allfiles.vbs: 显示子目录下的所有文件的修改时间、大小、全限定名等信息。输出文件版. ' Author: zyl910 ' Blog: http://www.cnblogs.com/zyl910 ' URL: http://www.cnblogs.com/zyl910/archive/2013/01/07/allfiles.html ' Version: V1.0 ' Updata: 2013-01-07 ' ' 输出文件是“allfiles.txt”。格式: ' Type  DateLastModified  Size  Base  Ext  FullName ' D  2013-1-1 12:30:30    Temp    C:\Temp ' F  2013-1-1 12:30:31  34  abc  txt  C:\Temp\abc.txt ' ' Type: 类型。D目录,F文件。 ' DateLastModified: 最后修改时间. ' Size: 文件大小. ' Base: 文件基本名. ' Ext: 扩展名. ' FullName: 文件的全限定名. ' 取得文件扩展名和基本名. Function GetFileExtAndBaseName(ByVal sfilename, ByRef sbasename) n = InStrRev(sfilename, ".") If n>1 Then GetFileExtAndBaseName = Mid(sfilename, n+1) sbasename = Left(sfilename, n-1) Else GetFileExtAndBaseName = "" sbasename = sfilename End If End Function ' 遍历该目录及子目录. ' ' Result: 目录和文件的总数. ' fileOut: 输出文件,用于输出遍历结果. ' fso: FileSystemObject对象. ' sPath: 目录. Function dirscan(ByRef fileOut, ByVal fso, ByVal sPath) rt = 0 Set currentFolder = Nothing 'MsgBox sPath On Error Resume Next Set currentFolder = fso.GetFolder(sPath) On Error Goto 0 If Not (currentFolder Is Nothing) Then ' Folders For Each subFolder in currentFolder.SubFolders sfull = subFolder.Path & "\"  ' 全限定名. s = "D" & vbTab & subFolder.DateLastModified & vbTab & "" & vbTab & subFolder.Name & vbTab & "" & vbTab & sfull & vbCrLf fileOut.write s rt = rt + 1 rt = rt + dirscan(fileOut, fso, subFolder.Path) Next ' Files For Each f in currentFolder.Files sbase = "" sext = GetFileExtAndBaseName(f.Name, sbase)  ' 扩展名. sfull = f.Path  ' 全限定名. s = "F" & vbTab & f.DateLastModified & vbTab & f.Size & vbTab & sbase & vbTab & sext & vbTab & sfull & vbCrLf fileOut.write s rt = rt + 1 Next End If dirscan = rt End Function '得到脚本文件所在的当前目录 Function GetCurrentFolderFullPath(fso) GetCurrentFolderFullPath = fso.GetParentFolderName(WScript.ScriptFullName) End Function ' 测试 Sub dotest Set fso = CreateObject("Scripting.FileSystemObject") strpath = GetCurrentFolderFullPath(fso)  ' 得到当前目录. Set FileObj = fso.opentextfile(strpath+"\allfiles.txt", 2, True, -1)  ' 打开输出文件. ForWriting, TristateTrue. s = "Type" & vbTab & "DateLastModified" & vbTab & "Size" & vbTab & "Base" & vbTab & "Ext" & vbTab & "FullName" & vbCrLf  ' 格式说明. FileObj.write s  ' 写入格式说明. cnt = dirscan(FileObj, fso, strpath)  ' 遍历目录及子目录. FileObj.Close  ' 关闭输出文件. MsgBox "OK! " & cnt & " items.", vbOKOnly, "allfiles" End Sub ' Run Call dotest()

将上面的代码复制到记事本,并保存为“allfiles.vbs”。然后将其复制到欲导出的目录,双击运行,该脚本便会将子目录下的所有文件信息导出到“allfiles.txt”中。因为该文本文件是以Tab分隔各列的,能很方便的复制到Excel中。

例如该脚本对VC2005的include目录的输出结果为――

Type    DateLastModified    Size    Base    Ext    FullName
D    2011-9-5 13:16:14        CodeAnalysis        C:\VS8_2005\VC\include\CodeAnalysis\
F    2006-12-1 22:16:08    5720    sourceannotations    h    C:\VS8_2005\VC\include\CodeAnalysis\sourceannotations.h
F    2005-11-11 22:52:36    866    Warnings    h    C:\VS8_2005\VC\include\CodeAnalysis\Warnings.h
D    2011-9-5 13:16:07        msclr        C:\VS8_2005\VC\include\msclr\
D    2011-9-5 13:16:07        com        C:\VS8_2005\VC\include\msclr\com\
F    2006-12-1 22:54:28    8078    ptr    h    C:\VS8_2005\VC\include\msclr\com\ptr.h
F    2005-11-11 22:52:36    585    all    h    C:\VS8_2005\VC\include\msclr\all.h
F    2006-12-1 22:54:28    125137    appdomain    h    C:\VS8_2005\VC\include\msclr\appdomain.h
F    2005-11-11 22:52:36    6155    auto_gcroot    h    C:\VS8_2005\VC\include\msclr\auto_gcroot.h
F    2005-11-11 22:52:36    4098    auto_handle    h    C:\VS8_2005\VC\include\msclr\auto_handle.h
F    2005-11-11 22:52:36    2504    event    h    C:\VS8_2005\VC\include\msclr\event.h
F    2005-11-11 22:52:36    3958    gcroot    h    C:\VS8_2005\VC\include\msclr\gcroot.h
F    2005-11-11 22:52:36    8012    lock    h    C:\VS8_2005\VC\include\msclr\lock.h
F    2005-11-11 22:52:36    1257    safebool    h    C:\VS8_2005\VC\include\msclr\safebool.h
D    2011-9-5 11:55:28        sys        C:\VS8_2005\VC\include\sys\
F    2005-11-11 22:52:36    997    locking    h    C:\VS8_2005\VC\include\sys\locking.h
F    2005-11-11 22:52:36    6969    stat    h    C:\VS8_2005\VC\include\sys\stat.h
F    2005-11-11 22:52:36    1856    stat    inl    C:\VS8_2005\VC\include\sys\stat.inl
F    2005-11-11 22:52:36    3340    timeb    h    C:\VS8_2005\VC\include\sys\timeb.h
F    2005-11-11 22:52:36    1414    timeb    inl    C:\VS8_2005\VC\include\sys\timeb.inl
F    2005-11-11 22:52:38    2150    types    h    C:\VS8_2005\VC\include\sys\types.h
以上就是allfiles.vbs 显示子目录下的所有文件的修改时间、大小、文件名、扩展名等的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 脚本专栏