简体   繁体   English

我可以将这些CSV文件合并为1个更大的CSV文件吗?

[英]Can I combine these CSV files into 1 larger CSV file?

My Old Bat file Copy F:\\File.hdr+F:*.csv F:\\FinalOutput.csv 我的旧蝙蝠文件复制F:\\ File.hdr + F:*。csv F:\\ FinalOutput.csv

the HDR file is a single entry file that has only header data for the CSV files HDR文件是单个条目文件,仅包含CSV文件的标题数据

Is there a way to perform this in PowerShell (to combine all the CSV files into a single file)? 有没有一种方法可以在PowerShell中执行(将所有CSV文件合并为一个文件)?

Here is my powershell script that doesn't work 这是我的PowerShell脚本不起作用

$CSVFolder = 'F:\Input\';
$OutputFile = 'F:\Output\NewOutput.csv';    
$CSV= @();    
Get-ChildItem -Path $CSVFolder -Filter *.inv | ForEach-Object { 
    $CSV += @(Import-Csv -Path $CSVFolder\$_)
}    
$CSVHeader = Import-Csv 'F:\Input\Headings.hdr'    
$CSV = $CSVHeader + $CSV    
$CSV | Export-Csv -Path $OutputFile -NoTypeInformation -Force;

I get the list of FileNames that are exported and not the content of the Files. 我得到了导出的文件名列表,而不是文件的内容。 The script is also modifying the date/time stamp on my INV files. 该脚本还在修改我的INV文件上的日期/时间戳。 It shouldn't be doing that. 它不应该那样做。

You can skip the whole CSV bit if you just append the files as you would before. 如果您只是像以前那样附加文件,则可以跳过整个CSV位。

Something like this should work: 这样的事情应该起作用:

# First we create the new file and add the header.
get-content $headerfile | set-content $outputfile

# Then we get the input files, read them out with get-content 
# and append them to the output file (add-content).
get-childitem -path $csvfolder *.inv | get-content | add-content $outputfile

The CSV commandlets are handy if you want to be processing the CSV data in your script, but in your case simply appending the files will do the trick. 如果要在脚本中处理CSV数据,则CSV命令小工具很方便,但是在您的情况下,只需添加文件即可解决问题。 Not bothering with the CSV conversion will be a lot faster as Powershell doesn't have to parse the CSV lines and create PS-objects. 由于Powershell不必解析CSV行并创建PS对象,因此不必担心CSV转换会快得多。 It's really fast with pure text though. 纯文本确实非常快。

Another trick here is how the get-content and add-content are used in the pipeline. 这里的另一个技巧是如何在管道中使用get-content和add-content。 Since they are aware of the pipeline you can pass in file objects without having to use a foreach loop. 由于他们知道管道,因此您可以传递文件对象,而不必使用foreach循环。 This makes your statements a lot shorter. 这使您的陈述短了很多。

How about: 怎么样:

get-childitem *.inv | foreach-object {
  import-csv $_ -header (get-content Headings.hdr)
} | export-csv NewOutput.csv -notypeinformation

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

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