'-------------vbsTree.vbs------------------------ '描述:用vbs輸出一個文件夾的目錄結構。 '------------------------------------------------ Const Unit4Size = "字節KBMBGB" Const OutFile = "OutTree.txt" Dim theApp,SelPath,TreePath,TreeStr Set theApp = CreateObject("Shell.Application") Set SelPath = theApp.BrowseForFolder(0,"請選擇需要列出子項目的路徑",0) If SelPath Is Nothing Then WScript.Quit TreePath = SelPath.items.Item.Path Set SelPathPath = Nothing Set theApp = Nothing Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") TreeStr = TreePath & FormatSize(objFSO.GetFolder(TreePath).Size) & vbCrLf Tree TreePath,"" Set objFile = objFSO.CreateTextFile(OutFile,True) objFile.Write TreeStr objFile.Close Set objFile = Nothing Set objFSO = Nothing MsgBox "查看當前目錄下的OutTree.txt",vbInformation,"完成 - vbsTree" Sub Tree(Path,SFSpace) Dim i,TempStr,FlSpace FlSpace = SFSpace & " " Set CrntFolder = objFSO.GetFolder(Path) i = 0:TempStr = "├─" For Each ConFile In CrntFolder.Files i = i + 1 If i = CrntFolder.Files.Count And CrntFolder.SubFolders.Count = 0 Then TempStr = "└─" TreeStr = TreeStr & FlSpace & Tempstr & ConFile.name & FormatSize(ConFile.size) & vbCrLf Next i = 0:TempStr = "├─" For Each SubFolder In CrntFolder.SubFolders i = i + 1 If i = CrntFolder.SubFolders.Count Then TempStr = "└─" SFSpace = FlSpace & " " Else SFSpace = FlSpace & "│" End If TreeStr = TreeStr & FlSpace & TempStr & SubFolder.name & FormatSize(SubFolder.size) & vbCrLf Tree SubFolder,(SFSpace) Next End Sub Function FormatSize(SZ) Dim i Do While SZ > 1024 i = i + 1 SZ = SZ / 1024 Loop FormatSize = " (" & SZ & Mid(Unit4Size,1 + 2 * i,2) & ")" End Function
文件夾瀏覽部分優化后的代碼
復制代碼 代碼如下:
'-------------vbsTree.vbs------------------------ '描述:用vbs輸出一個文件夾的目錄結構。 '------------------------------------------------ Const Unit4Size = "字節KBMBGB" Const OutFile = "OutTree.txt" Dim TreePath,TreeStr,WS Set WS = WScript.CreateObject("WScript.Shell") TreePath = BFF("請選擇需要列出子項目的路徑",&H0001 + &H0008 + &H0010,"") Set WS = Nothing If Len(TreePath) = 0 Then WScript.Quit If Len(TreePath) <= 3 Then MsgBox "無法處理根目錄!",64,"提示":WScript.Quit
Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") TreeStr = TreePath & FormatSize(objFSO.GetFolder(TreePath).Size) & vbCrLf Tree TreePath,"" Set objFile = objFSO.CreateTextFile(OutFile,True) objFile.Write TreeStr objFile.Close Set objFile = Nothing Set objFSO = Nothing MsgBox "查看當前目錄下的OutTree.txt",vbInformation,"完成 - vbsTree" Sub Tree(Path,SFSpace) Dim i,TempStr,FlSpace FlSpace = SFSpace & " " Set CrntFolder = objFSO.GetFolder(Path) i = 0:TempStr = "├─" For Each ConFile In CrntFolder.Files i = i + 1 If i = CrntFolder.Files.Count And CrntFolder.SubFolders.Count = 0 Then TempStr = "└─" TreeStr = TreeStr & FlSpace & Tempstr & ConFile.name & FormatSize(ConFile.size) & vbCrLf Next i = 0:TempStr = "├─" For Each SubFolder In CrntFolder.SubFolders i = i + 1 If i = CrntFolder.SubFolders.Count Then TempStr = "└─" SFSpace = FlSpace & " " Else SFSpace = FlSpace & "│" End If TreeStr = TreeStr & FlSpace & TempStr & SubFolder.name & FormatSize(SubFolder.size) & vbCrLf Tree SubFolder,(SFSpace) Next End Sub Function FormatSize(SZ) Dim i Do While SZ > 1024 i = i + 1 SZ = SZ / 1024 Loop FormatSize = " (" & SZ & Mid(Unit4Size,1 + 2 * i,2) & ")" End Function