繁体   English   中英

Powershell File按日期搜索,显示日期和路径信息,无法导出CSV

[英]Powershell File search by date, display date and path info, cannot export CSV

我是Powershell的新手,我已经获取了批处理文件,可以将结果输出到屏幕上。 我希望将文件导出为CSV,以便能够将其放入按目录(然后按日期)排序的Excel文件中。

我可以使用Out-File命令写入基本文件,但是CSV输出似乎并不能正常工作。 我只得到带有每个记录的Length的CSV文件。

有人可以帮忙吗? 提前致谢。


这是我尝试的:

#--Change the name with directory you want to visit --#    
$dir_to_look="E:\xPression\CustomerData\xPressionECR\*\XMLDATA"    

#--You may change the number of days of your choice --#   
$TwoDays=$(Get-Date).AddDays(-2)    

Remove-Item E:\xPression\CustomerData\xPressionECR\ReportOutput\Report.csv

#--Find the files which are modified or created within last 2 days --#    
Get-Childitem $dir_to_look *.xml  -Recurse | `   
        where-object {!($_.psiscontainer)} | `   
        where { $_.LastWriteTime -gt $TwoDays } | `   
        ForEach-Object {  "$($_.LastWriteTime) , $($_.Fullname)"| Out-File -LiteralPath E:\xPression\CustomerData\xPressionECR\ReportOutput\Report.csv -Append}

您应该仔细研究的两个有用的功能是Select-ObjectExport-Csv

如果要创建包含由LastWriteTimeFullName组成的两行的.csv文件,则可以这样操作:

Get-Childitem $dir_to_look *.xml  -Recurse | Where-Object { $_.LastWriteTime -gt $TwoDays } | Select-Object LastWriteTime, FullName | Export-Csv -LiteralPath "E:\xPression\CustomerData\xPressionECR\ReportOutput\Report.csv "

您应该在管道末端使用Export-Csv而不是Out-File。 您的代码将变为:

Get-Childitem $dir_to_look *.xml -Recurse  -File | 
where LastWriteTime -gt $TwoDays  | 
select Fullname, LastWriteTime |
Export-Csv -Path E:\xPression\CustomerData\xPressionECR\ReportOutput\Report.csv -NoTypeInformation

如果在Get-ChildItem上使用-File参数,则无需测试PSisContainer。 您可以使用select来传递您感兴趣的两个属性,然后输出到csv文件。 您在第二个where语句末尾的管道符号后也不需要`。 行尾的管道符号自动是行继续标记。

希望这能回答您的问题。 如果您还有其他要询问的有关代码的信息,我们很乐意跟进。

暂无
暂无

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

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