powershell / 将 .txt 文件合并到 csv 添加日期和文件名
powershell / merging .txt-files into csv adding date and filename
又是我,正如我昨天提到的,我是 Powershell 的新手(现在 3 天),我希望你能再次帮助我。
我想要的:
我想将不同的 txt 文件合并到一个 csv 文件中
加上添加的每一行都应以实际日期 (yyyy-mm-dd) 和文件名开头。
Expectation_Image
WhatIamActuallyGetting_Image
到目前为止我得到了什么:
New-Item Shoppinglist_$(get-date -f yyyyMMdd_hhmm).csv -ItemType File
$txtFiles = Get-ChildItem -Name *.txt
$desiredColumns = 'Date','Filename','Substance','Information','Comment'
ForEach ($file in $txtFiles) {
$csv = Import-Csv -path $file -Delimiter "`t"
$outcsv=$csv | Select-Object $desiredColumns
#I Think the mistake is somewhere here, but i habe no idea to fix it. :(
Select-Object *, @{Name = 'Date'; Expression = {(Get-Date -format s)}}
Select-Object *, @{Name = 'Filename'; Expression = {(GetFileName)}}
$outcsv | Export-Csv Shoppinglist_$(get-date -f yyyyMMdd_hhmm).csv -NoTypeInformation -Delimiter ";" -Append
}
我希望世界之外有人可以帮助我。 :)
您使用计算属性是对的,但是有点想多了。
此外,Get-ChildItem
returns FileInfo 或 DirectoryInfo 对象。 (除非您指定开关 -Name
,在那种情况下它 returns 仅路径中项目的名称)。
这些对象具有有用的属性,例如 FullName、Name、LastWriteTime 等
由于您只想返回 files,因此可以使用 -File
开关。
这假设两个输入文件都具有与示例中完全相同的列:
# the folder where the input files are and where the output csv file should be saved
$path = 'D:\Test'
$today = '{0:yyyy-MM-dd}' -f (Get-Date)
$txtFiles = Get-ChildItem -Path $path -Filter '*.txt' -File
$csv = foreach ($file in $txtFiles) {
Import-Csv -Path $file.FullName -Delimiter "`t" |
Select-Object @{Name = 'Date'; Expression = {$today}},
@{Name = 'Filename'; Expression = {$file.Name}}, *
}
$fileOut = Join-Path -Path $path -ChildPath ('Shoppinglist_{0:yyyyMMdd_HHmm}.csv' -f (Get-Date))
$csv | Export-Csv -Path $fileOut -Delimiter ";" -NoTypeInformation
这假设两个输入文件至少有 3 个所需的列:'Substance'、'Information' 和 'Comment'
# the folder where the input files are and where the output csv file should be saved
$path = 'D:\Test'
$today = '{0:yyyy-MM-dd}' -f (Get-Date)
$txtFiles = Get-ChildItem -Path $path -Filter '*.txt' -File
$csv = foreach ($file in $txtFiles) {
Import-Csv -Path $file.FullName -Delimiter "`t" |
Select-Object @{Name = 'Date'; Expression = {$today}},
@{Name = 'Filename'; Expression = {$file.Name}},
Substance, Information, Comment
}
$fileOut = Join-Path -Path $path -ChildPath ('Shoppinglist_{0:yyyyMMdd_HHmm}.csv' -f (Get-Date))
$csv | Export-Csv -Path $fileOut -Delimiter ";" -NoTypeInformation
如果您使用的 PowerShell 版本低于 3.0,则无法使用 -File
开关。然后使用:$txtFiles = Get-ChildItem -Path $path -Filter '*.txt' | Where-Object { !$_.PSIsContainer }
又是我,正如我昨天提到的,我是 Powershell 的新手(现在 3 天),我希望你能再次帮助我。
我想要的: 我想将不同的 txt 文件合并到一个 csv 文件中 加上添加的每一行都应以实际日期 (yyyy-mm-dd) 和文件名开头。
Expectation_Image
WhatIamActuallyGetting_Image
到目前为止我得到了什么:
New-Item Shoppinglist_$(get-date -f yyyyMMdd_hhmm).csv -ItemType File
$txtFiles = Get-ChildItem -Name *.txt
$desiredColumns = 'Date','Filename','Substance','Information','Comment'
ForEach ($file in $txtFiles) {
$csv = Import-Csv -path $file -Delimiter "`t"
$outcsv=$csv | Select-Object $desiredColumns
#I Think the mistake is somewhere here, but i habe no idea to fix it. :(
Select-Object *, @{Name = 'Date'; Expression = {(Get-Date -format s)}}
Select-Object *, @{Name = 'Filename'; Expression = {(GetFileName)}}
$outcsv | Export-Csv Shoppinglist_$(get-date -f yyyyMMdd_hhmm).csv -NoTypeInformation -Delimiter ";" -Append
}
我希望世界之外有人可以帮助我。 :)
您使用计算属性是对的,但是有点想多了。
此外,Get-ChildItem
returns FileInfo 或 DirectoryInfo 对象。 (除非您指定开关 -Name
,在那种情况下它 returns 仅路径中项目的名称)。
这些对象具有有用的属性,例如 FullName、Name、LastWriteTime 等
由于您只想返回 files,因此可以使用 -File
开关。
这假设两个输入文件都具有与示例中完全相同的列:
# the folder where the input files are and where the output csv file should be saved
$path = 'D:\Test'
$today = '{0:yyyy-MM-dd}' -f (Get-Date)
$txtFiles = Get-ChildItem -Path $path -Filter '*.txt' -File
$csv = foreach ($file in $txtFiles) {
Import-Csv -Path $file.FullName -Delimiter "`t" |
Select-Object @{Name = 'Date'; Expression = {$today}},
@{Name = 'Filename'; Expression = {$file.Name}}, *
}
$fileOut = Join-Path -Path $path -ChildPath ('Shoppinglist_{0:yyyyMMdd_HHmm}.csv' -f (Get-Date))
$csv | Export-Csv -Path $fileOut -Delimiter ";" -NoTypeInformation
这假设两个输入文件至少有 3 个所需的列:'Substance'、'Information' 和 'Comment'
# the folder where the input files are and where the output csv file should be saved
$path = 'D:\Test'
$today = '{0:yyyy-MM-dd}' -f (Get-Date)
$txtFiles = Get-ChildItem -Path $path -Filter '*.txt' -File
$csv = foreach ($file in $txtFiles) {
Import-Csv -Path $file.FullName -Delimiter "`t" |
Select-Object @{Name = 'Date'; Expression = {$today}},
@{Name = 'Filename'; Expression = {$file.Name}},
Substance, Information, Comment
}
$fileOut = Join-Path -Path $path -ChildPath ('Shoppinglist_{0:yyyyMMdd_HHmm}.csv' -f (Get-Date))
$csv | Export-Csv -Path $fileOut -Delimiter ";" -NoTypeInformation
如果您使用的 PowerShell 版本低于 3.0,则无法使用 -File
开关。然后使用:$txtFiles = Get-ChildItem -Path $path -Filter '*.txt' | Where-Object { !$_.PSIsContainer }