列出整个 C: 驱动器上名称长度正好为五个字符的所有文本文件 - 使列表以宽格式输出

列出整个 C: 驱动器上名称长度正好为五个字符的所有文本文件 - 使列表以宽格式输出

我已经尝试过 CD C:\ DIR /s /b ?????.txt 但是我会返回名称在 5 个字符以内的所有文件,那么如何仅显示 5 个字符呢?

非常感谢~

答案1

VBA 脚本列出所有文本文件....

'Wscript.Echo "begin."
Set objFSO = CreateObject("Scripting.FileSystemObject")
DIM folderStart,fileOutName, fileH
On Error Resume Next
folderStart = InputBox("Drive or folder to start looking in", , "D:\prog\github\academic")
fileOutName = InputBox("File to write results", "Output file", "c:\tmp\filesFounds.note")
Set objSuperFolder = objFSO.GetFolder(folderStart)

Set fileH = objFSO.OpenTextFile(fileOutName,2 ,1)
fileH.WriteLine ("Start " & Now)  
Call ShowSubfolders (objSuperFolder, fileH)
Call fileH.WriteLine ("end " & Now)  
fileH.Close
Wscript.Echo "Done " & Now
WScript.Quit 0

Sub ShowSubFolders(fFolder, fileH)
    Err.clear
    On Error Resume Next
    Set objFolder = objFSO.GetFolder(fFolder.Path)
    If Err.Number = 0 then
        Set colFiles = objFolder.Files
        If Err.Number = 0 then
            For Each objFile in colFiles
                If Err.Number = 0 then
                If UCase(objFSO.GetExtensionName(objFile.name)) = "TXT"  Then
                    If len(objFile.name) = 9  Then    
                        'Wscript.Echo objFile.Name
                        fileH.WriteLine (objFile.path)  
                        'fileH.WriteLine (objFile.name)  'only short name
                    End If
                End If
            End If
            Next
        End If

        For Each Subfolder in fFolder.SubFolders
        Call ShowSubFolders(Subfolder, fileH)
        Next
    End If
End Sub

原来的

'Wscript.Echo "开始。" 设置 objFSO = CreateObject("Scripting.FileSystemObject") DIM folderStart,fileOutName,fileH folderStart = InputBox("开始查找的驱动器或文件夹", , "c:\") fileOutName = InputBox("要写入结果的文件", "输出文件", "c:\tmp\filesFounds.note") 设置 objSuperFolder = objFSO.GetFolder(folderStart)

Set fileH = objFSO.OpenTextFile(fileOutName,2 ,1)
fileH.WriteLine ("Start " & Now)  
Call ShowSubfolders (objSuperFolder, fileH)
Call fileH.WriteLine ("end " & Now)  
fileH.Close
Wscript.Echo "Done " & Now
WScript.Quit 0

Sub ShowSubFolders(fFolder, fileH)
On Error Resume Next
    Set objFolder = objFSO.GetFolder(fFolder.Path)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
        If UCase(objFSO.GetExtensionName(objFile.name)) = "TXT"  Then
            If len(objFile.name) = 9  Then    
                    'Wscript.Echo objFile.Name
                    fileH.WriteLine (objFile.path)  
                    'fileH.WriteLine (objFile.name)  'only short name
                End If
        End If
    Next

    For Each Subfolder in fFolder.SubFolders
        Call ShowSubFolders(Subfolder, fileH)
    Next

End Sub

或者从http://sel2in.com/pages/prog/vba/

要使用的文件名应该是 a.vbs 或 names5.vbs(或以 vbs 结尾的其他名称),然后打开它 - 双击或右键单击并选择打开

有错误处理但未在 c:\ 上尝试(现在正在运行)

相关内容