ѵ >> ̿ >> VbScript


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


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

尝试过命令行,但不太好用—?/p>

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

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

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

全部代码如下—?/p>

' 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:Tempabc.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中?/p>

例如该脚本对VC2005的include目录的输出结果为—?/p>

Type    DateLastModified    Size    Base    Ext    FullName
D    2011-9-5 13:16:14        CodeAnalysis        C:VS8_2005VCincludeCodeAnalysis
F    2006-12-1 22:16:08    5720    sourceannotations    h    C:VS8_2005VCincludeCodeAnalysissourceannotations.h
F    2005-11-11 22:52:36    866    Warnings    h    C:VS8_2005VCincludeCodeAnalysisWarnings.h
D    2011-9-5 13:16:07        msclr        C:VS8_2005VCincludemsclr
D    2011-9-5 13:16:07        com        C:VS8_2005VCincludemsclrcom
F    2006-12-1 22:54:28    8078    ptr    h    C:VS8_2005VCincludemsclrcomptr.h
F    2005-11-11 22:52:36    585    all    h    C:VS8_2005VCincludemsclrall.h
F    2006-12-1 22:54:28    125137    appdomain    h    C:VS8_2005VCincludemsclrappdomain.h
F    2005-11-11 22:52:36    6155    auto_gcroot    h    C:VS8_2005VCincludemsclrauto_gcroot.h
F    2005-11-11 22:52:36    4098    auto_handle    h    C:VS8_2005VCincludemsclrauto_handle.h
F    2005-11-11 22:52:36    2504    event    h    C:VS8_2005VCincludemsclrevent.h
F    2005-11-11 22:52:36    3958    gcroot    h    C:VS8_2005VCincludemsclrgcroot.h
F    2005-11-11 22:52:36    8012    lock    h    C:VS8_2005VCincludemsclrlock.h
F    2005-11-11 22:52:36    1257    safebool    h    C:VS8_2005VCincludemsclrsafebool.h
D    2011-9-5 11:55:28        sys        C:VS8_2005VCincludesys
F    2005-11-11 22:52:36    997    locking    h    C:VS8_2005VCincludesyslocking.h
F    2005-11-11 22:52:36    6969    stat    h    C:VS8_2005VCincludesysstat.h
F    2005-11-11 22:52:36    1856    stat    inl    C:VS8_2005VCincludesysstat.inl
F    2005-11-11 22:52:36    3340    timeb    h    C:VS8_2005VCincludesys imeb.h
F    2005-11-11 22:52:36    1414    timeb    inl    C:VS8_2005VCincludesys imeb.inl
F    2005-11-11 22:52:38    2150    types    h    C:VS8_2005VCincludesys ypes.h
F    2005-11-11 22:52:38    4006    utime    h    C:VS8_2005VCincludesysutime.h
F    2005-11-11 22:52:38    2881    utime    inl    C:VS8_2005VCincludesysutime.inl
F    2005-11-11 22:52:38    1959    wstat    inl    C:VS8_2005VCincludesyswstat.inl
F    2006-12-1 22:54:24    191593    algorithm        C:VS8_2005VCincludealgorithm
F    2013-1-7 21:25:47    0    allfiles    txt    C:VS8_2005VCincludeallfiles.txt
F    2013-1-7 21:25:11    2730    allfiles    vbs    C:VS8_2005VCincludeallfiles.vbs
F    2005-11-11 22:52:24    689    assert    h    C:VS8_2005VCincludeassert.h
F    2005-11-11 22:52:24    13925    bitset        C:VS8_2005VCincludeitset
F    2005-11-11 22:52:24    223    cassert        C:VS8_2005VCincludecassert
F    2005-11-11 22:52:24    1050    cctype        C:VS8_2005VCincludecctype
F    2005-11-11 22:52:24    694    cerrno        C:VS8_2005VCincludecerrno
F    2005-11-11 22:52:24    296    cfloat        C:VS8_2005VCincludecfloat
F    2005-11-11 22:52:24    301    ciso646        C:VS8_2005VCincludeciso646
F    2005-11-11 22:52:24    336    climits        C:VS8_2005VCincludeclimits
F    2005-11-11 22:52:24    698    clocale        C:VS8_2005VCincludeclocale
F    2005-11-11 22:52:24    1553    cmath        C:VS8_2005VCincludecmath
F    2006-12-1 23:07:20    8949    comdef    h    C:VS8_2005VCincludecomdef.h
F    2005-11-11 22:52:24    79172    comdefsp    h    C:VS8_2005VCincludecomdefsp.h
F    2005-11-11 22:52:24    27097    comip    h    C:VS8_2005VCincludecomip.h
F    2005-11-11 22:52:24    28821    complex        C:VS8_2005VCincludecomplex
F    2005-11-11 22:52:24    58427    comutil    h    C:VS8_2005VCincludecomutil.h
F    2005-11-11 22:52:24    8895    conio    h    C:VS8_2005VCincludeconio.h
F    2006-12-1 22:54:26    646    crtassem    h    C:VS8_2005VCincludecrtassem.h
F    2006-12-1 22:54:26    38386    crtdbg    h    C:VS8_2005VCincludecrtdbg.h
F    2006-12-1 22:54:26    93735    crtdefs    h    C:VS8_2005VCincludecrtdefs.h
F    2005-11-11 22:52:24    2183    crtwrn    h    C:VS8_2005VCincludecrtwrn.h
F    2005-11-11 22:52:24    883    csetjmp        C:VS8_2005VCincludecsetjmp
F    2005-11-11 22:52:24    610    csignal        C:VS8_2005VCincludecsignal
F    2005-11-11 22:52:24    574    cstdarg        C:VS8_2005VCincludecstdarg
F    2005-11-11 22:52:24    592    cstddef        C:VS8_2005VCincludecstddef
F    2005-11-11 22:52:24    1514    cstdio        C:VS8_2005VCincludecstdio
F    2005-11-11 22:52:24    1045    cstdlib        C:VS8_2005VCincludecstdlib
F    2005-11-11 22:52:26    947    cstring        C:VS8_2005VCincludecstring
F    2005-11-11 22:52:26    758    ctime        C:VS8_2005VCincludectime
F    2005-11-11 22:52:26    19257    ctype    h    C:VS8_2005VCincludectype.h
F    2005-11-11 22:52:26    1621    cwchar        C:VS8_2005VCincludecwchar
F    2005-11-11 22:52:26    1266    cwctype        C:VS8_2005VCincludecwctype
F    2005-11-11 22:21:32    5125    dbgautoattach    h    C:VS8_2005VCincludedbgautoattach.h
F    2005-11-11 22:52:26    15385    delayhlp    cpp    C:VS8_2005VCincludedelayhlp.cpp
F    2005-11-11 22:52:26    4350    delayimp    h    C:VS8_2005VCincludedelayimp.h
F    2005-11-11 22:52:26    38621    deque        C:VS8_2005VCincludedeque
F    2005-11-11 22:52:26    4463    direct    h    C:VS8_2005VCincludedirect.h
F    2005-11-11 22:52:26    2027    dos    h    C:VS8_2005VCincludedos.h
F    2006-12-1 22:54:26    43278    dvec    h    C:VS8_2005VCincludedvec.h
F    2005-11-11 22:52:26    2997    eh    h    C:VS8_2005VCincludeeh.h
F    2005-11-11 22:52:26    14047    emmintrin    h    C:VS8_2005VCincludeemmintrin.h
F    2005-11-11 22:52:26    2279    errno    h    C:VS8_2005VCincludeerrno.h
F    2006-12-1 22:54:26    10407    exception        C:VS8_2005VCincludeexception
F    2005-11-11 22:52:26    3036    excpt    h    C:VS8_2005VCincludeexcpt.h
F    2005-11-11 22:52:26    2646    fcntl    h    C:VS8_2005VCincludefcntl.h
F    2005-11-11 22:52:26    13096    float    h    C:VS8_2005VCincludefloat.h
F    2005-11-11 22:52:26    7619    fpieee    h    C:VS8_2005VCincludefpieee.h
F    2005-11-11 22:52:26    30192    fstream        C:VS8_2005VCincludefstream
F    2005-11-11 22:52:26    20758    functional        C:VS8_2005VCincludefunctional
F    2006-12-1 22:54:26    16955    fvec    h    C:VS8_2005VCincludefvec.h
F    2005-11-11 22:52:26    1396    gcroot    h    C:VS8_2005VCincludegcroot.h
F    2005-11-11 22:52:26    9631    hash_map        C:VS8_2005VCincludehash_map
F    2005-11-11 22:52:26    8349    hash_set        C:VS8_2005VCincludehash_set
F    2006-12-1 22:54:26    80350    intrin    h    C:VS8_2005VCincludeintrin.h
F    2005-11-11 22:52:28    1586    invkprxy    h    C:VS8_2005VCincludeinvkprxy.h
F    2005-11-11 22:52:28    16413    io    h    C:VS8_2005VCincludeio.h
F    2005-11-11 22:52:28    2909    iomanip        C:VS8_2005VCincludeiomanip
F    2005-11-11 22:52:28    8146    ios        C:VS8_2005VCincludeios
F    2005-11-11 22:52:28    23755    iosfwd        C:VS8_2005VCincludeiosfwd
F    2005-11-11 22:52:28    2101    iostream        C:VS8_2005VCincludeiostream
F    2005-11-11 22:52:28    561    iso646    h    C:VS8_2005VCincludeiso646.h
F    2006-12-1 22:54:28    32646    istream        C:VS8_2005VCincludeistream
F    2006-12-1 22:54:28    14517    iterator        C:VS8_2005VCincludeiterator
F    2005-11-11 22:52:28    33146    ivec    h    C:VS8_2005VCincludeivec.h
F    2005-11-11 22:52:28    29325    limits        C:VS8_2005VCincludelimits
F    2005-11-11 22:52:28    4678    limits    h    C:VS8_2005VCincludelimits.h
F    2006-12-1 22:54:28    35936    list        C:VS8_2005VCincludelist
F    2005-11-12 0:20:20    2425    listing    inc    C:VS8_2005VCincludelisting.inc
F    2005-11-11 22:52:28    8068    locale        C:VS8_2005VCincludelocale
F    2005-11-11 22:52:28    3714    locale    h    C:VS8_2005VCincludelocale.h
F    2006-12-1 22:54:28    10463    malloc    h    C:VS8_2005VCincludemalloc.h
F    2005-11-11 22:52:28    10022    map        C:VS8_2005VCincludemap
F    2006-12-1 22:54:28    23670    math    h    C:VS8_2005VCincludemath.h
F    2005-11-11 22:52:28    5666    mbctype    h    C:VS8_2005VCincludembctype.h
F    2006-12-1 22:54:28    31431    mbstring    h    C:VS8_2005VCincludembstring.h
F    2006-12-1 22:54:28    29695    memory        C:VS8_2005VCincludememory
F    2005-11-11 22:52:30    2888    memory    h    C:VS8_2005VCincludememory.h
F    2005-11-11 22:52:30    456    minmax    h    C:VS8_2005VCincludeminmax.h
F    2005-11-11 22:52:30    1663    mm3dnow    h    C:VS8_2005VCincludemm3dnow.h
F    2005-11-11 22:52:30    6413    mmintrin    h    C:VS8_2005VCincludemmintrin.h
F    2005-11-11 22:52:30    3276    new        C:VS8_2005VCinclude ew
F    2005-11-11 22:52:30    3812    new    h    C:VS8_2005VCinclude ew.h
F    2006-12-1 22:54:28    27362    numeric        C:VS8_2005VCinclude umeric
F    2005-11-11 22:52:30    5310    omp    h    C:VS8_2005VCincludeomp.h
F    2006-12-1 22:54:28    29058    ostream        C:VS8_2005VCincludeostream
F    2005-9-27 20:49:22    110287    penwin    h    C:VS8_2005VCincludepenwin.h
F    2005-11-11 22:52:30    527    pgobootrun    h    C:VS8_2005VCincludepgobootrun.h
F    2005-11-11 22:52:30    12546    process    h    C:VS8_2005VCincludeprocess.h
F    2005-11-11 22:52:30    6374    queue        C:VS8_2005VCincludequeue
F    2005-11-11 22:52:30    5418    rtcapi    h    C:VS8_2005VCinclude tcapi.h
F    2005-11-11 22:52:30    45876    sal    h    C:VS8_2005VCincludesal.h
F    2005-11-11 22:52:30    6283    search    h    C:VS8_2005VCincludesearch.h
F    2005-11-11 22:52:30    8945    set        C:VS8_2005VCincludeset
F    2005-11-11 22:52:30    6208    setjmp    h    C:VS8_2005VCincludesetjmp.h
F    2005-11-11 22:52:30    849    setjmpex    h    C:VS8_2005VCincludesetjmpex.h
F    2005-11-11 22:52:30    899    share    h    C:VS8_2005VCincludeshare.h
F    2005-11-11 22:52:30    3251    signal    h    C:VS8_2005VCincludesignal.h
F    2005-11-12 0:20:26    14733    srv    h    C:VS8_2005VCincludesrv.h
F    2005-11-11 22:52:30    16632    sstream        C:VS8_2005VCincludesstream
F    2005-11-11 22:52:30    3687    stack        C:VS8_2005VCincludestack
F    2005-11-11 22:52:30    659    stdarg    h    C:VS8_2005VCincludestdarg.h
F    2005-11-11 22:52:32    1607    stddef    h    C:VS8_2005VCincludestddef.h
F    2005-11-11 22:52:32    6475    stdexcept        C:VS8_2005VCincludestdexcept
F    2005-11-11 22:52:32    555    stdexcpt    h    C:VS8_2005VCincludestdexcpt.h
F    2006-12-1 22:54:28    46583    stdio    h    C:VS8_2005VCincludestdio.h
F    2006-12-1 22:54:28    47898    stdlib    h    C:VS8_2005VCincludestdlib.h
F    2005-11-11 22:52:32    11931    streambuf        C:VS8_2005VCincludestreambuf
F    2005-11-11 22:52:32    21939    string        C:VS8_2005VCincludestring
F    2006-12-1 22:54:28    26938    string    h    C:VS8_2005VCincludestring.h
F    2005-11-11 22:52:32    17940    strstream        C:VS8_2005VCincludestrstream
F    2005-11-11 22:52:32    3825    swprintf    inl    C:VS8_2005VCincludeswprintf.inl
F    2006-12-1 22:54:28    99143    tchar    h    C:VS8_2005VCinclude char.h
F    2005-11-11 22:52:32    11837    time    h    C:VS8_2005VCinclude ime.h
F    2005-11-11 22:52:32    3730    time    inl    C:VS8_2005VCinclude ime.inl
F    2005-11-11 22:52:32    5157    typeinfo        C:VS8_2005VCinclude ypeinfo
F    2005-11-11 22:52:32    999    typeinfo    h    C:VS8_2005VCinclude ypeinfo.h
F    2005-11-11 22:52:32    3982    use_ansi    h    C:VS8_2005VCincludeuse_ansi.h
F    2005-11-11 22:52:32    4719    utility        C:VS8_2005VCincludeutility
F    2005-11-11 22:52:32    4032    vadefs    h    C:VS8_2005VCincludevadefs.h
F    2005-11-11 22:52:32    42003    valarray        C:VS8_2005VCincludevalarray
F    2005-11-11 22:52:32    3730    varargs    h    C:VS8_2005VCincludevarargs.h
F    2005-11-11 22:52:32    1405    vcclr    h    C:VS8_2005VCincludevcclr.h
F    2005-11-11 22:52:32    62008    vector        C:VS8_2005VCincludevector
F    2006-12-1 22:54:28    74974    wchar    h    C:VS8_2005VCincludewchar.h
F    2005-11-11 22:52:34    6705    wctype    h    C:VS8_2005VCincludewctype.h
F    2005-11-11 23:44:36    29082    wmiatlprov    h    C:VS8_2005VCincludewmiatlprov.h
F    2005-11-11 22:52:34    1381    wtime    inl    C:VS8_2005VCincludewtime.inl
F    2005-11-11 22:52:34    27165    xcomplex        C:VS8_2005VCincludexcomplex
F    2005-11-11 22:52:34    4717    xdebug        C:VS8_2005VCincludexdebug
F    2006-12-1 22:54:28    21142    xhash        C:VS8_2005VCincludexhash
F    2005-11-11 22:52:34    19697    xiosbase        C:VS8_2005VCincludexiosbase
F    2005-11-11 22:52:34    77261    xlocale        C:VS8_2005VCincludexlocale
F    2005-11-11 22:52:34    7701    xlocinfo        C:VS8_2005VCincludexlocinfo
F    2005-11-11 22:52:34    4614    xlocinfo    h    C:VS8_2005VCincludexlocinfo.h
F    2005-11-11 22:52:34    3841    xlocmes        C:VS8_2005VCincludexlocmes
F    2005-11-11 22:52:34    27813    xlocmon        C:VS8_2005VCincludexlocmon
F    2006-12-1 22:54:28    44597    xlocnum        C:VS8_2005VCincludexlocnum
F    2005-11-11 22:52:34    21103    xloctime        C:VS8_2005VCincludexloctime
F    2005-11-11 22:52:34    4360    xmath    h    C:VS8_2005VCincludexmath.h
F    2005-11-11 22:52:34    7218    xmemory        C:VS8_2005VCincludexmemory
F    2005-11-11 22:52:34    18149    xmmintrin    h    C:VS8_2005VCincludexmmintrin.h
F    2005-11-11 22:52:34    2311    xstddef        C:VS8_2005VCincludexstddef
F    2006-12-1 22:54:28    66005    xstring        C:VS8_2005VCincludexstring
F    2005-11-11 22:52:34    41252    xtree        C:VS8_2005VCincludextree
F    2006-12-1 22:54:28    123607    xutility        C:VS8_2005VCincludexutility
F    2005-11-11 22:52:36    2364    ymath    h    C:VS8_2005VCincludeymath.h
F    2006-12-1 22:54:28    21162    yvals    h    C:VS8_2005VCincludeyvals.h
F    2005-11-11 22:52:22    8816    _vcclrit    h    C:VS8_2005VCinclude\_vcclrit.h

到此这篇关于allfiles.vbs 显示子目录下的所有文件的修改时间、大小、文件名、扩展名等的文章就介绍到这了,更多相关显示文件的修改时间、大小、文件名等内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!