简体   繁体   English

Powershell 拆分文件名导出到csv

[英]Powershell split file name and export to csv

I have lot of files in the folder in this nameing format "yyyy-mm-dd_Discription_$Amount.pdf", how do I make powershell script that creates csv file with three columns (Date, Description and Amount)?我的文件夹中有很多文件采用这种命名格式“yyyy-mm-dd_Discription_$Amount.pdf”,如何制作 powershell 脚本来创建包含三列(日期、描述和金额)的 csv 文件?

I am able to extract the full file name in the below, but I need help to split and make columns.我能够在下面提取完整的文件名,但我需要帮助来拆分和制作列。

$Directory = "C:\path to directory"
Get-ChildItem -Path $Directory -Recurse -Force | ForEach {

    [PSCustomObject]@{                 
        Name = $_.BaseName          
    }
    
} | Export-Csv -Path "./temp.csv" -NoTypeInformation


I have tried this below我在下面试过这个

$Directory = "C:\path to directory"
Get-ChildItem -Path $Directory -Recurse -Force | ForEach {

    [PSCustomObject]@{                 
        Name = $_.BaseName          
    }
    
} | Export-Csv -Path "./temp.csv" -NoTypeInformation


This will work这将工作

$Directory = '.'

(Get-ChildItem -Path $Directory -File).BaseName | Select `
    @{l='Date'; e={$_.Split('_')[0]}},
    @{l='Description'; e={$_.Split('_')[1]}},
    @{l='Amount'; e={$_.Split('_')[2]}} |
        ConvertTo-Csv -NoTypeInformation

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

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