博罗县上往网

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

2026-03-27 10:01:02 浏览次数:1
详细信息

' allfiles.vbs
Option Explicit

Dim objFSO, objFolder, objFile, objSubFolder
Dim strFolderPath, strOutput
Dim intCount

' 设置要扫描的文件夹路径(当前目录)
strFolderPath = "."

' 创建文件系统对象
Set objFSO = CreateObject("Scripting.FileSystemObject")

' 检查文件夹是否存在
If Not objFSO.FolderExists(strFolderPath) Then
    WScript.Echo "错误:文件夹 '" & strFolderPath & "' 不存在!"
    WScript.Quit 1
End If

Set objFolder = objFSO.GetFolder(strFolderPath)

' 输出标题
strOutput = "扫描文件夹: " & objFolder.Path & vbCrLf
strOutput = strOutput & String(80, "=") & vbCrLf
strOutput = strOutput & "修改时间" & vbTab & "大小(字节)" & vbTab & "文件名" & vbTab & "扩展名" & vbTab & "完整路径" & vbCrLf
strOutput = strOutput & String(80, "-") & vbCrLf

intCount = 0

' 遍历所有子文件夹和文件
ScanFolder objFolder

' 输出总结信息
strOutput = strOutput & vbCrLf & String(80, "=") & vbCrLf
strOutput = strOutput & "总计文件数: " & intCount & vbCrLf

' 显示结果
WScript.Echo strOutput

' 清理对象
Set objFolder = Nothing
Set objFSO = Nothing

' 递归扫描文件夹的函数
Sub ScanFolder(folder)
    Dim subFolder, file

    ' 扫描当前文件夹中的所有文件
    For Each file In folder.Files
        intCount = intCount + 1
        strOutput = strOutput & _
            FormatDateTime(file.DateLastModified, 2) & " " & _
            FormatDateTime(file.DateLastModified, 4) & vbTab & _
            file.Size & vbTab & _
            file.Name & vbTab & _
            objFSO.GetExtensionName(file.Name) & vbTab & _
            file.Path & vbCrLf
    Next

    ' 递归扫描子文件夹
    For Each subFolder In folder.SubFolders
        ScanFolder subFolder
    Next
End Sub

更高级的版本(支持命令行参数和排序):

' allfiles_advanced.vbs
Option Explicit

Dim objFSO, objFolder, colFiles, objFile
Dim strFolderPath, strOutput, strSortBy
Dim intCount, intMaxDepth, intCurrentDepth
Dim blnShowHidden, blnRecursive

' 默认参数
strFolderPath = "."                     ' 当前目录
intMaxDepth = 0                          ' 0表示无限深度
blnShowHidden = False                   ' 是否显示隐藏文件
blnRecursive = True                     ' 是否递归扫描
strSortBy = "name"                      ' 排序方式:name, size, date, extension

' 解析命令行参数
ParseArguments

' 创建文件系统对象
Set objFSO = CreateObject("Scripting.FileSystemObject")

' 检查文件夹是否存在
If Not objFSO.FolderExists(strFolderPath) Then
    WScript.Echo "错误:文件夹 '" & strFolderPath & "' 不存在!"
    WScript.Quit 1
End If

Set objFolder = objFSO.GetFolder(strFolderPath)

' 收集所有文件
intCount = 0
intCurrentDepth = 0
Set colFiles = CreateObject("Scripting.Dictionary")
CollectFiles objFolder

' 排序文件
Dim arrFiles, i, j, temp
arrFiles = colFiles.Items

' 简单冒泡排序(按指定字段)
For i = 0 To UBound(arrFiles)
    For j = i + 1 To UBound(arrFiles)
        If ShouldSwap(arrFiles(i), arrFiles(j)) Then
            Set temp = arrFiles(i)
            Set arrFiles(i) = arrFiles(j)
            Set arrFiles(j) = temp
        End If
    Next
Next

' 输出结果
strOutput = "扫描文件夹: " & objFolder.Path & vbCrLf
strOutput = strOutput & "扫描深度: " & IIf(intMaxDepth = 0, "无限", intMaxDepth) & vbCrLf
strOutput = strOutput & String(100, "=") & vbCrLf
strOutput = strOutput & "最后修改时间" & vbTab & "大小" & vbTab & "文件名" & vbTab & "扩展名" & vbTab & "路径" & vbCrLf
strOutput = strOutput & String(100, "-") & vbCrLf

For i = 0 To UBound(arrFiles)
    Set objFile = arrFiles(i)
    strOutput = strOutput & _
        FormatDateTime(objFile.DateLastModified, 2) & " " & _
        FormatDateTime(objFile.DateLastModified, 3) & vbTab & _
        FormatSize(objFile.Size) & vbTab & _
        objFile.Name & vbTab & _
        objFSO.GetExtensionName(objFile.Name) & vbTab & _
        Mid(objFile.Path, Len(strFolderPath) + 2) & vbCrLf
Next

strOutput = strOutput & vbCrLf & String(100, "=") & vbCrLf
strOutput = strOutput & "总计文件数: " & intCount & vbCrLf
strOutput = strOutput & "总计大小: " & FormatSize(GetTotalSize(arrFiles)) & vbCrLf

WScript.Echo strOutput

' 清理对象
Set objFolder = Nothing
Set objFSO = Nothing

' =========== 函数定义 ===========

Sub ParseArguments()
    Dim i
    For i = 0 To WScript.Arguments.Count - 1
        Select Case LCase(WScript.Arguments(i))
            Case "-p", "-path"
                If i + 1 < WScript.Arguments.Count Then
                    strFolderPath = WScript.Arguments(i + 1)
                End If
            Case "-d", "-depth"
                If i + 1 < WScript.Arguments.Count Then
                    intMaxDepth = CInt(WScript.Arguments(i + 1))
                End If
            Case "-h", "-hidden"
                blnShowHidden = True
            Case "-nr", "-norecursive"
                blnRecursive = False
            Case "-s", "-sort"
                If i + 1 < WScript.Arguments.Count Then
                    strSortBy = LCase(WScript.Arguments(i + 1))
                End If
        End Select
    Next
End Sub

Sub CollectFiles(folder)
    Dim file, subFolder

    ' 检查深度限制
    If intMaxDepth > 0 And intCurrentDepth >= intMaxDepth Then
        Exit Sub
    End If

    ' 收集文件
    For Each file In folder.Files
        ' 检查是否显示隐藏文件
        If blnShowHidden Or Not file.Attributes And 2 Then
            intCount = intCount + 1
            colFiles.Add "file" & intCount, file
        End If
    Next

    ' 递归收集子文件夹
    If blnRecursive Then
        intCurrentDepth = intCurrentDepth + 1
        For Each subFolder In folder.SubFolders
            CollectFiles subFolder
        Next
        intCurrentDepth = intCurrentDepth - 1
    End If
End Sub

Function ShouldSwap(file1, file2)
    Select Case strSortBy
        Case "size"
            ShouldSwap = file1.Size > file2.Size
        Case "date"
            ShouldSwap = file1.DateLastModified > file2.DateLastModified
        Case "extension"
            ShouldSwap = objFSO.GetExtensionName(file1.Name) > objFSO.GetExtensionName(file2.Name)
        Case Else ' "name" 或其他
            ShouldSwap = file1.Name > file2.Name
    End Select
End Function

Function FormatSize(sizeInBytes)
    If sizeInBytes >= 1073741824 Then
        FormatSize = FormatNumber(sizeInBytes / 1073741824, 2) & " GB"
    ElseIf sizeInBytes >= 1048576 Then
        FormatSize = FormatNumber(sizeInBytes / 1048576, 2) & " MB"
    ElseIf sizeInBytes >= 1024 Then
        FormatSize = FormatNumber(sizeInBytes / 1024, 2) & " KB"
    Else
        FormatSize = sizeInBytes & " B"
    End If
End Function

Function GetTotalSize(arrFiles)
    Dim total, i
    total = 0
    For i = 0 To UBound(arrFiles)
        total = total + arrFiles(i).Size
    Next
    GetTotalSize = total
End Function

Function IIf(condition, truePart, falsePart)
    If condition Then
        IIf = truePart
    Else
        IIf = falsePart
    End If
End Function

使用方法:

基础版本:

高级版本:

高级版本支持以下参数:

示例:

cscript allfiles_advanced.vbs -p "C:\MyFolder" -s size -h

这个脚本会递归扫描指定文件夹(包括子文件夹)中的所有文件,并显示:

您可以根据需要修改脚本来显示更多信息,如创建时间、访问时间、文件属性等。

相关推荐