powershell/将.txt文件合并到csv中,添加日期和文件名
又是我,就像我昨天提到的那样,我是Powershell的新手(现在是3天),希望您能再次为我提供帮助.
it's me again, as i mentioned yesterday i'm new to Powershell (now 3 days) and i hope you can help me again.
我想要什么: 我想将不同的txt文件合并到一个csv文件中 加上每行添加的内容应以实际日期(yyyy-mm-dd)和文件名开头.
What I want: I want to merge different txt-files into one csv-file PLUS every line which is added should start with the actual date (yyyy-mm-dd) and the filename.
所以我到目前为止:
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
}
我希望世界上有人可以帮助我. :)
I hope there is someone outside in the world who can help me. :)
使用计算所得的属性是正确的,但是对此有些考虑过.
同样,Get-ChildItem
返回FileInfo或DirectoryInfo对象. (除非您指定了开关-Name
,否则它将仅返回路径中项目的名称.)
You are right to use calculated properties, but are overthinking this a bit.
Also, Get-ChildItem
returns FileInfo or DirectoryInfo objects. (unless you specify switch -Name
, in that case it returns only the names of the items in the path).
这些对象具有有用的属性,例如FullName,Name,LastWriteTime等.
由于只希望返回文件,因此可以使用-File
开关.
These objects have useful properties, such as FullName, Name, LastWriteTime, etc.
Since you only want files returned, you can use the -File
switch.
这假设两个输入文件的列与示例中的列完全相同:
This assumes both input files have the exact same columns as in your example:
# 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个所需的列:物质",信息"和评论"
This assumes both input files have at least the 3 desired columns: 'Substance','Information' and '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
如果使用的是低于3.0的PowerShell版本,则不能使用-File
开关.而是使用:$txtFiles = Get-ChildItem -Path $path -Filter '*.txt' | Where-Object { !$_.PSIsContainer }
If you are using a PowerShell version below 3.0, you cannot use the -File
switch. Instead then use: $txtFiles = Get-ChildItem -Path $path -Filter '*.txt' | Where-Object { !$_.PSIsContainer }