![列出整个 C: 驱动器上名称长度正好为五个字符的所有文本文件 - 使列表以宽格式输出](https://linux22.com/image/1360719/%E5%88%97%E5%87%BA%E6%95%B4%E4%B8%AA%20C%3A%20%E9%A9%B1%E5%8A%A8%E5%99%A8%E4%B8%8A%E5%90%8D%E7%A7%B0%E9%95%BF%E5%BA%A6%E6%AD%A3%E5%A5%BD%E4%B8%BA%E4%BA%94%E4%B8%AA%E5%AD%97%E7%AC%A6%E7%9A%84%E6%89%80%E6%9C%89%E6%96%87%E6%9C%AC%E6%96%87%E4%BB%B6%20-%20%E4%BD%BF%E5%88%97%E8%A1%A8%E4%BB%A5%E5%AE%BD%E6%A0%BC%E5%BC%8F%E8%BE%93%E5%87%BA%20.png)
我已经尝试过 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:\ 上尝试(现在正在运行)