文件夹子目录“目录”列表

时间:2016-09-25 22:35:31

标签: powershell csv batch-file recursion

我没有脚本/编码经验。

我的目标是:

创建一个动态创建闪存驱动器目录的脚本(VBS,Powershell,CMD.bat)。

此闪存驱动器将用于多台计算机,因此驱动器号不会是静态的。

我需要的是闪存驱动器的动态目录(仅限子文件夹)。

我试过

dir /a:d /s /b | sort

表示输出太大且难以阅读

我试图创建一个递归的PowerShell脚本

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

它更干净,但没有更接近我需要的东西。

我碰到了

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

很多更接近我正在寻找的东西,但对于我的生活,我无法让它“递归”。

有没有办法修改此代码,因此 1)它是递归的, 2)只是它的子插件。

说实话,我对Get-ChildItem -Recurse | ?{ $_.PSIsContainer }命令只是子文件夹感到满意。

我想要的是什么,还是超出我的掌控范围?

提前感谢您的努力和信息。

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

目录需要如下所示:

根:\

根:\ Dir_n1

根:\ Dir_n1 \ Dir_n2 \

根:\ Dir_N1 \ Dir_n2 \ Dir_n3

2 个答案:

答案 0 :(得分: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
 '**********************************************************************************************

答案 1 :(得分:0)

我把头靠在墙上直到鸡蛋破了之后,我已经找到了正确的命令。

Get-ChildItem **** | ?{$ _.PSIsContainer} | select-object FullName | out-string -Width 255&gt; Table_of_Contents.txt

感谢您的帮助和帮助。