当前位置: 移动技术网 > IT编程>脚本编程>VBScript > allfiles.vbs 显示子目录下的所有文件的修改时间、大小、文件名、扩展名等

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

2020年08月17日  | 移动技术网IT编程  | 我要评论
有的时候将子目录下的所有文件的修改时间、大小、全限定名等信息导出到excel表格中。尝试过命令行,但不太好用——1.对于“dir /s >1.txt”,当前目录与文件列表是分开显

有的时候将子目录下的所有文件的修改时间、大小、全限定名等信息导出到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
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

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

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网