站长资源脚本专栏

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

整理:jimmy2024/12/28浏览2
简介有的时候将子目录下的所有文件的修改时间、大小、全限定名等信息导出到Excel表格中。尝试过命令行,但不太好用——1.对于“dir /s >1.txt”,当前目录与文件列表是分开显示的,合并起来太麻烦,而且没有文件的全限定名。2.对于“dir /b /s >1.txt”,只有全限定名,没有修改时

"htmlcode">

' 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
F    2005-11-11 22:52:38    4006    utime    h    C:\VS8_2005\VC\include\sys\utime.h
F    2005-11-11 22:52:38    2881    utime    inl    C:\VS8_2005\VC\include\sys\utime.inl
F    2005-11-11 22:52:38    1959    wstat    inl    C:\VS8_2005\VC\include\sys\wstat.inl
F    2006-12-1 22:54:24    191593    algorithm        C:\VS8_2005\VC\include\algorithm
F    2013-1-7 21:25:47    0    allfiles    txt    C:\VS8_2005\VC\include\allfiles.txt
F    2013-1-7 21:25:11    2730    allfiles    vbs    C:\VS8_2005\VC\include\allfiles.vbs
F    2005-11-11 22:52:24    689    assert    h    C:\VS8_2005\VC\include\assert.h
F    2005-11-11 22:52:24    13925    bitset        C:\VS8_2005\VC\include\bitset
F    2005-11-11 22:52:24    223    cassert        C:\VS8_2005\VC\include\cassert
F    2005-11-11 22:52:24    1050    cctype        C:\VS8_2005\VC\include\cctype
F    2005-11-11 22:52:24    694    cerrno        C:\VS8_2005\VC\include\cerrno
F    2005-11-11 22:52:24    296    cfloat        C:\VS8_2005\VC\include\cfloat
F    2005-11-11 22:52:24    301    ciso646        C:\VS8_2005\VC\include\ciso646
F    2005-11-11 22:52:24    336    climits        C:\VS8_2005\VC\include\climits
F    2005-11-11 22:52:24    698    clocale        C:\VS8_2005\VC\include\clocale
F    2005-11-11 22:52:24    1553    cmath        C:\VS8_2005\VC\include\cmath
F    2006-12-1 23:07:20    8949    comdef    h    C:\VS8_2005\VC\include\comdef.h
F    2005-11-11 22:52:24    79172    comdefsp    h    C:\VS8_2005\VC\include\comdefsp.h
F    2005-11-11 22:52:24    27097    comip    h    C:\VS8_2005\VC\include\comip.h
F    2005-11-11 22:52:24    28821    complex        C:\VS8_2005\VC\include\complex
F    2005-11-11 22:52:24    58427    comutil    h    C:\VS8_2005\VC\include\comutil.h
F    2005-11-11 22:52:24    8895    conio    h    C:\VS8_2005\VC\include\conio.h
F    2006-12-1 22:54:26    646    crtassem    h    C:\VS8_2005\VC\include\crtassem.h
F    2006-12-1 22:54:26    38386    crtdbg    h    C:\VS8_2005\VC\include\crtdbg.h
F    2006-12-1 22:54:26    93735    crtdefs    h    C:\VS8_2005\VC\include\crtdefs.h
F    2005-11-11 22:52:24    2183    crtwrn    h    C:\VS8_2005\VC\include\crtwrn.h
F    2005-11-11 22:52:24    883    csetjmp        C:\VS8_2005\VC\include\csetjmp
F    2005-11-11 22:52:24    610    csignal        C:\VS8_2005\VC\include\csignal
F    2005-11-11 22:52:24    574    cstdarg        C:\VS8_2005\VC\include\cstdarg
F    2005-11-11 22:52:24    592    cstddef        C:\VS8_2005\VC\include\cstddef
F    2005-11-11 22:52:24    1514    cstdio        C:\VS8_2005\VC\include\cstdio
F    2005-11-11 22:52:24    1045    cstdlib        C:\VS8_2005\VC\include\cstdlib
F    2005-11-11 22:52:26    947    cstring        C:\VS8_2005\VC\include\cstring
F    2005-11-11 22:52:26    758    ctime        C:\VS8_2005\VC\include\ctime
F    2005-11-11 22:52:26    19257    ctype    h    C:\VS8_2005\VC\include\ctype.h
F    2005-11-11 22:52:26    1621    cwchar        C:\VS8_2005\VC\include\cwchar
F    2005-11-11 22:52:26    1266    cwctype        C:\VS8_2005\VC\include\cwctype
F    2005-11-11 22:21:32    5125    dbgautoattach    h    C:\VS8_2005\VC\include\dbgautoattach.h
F    2005-11-11 22:52:26    15385    delayhlp    cpp    C:\VS8_2005\VC\include\delayhlp.cpp
F    2005-11-11 22:52:26    4350    delayimp    h    C:\VS8_2005\VC\include\delayimp.h
F    2005-11-11 22:52:26    38621    deque        C:\VS8_2005\VC\include\deque
F    2005-11-11 22:52:26    4463    direct    h    C:\VS8_2005\VC\include\direct.h
F    2005-11-11 22:52:26    2027    dos    h    C:\VS8_2005\VC\include\dos.h
F    2006-12-1 22:54:26    43278    dvec    h    C:\VS8_2005\VC\include\dvec.h
F    2005-11-11 22:52:26    2997    eh    h    C:\VS8_2005\VC\include\eh.h
F    2005-11-11 22:52:26    14047    emmintrin    h    C:\VS8_2005\VC\include\emmintrin.h
F    2005-11-11 22:52:26    2279    errno    h    C:\VS8_2005\VC\include\errno.h
F    2006-12-1 22:54:26    10407    exception        C:\VS8_2005\VC\include\exception
F    2005-11-11 22:52:26    3036    excpt    h    C:\VS8_2005\VC\include\excpt.h
F    2005-11-11 22:52:26    2646    fcntl    h    C:\VS8_2005\VC\include\fcntl.h
F    2005-11-11 22:52:26    13096    float    h    C:\VS8_2005\VC\include\float.h
F    2005-11-11 22:52:26    7619    fpieee    h    C:\VS8_2005\VC\include\fpieee.h
F    2005-11-11 22:52:26    30192    fstream        C:\VS8_2005\VC\include\fstream
F    2005-11-11 22:52:26    20758    functional        C:\VS8_2005\VC\include\functional
F    2006-12-1 22:54:26    16955    fvec    h    C:\VS8_2005\VC\include\fvec.h
F    2005-11-11 22:52:26    1396    gcroot    h    C:\VS8_2005\VC\include\gcroot.h
F    2005-11-11 22:52:26    9631    hash_map        C:\VS8_2005\VC\include\hash_map
F    2005-11-11 22:52:26    8349    hash_set        C:\VS8_2005\VC\include\hash_set
F    2006-12-1 22:54:26    80350    intrin    h    C:\VS8_2005\VC\include\intrin.h
F    2005-11-11 22:52:28    1586    invkprxy    h    C:\VS8_2005\VC\include\invkprxy.h
F    2005-11-11 22:52:28    16413    io    h    C:\VS8_2005\VC\include\io.h
F    2005-11-11 22:52:28    2909    iomanip        C:\VS8_2005\VC\include\iomanip
F    2005-11-11 22:52:28    8146    ios        C:\VS8_2005\VC\include\ios
F    2005-11-11 22:52:28    23755    iosfwd        C:\VS8_2005\VC\include\iosfwd
F    2005-11-11 22:52:28    2101    iostream        C:\VS8_2005\VC\include\iostream
F    2005-11-11 22:52:28    561    iso646    h    C:\VS8_2005\VC\include\iso646.h
F    2006-12-1 22:54:28    32646    istream        C:\VS8_2005\VC\include\istream
F    2006-12-1 22:54:28    14517    iterator        C:\VS8_2005\VC\include\iterator
F    2005-11-11 22:52:28    33146    ivec    h    C:\VS8_2005\VC\include\ivec.h
F    2005-11-11 22:52:28    29325    limits        C:\VS8_2005\VC\include\limits
F    2005-11-11 22:52:28    4678    limits    h    C:\VS8_2005\VC\include\limits.h
F    2006-12-1 22:54:28    35936    list        C:\VS8_2005\VC\include\list
F    2005-11-12 0:20:20    2425    listing    inc    C:\VS8_2005\VC\include\listing.inc
F    2005-11-11 22:52:28    8068    locale        C:\VS8_2005\VC\include\locale
F    2005-11-11 22:52:28    3714    locale    h    C:\VS8_2005\VC\include\locale.h
F    2006-12-1 22:54:28    10463    malloc    h    C:\VS8_2005\VC\include\malloc.h
F    2005-11-11 22:52:28    10022    map        C:\VS8_2005\VC\include\map
F    2006-12-1 22:54:28    23670    math    h    C:\VS8_2005\VC\include\math.h
F    2005-11-11 22:52:28    5666    mbctype    h    C:\VS8_2005\VC\include\mbctype.h
F    2006-12-1 22:54:28    31431    mbstring    h    C:\VS8_2005\VC\include\mbstring.h
F    2006-12-1 22:54:28    29695    memory        C:\VS8_2005\VC\include\memory
F    2005-11-11 22:52:30    2888    memory    h    C:\VS8_2005\VC\include\memory.h
F    2005-11-11 22:52:30    456    minmax    h    C:\VS8_2005\VC\include\minmax.h
F    2005-11-11 22:52:30    1663    mm3dnow    h    C:\VS8_2005\VC\include\mm3dnow.h
F    2005-11-11 22:52:30    6413    mmintrin    h    C:\VS8_2005\VC\include\mmintrin.h
F    2005-11-11 22:52:30    3276    new        C:\VS8_2005\VC\include\new
F    2005-11-11 22:52:30    3812    new    h    C:\VS8_2005\VC\include\new.h
F    2006-12-1 22:54:28    27362    numeric        C:\VS8_2005\VC\include\numeric
F    2005-11-11 22:52:30    5310    omp    h    C:\VS8_2005\VC\include\omp.h
F    2006-12-1 22:54:28    29058    ostream        C:\VS8_2005\VC\include\ostream
F    2005-9-27 20:49:22    110287    penwin    h    C:\VS8_2005\VC\include\penwin.h
F    2005-11-11 22:52:30    527    pgobootrun    h    C:\VS8_2005\VC\include\pgobootrun.h
F    2005-11-11 22:52:30    12546    process    h    C:\VS8_2005\VC\include\process.h
F    2005-11-11 22:52:30    6374    queue        C:\VS8_2005\VC\include\queue
F    2005-11-11 22:52:30    5418    rtcapi    h    C:\VS8_2005\VC\include\rtcapi.h
F    2005-11-11 22:52:30    45876    sal    h    C:\VS8_2005\VC\include\sal.h
F    2005-11-11 22:52:30    6283    search    h    C:\VS8_2005\VC\include\search.h
F    2005-11-11 22:52:30    8945    set        C:\VS8_2005\VC\include\set
F    2005-11-11 22:52:30    6208    setjmp    h    C:\VS8_2005\VC\include\setjmp.h
F    2005-11-11 22:52:30    849    setjmpex    h    C:\VS8_2005\VC\include\setjmpex.h
F    2005-11-11 22:52:30    899    share    h    C:\VS8_2005\VC\include\share.h
F    2005-11-11 22:52:30    3251    signal    h    C:\VS8_2005\VC\include\signal.h
F    2005-11-12 0:20:26    14733    srv    h    C:\VS8_2005\VC\include\srv.h
F    2005-11-11 22:52:30    16632    sstream        C:\VS8_2005\VC\include\sstream
F    2005-11-11 22:52:30    3687    stack        C:\VS8_2005\VC\include\stack
F    2005-11-11 22:52:30    659    stdarg    h    C:\VS8_2005\VC\include\stdarg.h
F    2005-11-11 22:52:32    1607    stddef    h    C:\VS8_2005\VC\include\stddef.h
F    2005-11-11 22:52:32    6475    stdexcept        C:\VS8_2005\VC\include\stdexcept
F    2005-11-11 22:52:32    555    stdexcpt    h    C:\VS8_2005\VC\include\stdexcpt.h
F    2006-12-1 22:54:28    46583    stdio    h    C:\VS8_2005\VC\include\stdio.h
F    2006-12-1 22:54:28    47898    stdlib    h    C:\VS8_2005\VC\include\stdlib.h
F    2005-11-11 22:52:32    11931    streambuf        C:\VS8_2005\VC\include\streambuf
F    2005-11-11 22:52:32    21939    string        C:\VS8_2005\VC\include\string
F    2006-12-1 22:54:28    26938    string    h    C:\VS8_2005\VC\include\string.h
F    2005-11-11 22:52:32    17940    strstream        C:\VS8_2005\VC\include\strstream
F    2005-11-11 22:52:32    3825    swprintf    inl    C:\VS8_2005\VC\include\swprintf.inl
F    2006-12-1 22:54:28    99143    tchar    h    C:\VS8_2005\VC\include\tchar.h
F    2005-11-11 22:52:32    11837    time    h    C:\VS8_2005\VC\include\time.h
F    2005-11-11 22:52:32    3730    time    inl    C:\VS8_2005\VC\include\time.inl
F    2005-11-11 22:52:32    5157    typeinfo        C:\VS8_2005\VC\include\typeinfo
F    2005-11-11 22:52:32    999    typeinfo    h    C:\VS8_2005\VC\include\typeinfo.h
F    2005-11-11 22:52:32    3982    use_ansi    h    C:\VS8_2005\VC\include\use_ansi.h
F    2005-11-11 22:52:32    4719    utility        C:\VS8_2005\VC\include\utility
F    2005-11-11 22:52:32    4032    vadefs    h    C:\VS8_2005\VC\include\vadefs.h
F    2005-11-11 22:52:32    42003    valarray        C:\VS8_2005\VC\include\valarray
F    2005-11-11 22:52:32    3730    varargs    h    C:\VS8_2005\VC\include\varargs.h
F    2005-11-11 22:52:32    1405    vcclr    h    C:\VS8_2005\VC\include\vcclr.h
F    2005-11-11 22:52:32    62008    vector        C:\VS8_2005\VC\include\vector
F    2006-12-1 22:54:28    74974    wchar    h    C:\VS8_2005\VC\include\wchar.h
F    2005-11-11 22:52:34    6705    wctype    h    C:\VS8_2005\VC\include\wctype.h
F    2005-11-11 23:44:36    29082    wmiatlprov    h    C:\VS8_2005\VC\include\wmiatlprov.h
F    2005-11-11 22:52:34    1381    wtime    inl    C:\VS8_2005\VC\include\wtime.inl
F    2005-11-11 22:52:34    27165    xcomplex        C:\VS8_2005\VC\include\xcomplex
F    2005-11-11 22:52:34    4717    xdebug        C:\VS8_2005\VC\include\xdebug
F    2006-12-1 22:54:28    21142    xhash        C:\VS8_2005\VC\include\xhash
F    2005-11-11 22:52:34    19697    xiosbase        C:\VS8_2005\VC\include\xiosbase
F    2005-11-11 22:52:34    77261    xlocale        C:\VS8_2005\VC\include\xlocale
F    2005-11-11 22:52:34    7701    xlocinfo        C:\VS8_2005\VC\include\xlocinfo
F    2005-11-11 22:52:34    4614    xlocinfo    h    C:\VS8_2005\VC\include\xlocinfo.h
F    2005-11-11 22:52:34    3841    xlocmes        C:\VS8_2005\VC\include\xlocmes
F    2005-11-11 22:52:34    27813    xlocmon        C:\VS8_2005\VC\include\xlocmon
F    2006-12-1 22:54:28    44597    xlocnum        C:\VS8_2005\VC\include\xlocnum
F    2005-11-11 22:52:34    21103    xloctime        C:\VS8_2005\VC\include\xloctime
F    2005-11-11 22:52:34    4360    xmath    h    C:\VS8_2005\VC\include\xmath.h
F    2005-11-11 22:52:34    7218    xmemory        C:\VS8_2005\VC\include\xmemory
F    2005-11-11 22:52:34    18149    xmmintrin    h    C:\VS8_2005\VC\include\xmmintrin.h
F    2005-11-11 22:52:34    2311    xstddef        C:\VS8_2005\VC\include\xstddef
F    2006-12-1 22:54:28    66005    xstring        C:\VS8_2005\VC\include\xstring
F    2005-11-11 22:52:34    41252    xtree        C:\VS8_2005\VC\include\xtree
F    2006-12-1 22:54:28    123607    xutility        C:\VS8_2005\VC\include\xutility
F    2005-11-11 22:52:36    2364    ymath    h    C:\VS8_2005\VC\include\ymath.h
F    2006-12-1 22:54:28    21162    yvals    h    C:\VS8_2005\VC\include\yvals.h
F    2005-11-11 22:52:22    8816    _vcclrit    h    C:\VS8_2005\VC\include\_vcclrit.h