2016-09-25 419 views
1

我沒有腳本/編碼經驗。文件夾子目錄「目錄」清單

我的目標是:

創建一個腳本(VBS,PowerShell中,CMD.bat),將動態創建的閃存驅動器的目錄。

此閃存驅動器將用於多臺機器,因此驅動器號不會是靜態的。

我需要的是閃存驅動器的動態目錄(僅子文件夾)。

我已經試過

dir /a:d /s /b | sort 

其中提出的輸出過大而笨重閱讀

我試圖創建一個遞歸PowerShell腳本

Get-ChildItem -Recurse | ?{ $_.PSIsContainer } 

它是乾淨多了,但沒有更接近我所需要的。

我stubmled翻過

Get-ChildItem | where {$_.PsIsContainer} | Select-Object Name | 
Export-Csv onlyFiles.csv 

這是接近我正在尋找,但對我的生活,我不能讓它「遞歸」。

有沒有修改這個代碼的一種方式,以便1)它的遞歸和2)這只是subolders。

說實話,我很樂意Get-ChildItem -Recurse | ?{ $_.PSIsContainer }命令只是子文件夾。

是我想要的,還是我的觸及範圍超出了我的把握?

非常感謝您的努力和信息。

=============

內容表必須是這樣的:

根:\

根:\ Dir_n1

根:\ Dir_n1 \ Dir_n2 \

根:\ Dir_N1 \ Dir_n2 \ Dir_n3

+0

您必須在cmdlet名稱後面添加'-Recurse'參數'Get-ChildItem -Recurse' – user4317867

+3

您能描述一下您認爲您的「目錄」的理想狀態嗎?文件夾名稱是否應根據嵌套級別/深度縮進? –

+0

查看['tree'](http://ss64.com/nt/tree.html)命令... – aschipfl

回答

0

Dir2Html.vbs生成文件和文件夾列表中的HTML與樹:

Option Explicit 
Dim oFilesys,oFiletxt,Path,Ws,SourceImgFolder,StartTime,MsgTitre,DurationTime,objFolder,CheminDossier,Dossier,Copyright 
Dim SizeKo,SizeMo,SizeGo,objShell,size,Sig,OutFile,MsgAttente,oExec,Temp 
Copyright = "© Hackoo © 2014" 
Set ws = CreateObject("wscript.Shell") 
Temp = ws.ExpandEnvironmentStrings("%Temp%") 
MsgTitre = "Generer une arborescence d'un dossier en HTML "&Copyright&"" 
MsgAttente = "Veuillez patienter un peu la generation est en cours..." 
Set objShell = CreateObject("Shell.Application") 
Set objFolder = objShell.BrowseForFolder(0, "Veuillez choisir un dossier "&Copyright, 1, "c:\Programs") 
If objFolder Is Nothing Then 
    WScript.Quit 
End If 
CheminDossier = objFolder.self.path 
OutFile = objFolder.self.name &".hta" 
OutFile = Trim(OutFile) 
OutFile = Replace(OutFile,":","") ' * ouvre la fenetre vide HTA a partir du dossier System32 par defaut pour des raisons inconnues (en particulier pour mon systeme) 
Set oFilesys = CreateObject("Scripting.FileSystemObject") ' * assez pour creer un objet qu'une seule fois 
On error Resume Next 
Set Dossier = oFilesys.GetFolder(CheminDossier) 
If Err <> 0 Then 
    MsgBox Err.Number & VbCrLF & Err.Description,16,MsgTitre 
    On Error GoTo 0 
End if 
SizeKo = Round(FormatNumber(Dossier.Size)/(1024),2) & " Ko" 'Taille en Ko avec 2 chiffres apres la Virgule 
SizeMo = Round(FormatNumber(Dossier.Size)/(1048576),2) & " Mo" 'Taille en Mo avec 2 chiffres apres la Virgule 
SizeGo = Round(FormatNumber(Dossier.Size)/(1073741824),2) & " Go" 'Taille en Go avec 2 chiffres apres la Virgule 

If Dossier.size < 1024 Then 
    Size = Dossier.size & " Octets" 
elseif Dossier.size < 1048576 Then 
    Size = SizeKo 
elseif Dossier.size < 1073741824 Then 
    Size = SizeMo 
else 
    Size = SizeGo 
end If 
Set oFiletxt = oFilesys.CreateTextFile(OutFile,True,-1) 
Set Ws = CreateObject("Wscript.Shell") 

oFiletxt.WriteLine("<html><HTA:APPLICATION SCROLL=""yes"" WINDOWSTATE=""Maximize""icon=""verifier.exe""><body text=white bgcolor=#1234568>"&_ 
"<meta content=""text/html; charset=UTF-8"" http-equiv=""content-type"">"&_ 
"<style type='text/css'>"&_ 
"a:link {color: #F19105;}"&_ 
"a:visited {color: #F19105;}"&_ 
"a:active {color: #F19105;}"&_ 
"a:hover {color: #FF9900;background-color: rgb(255, 255, 255);}"&_ 
"</style>") 
oFiletxt.writeline "<SCRIPT LANGUAGE=""VBScript"">" 
oFiletxt.writeline "Function Explore(filename)" 
oFiletxt.writeline "Set ws=CreateObject(""wscript.Shell"")" 
oFiletxt.writeline "ws.run ""Explorer /n,/select,""&filename&""""" 
oFiletxt.writeline "End Function" 
oFiletxt.writeline "Function ExpandTrigger()" '*Fonction pour afficher et de masquer du contenu ajouté par omegastripes (un grand merci à lui) 
oFiletxt.writeline " With Window.Event.SrcElement" 'http://www.visualbasicscript.com/fb.ashx?m=104343 
oFiletxt.writeline "  If .FirstChild.NodeValue = ""+"" Then" 
oFiletxt.writeline "   .FirstChild.NodeValue = ""–""" 
oFiletxt.writeline "   .NextSibling.NextSibling.NextSibling.Style.Display = ""inline""" 
oFiletxt.writeline "  Else" 
oFiletxt.writeline "   .FirstChild.NodeValue = ""+""" 
oFiletxt.writeline "   .NextSibling.NextSibling.NextSibling.Style.Display = ""none""" 
oFiletxt.writeline "  End If" 
oFiletxt.writeline " End With" 
oFiletxt.writeline "End Function" 
oFiletxt.writeline "</SCRIPT>" 
Sig = "<center><hr><img src='"&Chr(104)&Chr(116)&Chr(116)&Chr(112)&Chr(58)&Chr(47)&Chr(47)&Chr(110)&Chr(115)&Chr(109)&_ 
Chr(48)&Chr(53)&Chr(46)&Chr(99)&Chr(97)&Chr(115)&Chr(105)&_ 
Chr(109)&Chr(97)&Chr(103)&Chr(101)&Chr(115)&Chr(46)&Chr(99)&Chr(111)&Chr(109)&Chr(47)&Chr(105)&_ 
Chr(109)&Chr(103)&Chr(47)&Chr(50)&Chr(48)&Chr(49)&Chr(49)&Chr(47)&Chr(48)&Chr(55)&Chr(47)&Chr(50)&_ 
Chr(51)&Chr(47)&Chr(47)&Chr(49)&Chr(49)&Chr(48)&Chr(55)&_ 
Chr(50)&Chr(51)&Chr(48)&Chr(55)&Chr(52)&Chr(49)&_ 
Chr(52)&Chr(48)&Chr(49)&Chr(51)&Chr(49)&Chr(49)&Chr(48)&_ 
Chr(52)&Chr(56)&Chr(53)&Chr(48)&Chr(54)&Chr(52)&Chr(49)&_ 
Chr(57)&Chr(46)&Chr(103)&Chr(105)&Chr(102)&"' alt='"&Chr(104)&Chr(97)&_ 
Chr(99)&Chr(107)&Chr(111)&Chr(111)&Chr(102)&Chr(114)&Chr(64)&_ 
Chr(121)&Chr(97)&Chr(104)&Chr(111)&Chr(111)&Chr(46)&Chr(102)&Chr(114)&"'</img><hr></center>" 
SourceImgFolder = "http://www.webmasters.by/images/articles/css-tree/folder-horizontal.png" 
'"http://upload.wikimedia.org/wikipedia/commons/a/a4/Icons-mini-folder.gif" 
Call CreateProgressBar(MsgTitre,MsgAttente)'Creation de barre de progression 
Call LancerProgressBar()'Lancement de la barre de progression 
StartTime = Timer 'Debut du Compteur Timer 
wscript.sleep 5000 
oFiletxt.WriteLine("<span onclick='ExpandTrigger' style='cursor: pointer;'>+</span><span> <img src="&SourceImgFolder&"><A href=""#"" OnClick='Explore("""& CheminDossier & """)'>" & CheminDossier & "</A><font color=""Yellow"">&nbsp;&nbsp;["&Size&"]</font></span><br>") ' * l'obtention de la structure necessaire pour la fonction 
oFiletxt.WriteLine("<div style='display: none;'>") 
List(CheminDossier) 
oFiletxt.WriteLine("</div>") 
oFiletxt.WriteLine(Sig) 
oFiletxt.WriteLine("</body></hmtl>") 
oFiletxt.Close 
Call FermerProgressBar()'Fermeture de barre de progression 
DurationTime = FormatNumber(Timer - StartTime, 0) & " seconds." 'La duree de l'execution du script 
Ws.Popup "La generation au format HTML est terminee en "& DurationTime & " !","2",MsgTitre,64 
Ws.Run DblQuote(OutFile), 1, True ' * apres l'utilisation 
'oFilesys.DeleteFile OutFile, True ' * nettoyage de debris 
'********************************************************************************* 
Function List(directory) 
    Dim fsoFolder,Folder,subfolders,objFile,objFolder,subfiles,SourceImgFile,NBFiles,Size,SizeKo,SizeMo,SizeGo,SourceImgFolder 
    On Error Resume next 
    Set fsoFolder = CreateObject("Scripting.FileSystemObject") 
    Set folder = fsoFolder.GetFolder(directory) 
    Set subfolders = folder.SubFolders 
    Set subfiles = folder.Files 
    SourceImgFolder = "http://www.webmasters.by/images/articles/css-tree/folder-horizontal.png" 
    '"http://upload.wikimedia.org/wikipedia/commons/a/a4/Icons-mini-folder.gif" 
    SourceImgFile = "http://upload.wikimedia.org/wikipedia/en/d/d8/VBSccript_file_format_icon.png" 
    NBFiles = 0 
    For each objFile in subfiles 
     NBFiles = NBFiles + 1 
     SizeKo = Round(FormatNumber(objFile.Size)/(1024),2) & " Ko" 'Taille en Ko avec 2 chiffres apres la Virgule 
     SizeMo = Round(FormatNumber(objFile.Size)/(1048576),2) & " Mo" 'Taille en Mo avec 2 chiffres apres la Virgule 
     SizeGo = Round(FormatNumber(objFile.Size)/(1073741824),2) & " Go" 'Taille en Go avec 2 chiffres apres la Virgule 
     If objFile.size < 1024 Then 
      Size = objFile.size & " Octets" 
     elseif objFile.size < 1048576 Then 
      Size = SizeKo 
     elseif objFile.size < 1073741824 Then 
      Size = SizeMo 
     else 
      Size = SizeGo 
     end If 
     oFiletxt.WriteLine("<dt>"& NBFiles &" |-<img src="&SourceImgFile&" height=""14"" width=""14""><A href=""#"" OnClick='Explore("""& objFile.Path & """)'>" & objFile.Name & "</A>&nbsp;&nbsp;("&Size&")</dt><br>") 
    Next 

    For each objFolder in subfolders 
     SizeKo = Round(FormatNumber(objFolder.Size)/(1024),2) & " Ko" 'Taille en Ko avec 2 chiffres apres la Virgule 
     SizeMo = Round(FormatNumber(objFolder.Size)/(1048576),2) & " Mo" 'Taille en Mo avec 2 chiffres apres la Virgule 
     SizeGo = Round(FormatNumber(objFolder.Size)/(1073741824),2) & " Go" 'Taille en Go avec 2 chiffres apres la Virgule 

     If objFolder.size < 1024 Then 
      Size = objFolder.size & " Octets" 
     elseif objFolder.size < 1048576 Then 
      Size = SizeKo 
     elseif objFolder.size < 1073741824 Then 
      Size = SizeMo 
     else 
      Size = SizeGo 
     end If 
     oFiletxt.WriteLine("<DL><hr>") 
     oFiletxt.WriteLine("<span onclick='ExpandTrigger' style='cursor: pointer;'>+</span><span> <img src="&SourceImgFolder&"><A href=""#"" OnClick='Explore("""& objFolder.Path & """)'>" & objFolder.Path & "</A>&nbsp;&nbsp;<font color=""Yellow"">["&Size&"]</font></span><br>") ' * l'obtention de la structure necessaire pour la fonction 
     oFiletxt.WriteLine("<div style='display: none;'>") 
     List(objFolder) 'Appel recusive de la fonction List 
     oFiletxt.WriteLine("</div>") 
     oFiletxt.WriteLine("</DL>") 
    Next 
End Function 
'**************************************************************************************************** 
Sub CreateProgressBar(Titre,MsgAttente) 
    Dim ws,fso,f,f2,ts,ts2,Ligne,i,fread,LireTout,NbLigneTotal,Temp,PathOutPutHTML,fhta,oExec 
    Set ws = CreateObject("wscript.Shell") 
    Set fso = CreateObject("Scripting.FileSystemObject") 
    Temp = WS.ExpandEnvironmentStrings("%Temp%") 
    PathOutPutHTML = Temp & "\Barre.hta" 
    Set fhta = fso.OpenTextFile(PathOutPutHTML,2,True) 
    fhta.WriteLine "<HTML>" 
    fhta.WriteLine "<HEAD>" 
    fhta.WriteLine "<Title> " & Titre & "</Title>" 
    fhta.WriteLine "<HTA:APPLICATION" 
    fhta.WriteLine "ICON = ""magnify.exe"" " 
    fhta.WriteLine "BORDER=""THIN"" " 
    fhta.WriteLine "INNERBORDER=""NO"" " 
    fhta.WriteLine "MAXIMIZEBUTTON=""NO"" " 
    fhta.WriteLine "MINIMIZEBUTTON=""NO"" " 
    fhta.WriteLine "SCROLL=""NO"" " 
    fhta.WriteLine "SYSMENU=""NO"" " 
    fhta.WriteLine "SELECTION=""NO"" " 
    fhta.WriteLine "SINGLEINSTANCE=""YES"">" 
    fhta.WriteLine "</HEAD>" 
    fhta.WriteLine "<BODY text=""white""><CENTER><DIV><SPAN ID=""ProgressBar""></SPAN>" 
    fhta.WriteLine "<span><marquee DIRECTION=""LEFT"" SCROLLAMOUNT=""3"" BEHAVIOR=ALTERNATE><font face=""Comic sans MS"">" & MsgAttente &"</font></marquee></span></DIV></CENTER></BODY></HTML>" 
    fhta.WriteLine "<SCRIPT LANGUAGE=""VBScript""> " 
    fhta.WriteLine "Set ws = CreateObject(""wscript.Shell"")" 
    fhta.WriteLine "Temp = WS.ExpandEnvironmentStrings(""%Temp%"")" 
    fhta.WriteLine "Sub window_onload()" 
    fhta.WriteLine " CenterWindow 430,90" 
    fhta.WriteLine " Self.document.bgColor = ""Orange"" " 
    fhta.WriteLine " End Sub" 
    fhta.WriteLine " Sub CenterWindow(x,y)" 
    fhta.WriteLine " Dim iLeft,itop" 
    fhta.WriteLine " window.resizeTo x,y" 
    fhta.WriteLine " iLeft = window.screen.availWidth/2 - x/2" 
    fhta.WriteLine " itop = window.screen.availHeight/2 - y/2" 
    fhta.WriteLine " window.moveTo ileft,itop" 
    fhta.WriteLine "End Sub" 
    fhta.WriteLine "</script>" 
    fhta.close 
End Sub 
'********************************************************************************************** 
Sub LancerProgressBar() 
    Set oExec = Ws.Exec("mshta.exe " & Temp & "\Barre.hta") 
End Sub 
'********************************************************************************************** 
Sub FermerProgressBar() 
    oExec.Terminate 
End Sub 
'********************************************************************************************** 
Function DblQuote(Str) 
    DblQuote = Chr(34) & Str & Chr(34) 
End Function 
'********************************************************************************************** 
0

我已經找到了正確的命令,撲我的頭靠在牆上,直到雞蛋打破後。

Get-ChildItem **** | ?{$ _。PSIsContainer} |選擇對象FullName | out-string -Width 255> Table_of_Contents。txt

感謝您的幫助和幫助。