vbsTree VBS腳本模擬tree命令
來(lái)源:易賢網(wǎng) 閱讀:1238 次 日期:2016-06-30 11:21:37
溫馨提示:易賢網(wǎng)小編為您整理了“vbsTree VBS腳本模擬tree命令”,方便廣大網(wǎng)友查閱!

用vbs輸出一個(gè)文件夾的目錄結(jié)構(gòu),喜歡的朋友可以測(cè)試下

代碼如下:

'-------------vbsTree.vbs------------------------

'描述:用vbs輸出一個(gè)文件夾的目錄結(jié)構(gòu)。

'------------------------------------------------

Const Unit4Size = "字節(jié)KBMBGB"

Const OutFile = "OutTree.txt"

Dim theApp,SelPath,TreePath,TreeStr

Set theApp = CreateObject("Shell.Application")

Set SelPath = theApp.BrowseForFolder(0,"請(qǐng)選擇需要列出子項(xiàng)目的路徑",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 "查看當(dāng)前目錄下的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

文件夾瀏覽部分優(yōu)化后的代碼

代碼如下:

'-------------vbsTree.vbs------------------------

'描述:用vbs輸出一個(gè)文件夾的目錄結(jié)構(gòu)。

'------------------------------------------------

Const Unit4Size = "字節(jié)KBMBGB"

Const OutFile = "OutTree.txt"

Dim TreePath,TreeStr,WS

Set WS = WScript.CreateObject("WScript.Shell")

TreePath = BFF("請(qǐng)選擇需要列出子項(xiàng)目的路徑",&H0001 + &H0008 + &H0010,"")

Set WS = Nothing

If Len(TreePath) = 0 Then WScript.Quit

If Len(TreePath) <= 3 Then MsgBox "無(wú)法處理根目錄!",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 "查看當(dāng)前目錄下的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

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 "桌面"

BFF = WS.SpecialFolders("Desktop")

Case "我的文檔"

BFF = WS.SpecialFolders("MyDocuments")

Case "我的電腦"

MsgBox "無(wú)效目錄!",64,"提示":WScript.Quit

Case "網(wǎng)上鄰居"

MsgBox "無(wú)效目錄!",64,"提示":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

更多信息請(qǐng)查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:vbsTree VBS腳本模擬tree命令
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽(tīng)報(bào)名

  • 報(bào)班類(lèi)型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專(zhuān)用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)