Copy codeThe Code is as follows: '------------- vbsTree. vbs ------------------------
'Description: outputs the directory structure of a folder using vbs.
'------------------------------------------------
Const Unit4Size = "Byte KBMBGB"
Const OutFile = "OutTree.txt"
Dim theApp, SelPath, TreePath, TreeStr
Set theApp = CreateObject ("Shell. Application ")
Set SelPath = theApp. BrowseForFolder (0, "select the path to list subitems", 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 "initialize outtree.txt under the current directory", vbInformation, "complete-vbsTree"
Sub Tree (Path, SFSpace)
Dim I, TempStr, FlSpace
FlSpace = SFSpace &""
Set CrntFolder = objFSO. GetFolder (Path)
I = 0: TempStr = "yun──"
For Each ConFile In CrntFolder. Files
I = I + 1
If I = CrntFolder. Files. Count And CrntFolder. SubFolders. Count = 0 Then TempStr = "audio"
TreeStr = TreeStr & FlSpace & Tempstr & ConFile. name & FormatSize (ConFile. size) & vbCrLf
Next
I = 0: TempStr = "yun──"
For Each SubFolder In CrntFolder. SubFolders
I = I + 1
If I = CrntFolder. SubFolders. Count Then
TempStr = "yun──"
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
Optimized Code for folder browsingCopy codeThe Code is as follows: '------------- vbsTree. vbs ------------------------
'Description: outputs the directory structure of a folder using vbs.
'------------------------------------------------
Const Unit4Size = "Byte KBMBGB"
Const OutFile = "OutTree.txt"
Dim TreePath, TreeStr, WS
Set WS = WScript. CreateObject ("WScript. Shell ")
TreePath = BFF ("select the path to list subitem targets", & H0001 + & H0008 + & H0010 ,"")
Set WS = Nothing
If Len (TreePath) = 0 Then WScript. Quit
If Len (TreePath) <= 3 Then MsgBox "cannot process the root directory! ", 64," prompt ": 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 "initialize outtree.txt under the current directory", vbInformation, "complete-vbsTree"
Sub Tree (Path, SFSpace)
Dim I, TempStr, FlSpace
FlSpace = SFSpace &""
Set CrntFolder = objFSO. GetFolder (Path)
I = 0: TempStr = "yun──"
For Each ConFile In CrntFolder. Files
I = I + 1
If I = CrntFolder. Files. Count And CrntFolder. SubFolders. Count = 0 Then TempStr = "audio"
TreeStr = TreeStr & FlSpace & Tempstr & ConFile. name & FormatSize (ConFile. size) & vbCrLf
Next
I = 0: TempStr = "yun──"
For Each SubFolder In CrntFolder. SubFolders
I = I + 1
If I = CrntFolder. SubFolders. Count Then
TempStr = "yun──"
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
Function BFF (title, flag, dir)
On Error Resume Next
Dim oShell, oItem, oStr
Set oShell = WScript. CreateObject ("Shell. Application ")
Set oItem = oShell. BrowseForFolder (& H0, title, flag, dir)
OStr = oItem. Title
If Err <> 0 Then
Set oShell = Nothing
Set oItem = Nothing
Exit Function
End If
If InStr (oStr, ":") Then
BFF = mid (oStr, InStr (oStr, ":")-1, 2)
Else
Select Case oStr
Case "desktop"
BFF = WS. SpecialFolders ("Desktop ")
Case "My Documents"
BFF = WS. SpecialFolders ("MyDocuments ")
Case "my computer"
MsgBox "invalid directory! ", 64," prompt ": WScript. Quit
Case "Network neighbors"
MsgBox "invalid directory! ", 64," prompt ": WScript. Quit
Case Else
BFF = oItem. ParentFolder. ParseName (oItem. Title). Path
End Select
End If
Set oShell = Nothing
Set oItem = Nothing
If Right (BFF, 1) <> "\" Then
BFF = BFF &"\"
End If
On Error GoTo 0
End Function