【问题标题】:powershell get-childitem to csv with detailspowershell get-childitem 到 csv 的详细信息
【发布时间】:2013-02-22 01:30:55
【问题描述】:

我正在尝试为具有深层文件夹结构的文件共享创建 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
}

【问题讨论】:

  • 你能伪造你希望输出的样子吗?例如:image.png、D:\images、.png、D:\images lister.csv、D:\images\Lists\、.csv、D:\images\Lists
  • 我明白,但是你能做一些数据吗(格式可以假)所以我确认你想要什么?特别是文件夹结构。你想要 D:\ +images ++image1.jpg +lists ++Exports +++export1.xlsx +++Export4.csv

标签: powershell directory enumerate


【解决方案1】:

您不需要在 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      |
+-------------------------------+----------------+-------------------------------------+-----------+

其中显示“文件夹”的扩展名只是返回它是一个目录而不是空白(无扩展名)。

【讨论】:

  • 那个属性是Parent,所以如果你把$_.Fullname改成$_.Parent就行了,但是为了可读性你也可以把变量名改成$Parent,然后更新@ {n="filePath";e={$Path}},` to @{n="Folder Name";e={$Parent}},`
  • 哎呀,目光短浅。因为您将位于根“D:\”,所以您也需要填充它。否则你最终会在 D:\ 上没有导出所以将你的选择对象更改为 @{n="Folder Name";e={if($Parent){$Parent}else{"Root"}}},`
  • Get-ChildItem -Path d:\ -Recurse |` foreach{ $Item = $_ $Type = $_.Extension $Path = $_.FullName $FolderPath = $_.Parent $Folder = $_.PSIsContainer $Age = $_.CreationTime $Path |选择对象`@{n="Name";e={$Item}},`@{n="Created";e={$Age}},`@{n="filePath";e={$路径}},`@{n="文件夹名称";e={$FolderPath}},`@{n="Extension";e={if($Folder){"Folder"}else{$Type}} }` }|导出-Csv d:\Results.csv -NoTypeInformation
  • 你在解析什么?丢失了多少文件?还是整个目录? PS窗口中的任何错误?从 PowerGUI 运行它或从打开的 PS shell 调用它,这样它就不会自动关闭。
  • 我认为它没有报告根容器,添加它并再次发布结果。这里也很晚,所以叫它一个晚上。谁知道呢,旧的大脑可能一开始就不能正常工作……@{n="Container";e={$Folder}},`
【解决方案2】:

好的,我改变了它检查父级的方式。它不再直接查看 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 处为您提供值

【讨论】:

  • 我应该怎么做才能得到完整的路径(减去文件名)? $ParentS[@($ParentS.Length - 2)]
  • 我添加了一个新变量 $ParentPath = $_.PSParentPath 它有点工作但给了我这个“Microsoft.PowerShell.Core\FileSystem::D:\Install Files\AutoSPInstaller\SP2010\AutoSPInstaller” . “Microsoft.PowerShell.Core\FileSystem::”是多余的,我不需要。
  • Fullname 是返回完整路径的属性,如 D:\Folder1\Images\Image1.jpg, $ParentS = ($_.Fullname).split("\") $Parent = $ParentS [@($ParentS.Length - 2)] 这为您提供了文件夹路径 - 实际文件名。
【解决方案3】:

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

$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 

【讨论】:

  • 是的,FullName 是全名属性。有了PS,你也可以做一个DIR | FL * 这将为您提供所有可用的属性(有些不会在 get-childitems 中返回值),然后您可以通过 $_.AttributeName 来获取该值。一旦你知道什么是有效的,你就可以进行字符串操作。代码可以更短,但为什么,真的?可读性比节省几行更重要,执行时间和文件大小差异几乎没有。
猜你喜欢
  • 2019-02-24
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
相关资源
最近更新 更多