2013-03-07 19 views
0

我试图创建具有深层文件夹结构的文件共享CSV文件为CSV。PowerShell中得到-childitem与细节

我想要的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 
} 
+0

你能伪造你想要的输出看起来像什么吗? 例如: image.png,D:\ images,.png,D:\ images lister.csv,D:\ images \ Lists \,.csv,D:\ images \ Lists – 2013-03-07 01:05:34

+0

我明白,但你能做一些数据(假的格式可以),所以我确认你想要什么?尤其是夹层结构。你要像 d:\ +图像 ++ image1.jpg +列表 ++出口 +++ export1.xlsx +++ Export4.csv – 2013-03-07 01:14:16

回答

4

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

它说:“文件夹”,为推广只是把它返回它是一个目录,而不是一个空白的(没有扩展名)。

+0

该属性是家长,所以,如果你改变$ 。_.Fullname至$ _家长会工作,但为了可读性,你也可以改变变量名$家长,然后更新行@ {N =“文件路径”; E = {$ PATH}}'来@ {n =“文件夹名称”; e = {$ Parent}},' – 2013-03-07 03:00:32

+0

呃,在那个短视。因为你将在根目录“D:\”你也需要填充它。否则,你最终会与d没有出口:\ 那么改变你的选择对象,以 @ {N =“文件夹名称”; E = {如果($父){$母公司)其他(“根”}}} ' – 2013-03-07 03:08:26

+0

GET-ChildItem -Path d:。\ -Recurse |' 的foreach {$ 项目= $ _ $ 类型= $ _扩展 $ PATH = $ _全名 $ FOLDERPATH = $ _父 $ Folder = $ _。PSIsContainer $ Age = $ _。CreationTime $ Path |选择对象' @ {n =“Name”; e = {$ Item}},' @ {n =“Created”; e = {$ Age}},' @ {n =“filePath”; e = {$ Path}},' @ {n =“文件夹名称”; e = {$ FolderPath}},' @ {n =“Extension”; e = {if($ Folder){“Folder”} else {$ Type}}}' } | Export-Csv d:\ Results.csv -NoTypeInformation – torres 2013-03-07 03:20:12

0

OK,我改变了它检查父的方式。它不再直接查看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

+0

我应该在这里做什么来获取完整路径(减去fileName )? $ ParentS [@($ ParentS.Length - 2)] – torres 2013-03-07 12:58:24

+0

我添加了一个新的变量$ ParentPath = $ _。PSParentPath和它的工作,但给我这个“Microsoft.PowerShell.Core \ FileSystem :: D:\ Install Files \ AutoSPInstaller \ SP2010 \ AutoSPInstaller“。 “Microsoft.PowerShell.Core \ FileSystem ::”是额外的,我不需要。 – torres 2013-03-07 13:17:41

+0

Fullname是返回完整路径的属性,如D:\ Folder1 \ Images \ Image1.jpg, $ ParentS =($ _。Fullname).split(“\”) $ Parent = $ ParentS [@($ ParentS .Length - 2)] 这给你的文件夹路径 - 实际的文件名。 – 2013-03-07 14:07:58

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 
+0

是的,FullName是全名属性。 有了PS,你也可以做一个 DIR | FL * 这将为您提供所有可用属性(有些不会在get-childitems中返回值),然后您可以使用$ _。AttributeName来获取该值。一旦你知道什么工作,你就可以做字符串操作。代码可能更短,但为什么呢?可读性比保存几行更重要,执行时间和文件大小的差异几乎没有。 – 2013-03-07 14:13:08