powershell/将.txt文件合并到csv中,添加日期和文件名 [英] powershell / merging .txt-files into csv adding date and filename

查看:313
本文介绍了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.

期望图像

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 
}

我希望世界上有人可以帮助我. :)

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 }

这篇关于powershell/将.txt文件合并到csv中,添加日期和文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆