咔叽游戏

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 501|回复: 0

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

[复制链接]
  • TA的每日心情
    无聊
    2019-5-27 08:20
  • 签到天数: 4 天

    [LV.2]圆转纯熟

    发表于 2020-8-15 10:54:03 | 显示全部楼层 |阅读模式
    有的时候将子目录下的所有文件的修改时间、大小、全限定名等信息导出到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  

    QQ|免责声明|小黑屋|手机版|Archiver|咔叽游戏

    GMT+8, 2024-3-19 19:14

    Powered by Discuz! X3.4

    © 2001-2023 Discuz! Team.

    快速回复 返回顶部 返回列表