简体   繁体   English

Powershell在给定路径中获取扩展文件

[英]Powershell get extension files within a given path

The main idea of this post is that, given a path to folder or disk. 这篇文章的主要思想是,给出文件夹或磁盘的路径。 Then powershell will search all file and folder inside this path and list all the extension file current stored in this path (docx, xlsx, jpg,...) 然后,powershell将搜索该路径内的所有文件和文件夹,并列出当前存储在该路径中的所有扩展文件(docx,xl​​sx,jpg等)。

The final results will be stored in csv file. 最终结果将存储在csv文件中。

I think you want something like this, but could be I misunderstood your question. 我认为您想要这样的东西,但可能是我误解了您的问题。

$Folder = "C:\Temp\"
Get-ChildItem $Folder | Select Extension | Where-Object{$_.Extension -ne ""} | Export-Csv -Path C:\Temp\YourCSV.csv -NoTypeInformation

I've made a little fix in the svanzundert solution 我在svanzundert解决方案中做了一些修复

$Folder = "C:\Temp\"
Get-ChildItem $Folder | Select Extension -Unique | Where-Object{$_.Extension -ne ""} | Export-Csv -Path C:\Temp\YourCSV.csv -NoTypeInformation

You just have to use -Unique parameter in the Select-Object 您只需要在Select-Object中使用-Unique参数

For sure there will be better ways, like use filter to get from childs files onlz but zou can do something like follows: 当然,会有更好的方法,例如使用filter从子文件onlz中获取,但是zou可以执行以下操作:

#will load directory content
$files = Get-ChildItem "C:\Users\fooo\Documents\" 

#empty array for extensions preparation
$extensions = New-Object System.Collections.ArrayList($null)

#iterate over items= files and directories
for ($i=0; $i -lt $files.Count; $i++) {
    #print all (once by one of course)
    #Write-Host $files[$i].FullName
    #print extension only
    #Write-Host $files[$i].Extension

    #store extensions by unique, if its a file
    if( ($files[$i].GetType().Name -eq "FileInfo" ) -and !$extensions.Contains($files[$i].Extension) ){
        $extensions.Add($files[$i].Extension)
    }
}

Write-Host $extensions

my output: 我的输出:

.gitattributes .gitignore .mailmap .yml .cmd .fsx .sh .md .dependencies .lock .sln .DotSettings

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM