Powershell-列出所有文件夹、子文件夹和每个包含的文件(递归),但采用格式化方式(树状视图)
我在powershell上使用以下命令创建特定目录中所有文件和子文件夹的列表:Powershell-列出所有文件夹、子文件夹和每个包含的文件(递归),但采用格式化方式(树状视图),powershell,sorting,directory,get-childitem,Powershell,Sorting,Directory,Get Childitem,我在powershell上使用以下命令创建特定目录中所有文件和子文件夹的列表: get-childitem -path c:\users\username\desktop\test -recurse | select name 因此,假设我的桌面上有一个名为“test”的文件夹,在这个文件夹中,我有三个文件和一个子文件夹,它本身包含更多的文件和子文件夹等等,我会得到如下输出: subfolder 1 of "test" file 1 in "test"
get-childitem -path c:\users\username\desktop\test -recurse | select name
因此,假设我的桌面上有一个名为“test”的文件夹,在这个文件夹中,我有三个文件和一个子文件夹,它本身包含更多的文件和子文件夹等等,我会得到如下输出:
subfolder 1 of "test"
file 1 in "test"
file 2 in "test"
file 3 in "test"
subfolder a of "subfolder 1"
file 1 in subfolder 1
file 2 in subfolder 1
file 3 in subfolder 1
file 1 in subfolder a
file 2 in subfolder a
file 3 in subfolder a
这很好,但我想得到另一种输出,如下所示:
+ c:\users\username\desktop\test
| - file 1 in "test"
| - file 2 in "test"
| - file 3 in "test"
|--+ subfolder 1 of "test"
| | - file 1 in subfolder 1
| | - file 2 in subfolder 1
| | - file 3 in subfolder 1
| |--+ subfolder a of "subfolder 1"
| | | - file 1 in subfolder a
| | | - file 2 in subfolder a
| | | - file 3 in subfolder a
|--+ subfolder 2 of "test"
| | -
| | .
| . .
. .
.
有没有可能(如果有,怎么做?)得到这样的输出?
我知道当时有一个名为“tree”的dos命令,但由于其局限性,它无法在powershell中处理get childitem的输出。powershell中是否有某种等效命令,或者我可以使用get childitem命令及其开关/additions/
对不起,我的英语不好。
还有:对不起,我对powershell完全是个初学者
感谢您的帮助。您习惯于从cmd中获取的旧“树”是system32文件夹中的应用程序,而不是一些硬编码的cmd功能
因此,您仍然可以像往常一样从powershell运行它
e、 g
Robocopy和其他一些著名的应用程序也以同样的方式工作
外部程序中的错误可以在$LastExitCode而不是通常的$Error中捕获。这些代码的含义将因程序而异。您从cmd中习惯的旧“树”是system32文件夹中的应用程序,而不是一些硬编码的cmd功能
因此,您仍然可以像往常一样从powershell运行它
e、 g
Robocopy和其他一些著名的应用程序也以同样的方式工作
外部程序中的错误可以在$LastExitCode而不是通常的$Error中捕获。这些代码的含义因程序而异。您可以从Powershell调用任何cmd/DOS可执行文件。只要你做得好。在consolehost(powershell.exe/pwsh.exe)中,它实际上与使用cmd.exe相同,但在ISE中有所不同。不能在ISE中使用交互式命令。您可以使用该命令,但必须传递它所需的所有信息 在PowerShell控制台主机(PowerShell.exe/pwsh.exe)中,只需键入
$PSVersionTable
Name Value
---- -----
PSVersion 5.1.19041.1
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.1}
BuildVersion 10.0.19041.1
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
tree |more
Folder PATH listing for volume Data
Volume serial number is CE3D-F392
D:.
├───.vs
│ └───Scripts
│ └───v16
├───.vscode
...
没有理由从头开始。提供此功能的示例甚至模块很多
一个快速的搜索会显示你开始调整或使用,因为他们是
使用PowerShell社区扩展项目中的Show Tree cmdlet:
Find-Module -Name pscx |
Format-Table -AutoSize
# Results
<#
Version Name Repository Description
------- ---- ---------- -----------
3.3.2 Pscx PSGallery PowerShell Community Extensions (PSCX) base module which implements a general-purpose set of Cmdlets.
#>
Show-Tree e:\data –depth 2
使用PowerShell创建文件系统大小树视图:
https://key2consulting.com/powershell-file-directory-tree-view
#Variables that need to be set for each run
$startFolder = "C:\Program Files"; #The starting folder to analyze
$sourceHTMLFile = "C:\finalTemplate.html"; #The html source template file
$destinationHTMLFile = "C:\final.html"; #The final html file that will be produced, #does not need to exist
$htmlLines = @();
#Function that creates a folder detail record
function CreateFolderDetailRecord
{
param([string]$FolderPath)
#Get the total size of the folder by recursively summing its children
$subFolderItems = Get-ChildItem $FolderPath -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
$folderSizeRaw = 0;
$folderSize = 0;
$units = "";
#Account for no children
if($subFolderItems.sum -gt 0)
{
$folderSizeRaw = $subFolderItems.sum;
}
#Determine units for a more friendly output
if(($subFolderItems.sum / 1GB) -ge 1)
{
$units = "GB"
$folderSize = [math]::Round(($subFolderItems.sum / 1GB),2)
}
else
{
if(($subFolderItems.sum / 1MB) -ge 1)
{
$units = "MB"
$folderSize = [math]::Round(($subFolderItems.sum / 1MB),2)
}
else
{
$units = "KB"
$folderSize = [math]::Round(($subFolderItems.sum / 1KB),2)
}
}
#Create an object with the given properties
$newFolderRecord = New-Object –TypeName PSObject
$newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderPath –Value $FolderPath;
$newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderSizeRaw –Value $folderSizeRaw
$newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderSizeInUnits –Value $folderSize;
$newFolderRecord | Add-Member –MemberType NoteProperty –Name Units –Value $units;
return $newFolderRecord;
}
#Function that recursively creates the html for the output, given a starting location
function GetAllFolderDetails
{
param([string]$FolderPath)
$recursiveHTML = @();
#Get properties used for processing
$folderItem = Get-Item -Path $FolderPath
$folderDetails = CreateFolderDetailRecord -FolderPath $FolderPath
$subFolders = Get-ChildItem $FolderPath | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
#If has subfolders, create hmtl drilldown.
if($subFolders.Count -gt 0)
{
$recursiveHTML += "<li><span class='caret'>" + $folderItem.Name + " (<span style='color:red'>" + $folderDetails.FolderSizeInUnits + " " + $folderDetails.Units + "</span>)" + "</span>"
$recursiveHTML += "<ul class='nested'>"
}
else
{
$recursiveHTML += "<li>" + $folderItem.Name + " (<span style='color:red'>" + $folderDetails.FolderSizeInUnits + " " + $folderDetails.Units + "</span>)";
}
#Recursively call this function for all subfolders
foreach($subFolder in $subFolders)
{
$recursiveHTML += GetAllFolderDetails -FolderPath $subFolder.FullName;
}
#Close up all tags
if($subFolders.Count -gt 0)
{
$recursiveHTML += "</ul>";
}
$recursiveHTML += "</li>";
return $recursiveHTML
}
#Processing Starts Here
#Opening html
$htmlLines += "<ul id='myUL'>"
#This function call will return all of the recursive html for the startign folder and below
$htmlLines += GetAllFolderDetails -FolderPath $startFolder
#Closing html
$htmlLines += "</ul>"
#Get the html template, replace the template with generated code and write to the final html file
$sourceHTML = Get-Content -Path $sourceHTMLFile;
$destinationHTML = $sourceHTML.Replace("[FinalHTML]", $htmlLines);
$destinationHTML | Set-Content $destinationHTMLFile
https://key2consulting.com/powershell-file-directory-tree-view
#需要为每次运行设置的变量
$startFolder=“C:\Program Files”#要分析的起始文件夹
$sourceHTMLFile=“C:\finalTemplate.html”#html源模板文件
$destinationHTMLFile=“C:\final.html”#将生成的最终html文件不需要存在
$htmlines=@();
#创建文件夹详细信息记录的函数
函数CreateFolderDetailRecord
{
参数([string]$FolderPath)
#通过递归求和子文件夹的总大小
$subFolderItems=Get ChildItem$FolderPath-recurse-force | Where Object{$$\ PSIsContainer-eq$false}|度量对象-属性长度-总和|选择对象总和
$folderSizeRaw=0;
$folderSize=0;
$units=“”;
#没有孩子
如果($subFolderItems.sum-gt 0)
{
$folderSizeRaw=$subFolderItems.sum;
}
#为更友好的输出确定单位
如果($subFolderItems.sum/1GB)-ge 1)
{
$units=“GB”
$folderSize=[math]::四舍五入($subFolderItems.sum/1GB),2)
}
其他的
{
如果($subFolderItems.sum/1MB)-ge 1)
{
$units=“MB”
$folderSize=[math]::四舍五入($subFolderItems.sum/1MB),2)
}
其他的
{
$units=“KB”
$folderSize=[math]::四舍五入($subFolderItems.sum/1KB),2)
}
}
#创建具有给定属性的对象
$newFolderRecord=新对象–类型名称PSObject
$newFolderRecord |添加成员–MemberType NoteProperty–名称FolderPath–值$FolderPath;
$newFolderRecord |添加成员–成员类型NoteProperty–名称FolderSizeRaw–值$FolderSizeRaw
$newFolderRecord |添加成员–成员类型NoteProperty–名称FolderSizeInUnits–值$folderSize;
$newFolderRecord |添加成员–成员类型NoteProperty–名称单位–值$Units;
返回$newFolderRecord;
}
#函数,该函数在给定起始位置的情况下,递归地为输出创建html
函数GetAllFolderDetails
{
参数([string]$FolderPath)
$recursiveHTML=@();
#获取用于处理的属性
$folderItem=获取项-路径$FolderPath
$folderDetails=CreateFolderDetailRecord-FolderPath$FolderPath
$subFolders=Get ChildItem$FolderPath | Where Object{$\ PSIsContainer-eq$true}| Sort Object
#如果有子文件夹,则创建hmtl向下展开。
如果($subFolders.Count-gt 0)
{
$recursiveHTML++=“”++$folderItem.Name+”(“++$folderDetails.FolderSizeInUnits++++$folderDetails.Units++”
$recursiveHTML+=“”
}
其他的
{
$recursiveHTML++=“- ”++$folderItem.Name+”(“++$folderDetails.FolderSizeInUnits++++$folderDetails.Units+”);
}
#对所有子文件夹递归调用此函数
foreach($子文件夹中的子文件夹)
{
$recursiveHTML+=GetAllFolderDetails-FolderPath$subFolder.FullName;
}
#收尾
$objDriveLetters = GET-WMIOBJECT –query "SELECT * from win32_logicaldisk"
$form = New-Object System.Windows.Forms.Form
$treeView = New-Object System.Windows.Forms.TreeView
$treeView.Dock = 'Fill'
$treeView.CheckBoxes = $true
foreach ($iDrive in $objDriveLetters)
{
$DriveRoot = Get-Item $iDrive.DeviceID
#$FolderRoot = Get-ChildItem -Path $iDrive.DeviceID
$FolderRoot = Get-Item -Path $iDrive.DeviceID
$treeView.Nodes.Add($FolderRoot.FullName, $FolderRoot.FullName)
}
$form.Controls.Add($treeView)
$form.ShowDialog()
https://key2consulting.com/powershell-file-directory-tree-view
#Variables that need to be set for each run
$startFolder = "C:\Program Files"; #The starting folder to analyze
$sourceHTMLFile = "C:\finalTemplate.html"; #The html source template file
$destinationHTMLFile = "C:\final.html"; #The final html file that will be produced, #does not need to exist
$htmlLines = @();
#Function that creates a folder detail record
function CreateFolderDetailRecord
{
param([string]$FolderPath)
#Get the total size of the folder by recursively summing its children
$subFolderItems = Get-ChildItem $FolderPath -recurse -force | Where-Object {$_.PSIsContainer -eq $false} | Measure-Object -property Length -sum | Select-Object Sum
$folderSizeRaw = 0;
$folderSize = 0;
$units = "";
#Account for no children
if($subFolderItems.sum -gt 0)
{
$folderSizeRaw = $subFolderItems.sum;
}
#Determine units for a more friendly output
if(($subFolderItems.sum / 1GB) -ge 1)
{
$units = "GB"
$folderSize = [math]::Round(($subFolderItems.sum / 1GB),2)
}
else
{
if(($subFolderItems.sum / 1MB) -ge 1)
{
$units = "MB"
$folderSize = [math]::Round(($subFolderItems.sum / 1MB),2)
}
else
{
$units = "KB"
$folderSize = [math]::Round(($subFolderItems.sum / 1KB),2)
}
}
#Create an object with the given properties
$newFolderRecord = New-Object –TypeName PSObject
$newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderPath –Value $FolderPath;
$newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderSizeRaw –Value $folderSizeRaw
$newFolderRecord | Add-Member –MemberType NoteProperty –Name FolderSizeInUnits –Value $folderSize;
$newFolderRecord | Add-Member –MemberType NoteProperty –Name Units –Value $units;
return $newFolderRecord;
}
#Function that recursively creates the html for the output, given a starting location
function GetAllFolderDetails
{
param([string]$FolderPath)
$recursiveHTML = @();
#Get properties used for processing
$folderItem = Get-Item -Path $FolderPath
$folderDetails = CreateFolderDetailRecord -FolderPath $FolderPath
$subFolders = Get-ChildItem $FolderPath | Where-Object {$_.PSIsContainer -eq $true} | Sort-Object
#If has subfolders, create hmtl drilldown.
if($subFolders.Count -gt 0)
{
$recursiveHTML += "<li><span class='caret'>" + $folderItem.Name + " (<span style='color:red'>" + $folderDetails.FolderSizeInUnits + " " + $folderDetails.Units + "</span>)" + "</span>"
$recursiveHTML += "<ul class='nested'>"
}
else
{
$recursiveHTML += "<li>" + $folderItem.Name + " (<span style='color:red'>" + $folderDetails.FolderSizeInUnits + " " + $folderDetails.Units + "</span>)";
}
#Recursively call this function for all subfolders
foreach($subFolder in $subFolders)
{
$recursiveHTML += GetAllFolderDetails -FolderPath $subFolder.FullName;
}
#Close up all tags
if($subFolders.Count -gt 0)
{
$recursiveHTML += "</ul>";
}
$recursiveHTML += "</li>";
return $recursiveHTML
}
#Processing Starts Here
#Opening html
$htmlLines += "<ul id='myUL'>"
#This function call will return all of the recursive html for the startign folder and below
$htmlLines += GetAllFolderDetails -FolderPath $startFolder
#Closing html
$htmlLines += "</ul>"
#Get the html template, replace the template with generated code and write to the final html file
$sourceHTML = Get-Content -Path $sourceHTMLFile;
$destinationHTML = $sourceHTML.Replace("[FinalHTML]", $htmlLines);
$destinationHTML | Set-Content $destinationHTMLFile