powershell get-childitem到csv的详细信息

时间:2013-03-07 01:02:28

标签: powershell directory enumerate

我正在尝试为具有深层文件夹结构的文件共享创建CSV文件。

我希望CSV看起来像:

filename, filepath, file type, folderStructure

到目前为止,我有以下内容:

#Get-ChildItem -Path D:\ -Recurse
$directory="d:\"
gci $directory -recurse |
where {$_.psiscontainer} |
foreach {
    get-childitem $_.fullname |
    sort creationtime |
    select -expand fullname -last 1
}

3 个答案:

答案 0 :(得分:5)

你不需要在Powershell中递归递归。它将自动遍历子目录的所有子目录。

我也不太了解你想要的一些信息,但是这里有一个脚本可以完成我想要的,我相信并且IMO可以更好地阅读。

Get-ChildItem -Path X:\Test -Recurse |`
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName
$Folder = $_.PSIsContainer
$Age = $_.CreationTime

$Path | Select-Object `
    @{n="Name";e={$Item}},`
    @{n="Created";e={$Age}},`
    @{n="filePath";e={$Path}},`
    @{n="Extension";e={if($Folder){"Folder"}else{$Type}}}`
}| Export-Csv X:\Test\Results.csv -NoTypeInformation 

您需要更改路径,因为我为测试创建了此路径。我的结果在Excel中看起来像这样:

+-------------------------------+----------------+-------------------------------------+-----------+
|             Name              |    Created     |              filePath               | Extension |
+-------------------------------+----------------+-------------------------------------+-----------+
|         test2                 | 3/6/2013 19:21 | X:\Test\test2                       | Folder    |
|         Results.csv           | 3/6/2013 19:51 | X:\Test\Results.csv                 | .csv      |
|         test3                 | 3/6/2013 19:21 | X:\Test\test2\test3                 | Folder    |
|         New Text Document.txt | 3/6/2013 19:21 | X:\Test\test2\New Text Document.txt | .txt      |
+-------------------------------+----------------+-------------------------------------+-----------+

如果扩展名为“文件夹”,则只返回它是一个目录而不是空白(无扩展名)。

答案 1 :(得分:0)

好的,我改变了检查父母的方式。它不再直接查看Parent属性,它现在应该更正它。

    Get-ChildItem -Path D:\ -Recurse |`
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName

$ParentS = ($_.Fullname).split("\")
$Parent = $ParentS[@($ParentS.Length - 2)]

$Folder = $_.PSIsContainer
$Age = $_.CreationTime

$Path | Select-Object `
    @{n="Name";e={$Item}},`
    @{n="Created";e={$Age}},`
    @{n="Folder Name";e={if($Parent){$Parent}else{$Parent}}},`
    @{n="filePath";e={$Path}},`
    @{n="Extension";e={if($Folder){"Folder"}else{$Type}}}`
}| Export-Csv X:\Test\ResultsC.csv -NoTypeInformation 

现在正在获取当前项目的路径,通过拆分\来将其转换为数组,然后在ArryLength中为您提供值 - 2

答案 2 :(得分:0)

我不确定这是否是最佳方法,但它有效。我有一种感觉代码可以缩短以获得文件的完整路径。

$myDirectory = "D:\"
Get-ChildItem -Path $myDirectory -Recurse |`
foreach{
$Item = $_
$Type = $_.Extension
$Path = $_.FullName

$ParentS = ($_.Fullname).split("\")
$Parent = $ParentS[@($ParentS.Length - 2)]
$ParentPath = $_.PSParentPath
$ParentPathSplit = ($_.PSParentPath).split("::")
$ParentPathFinal = $ParentPathSplit[@($ParentPathSplit.Length -1)]
#$ParentPath = [io.path]::GetDirectoryName($myDirectory)

$Folder = $_.PSIsContainer
$Age = $_.CreationTime

$Path | Select-Object `
    @{n="Name";e={$Item}},`
    @{n="Created";e={$Age}},`
    @{n="Folder Name";e={if($Parent){$Parent}else{$Parent}}},`
    @{n="filePath";e={$Path}},`
    @{n="Extension";e={if($Folder){"Folder"}else{$Type}}},`
    @{n="Folder Name 2";e={if($Parent){$Parent}else{$Parent}}},`
    #@{n="Folder Path";e={$ParentPath}},`
    @{n="Folder Path 2";e={$ParentPathFinal}}`

}| Export-Csv d:\ResultsC_2_F.csv -NoTypeInformation 
相关问题