合并多个CSV文件合并成一个使用PowerShell的 [英] Merging multiple CSV files into one using PowerShell

查看:2016
本文介绍了合并多个CSV文件合并成一个使用PowerShell的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在寻找PowerShell脚本这将合并目录中的所有CSV文件到一个文本文件(.txt)。所有CSV文件有它总是存储在每个文件的第一行相同的标题。所以,我需要从标题的第一个文件,但这些文件的其余部分中的第一行应该被忽略。
我能找到这是做正是我需要的批处理文件,但我有超​​过4000的CSV文件在一个目录,它需要超过45分钟来完成这项工作。

Hello I'm looking for powershell script which would merge all csv files in a directory into one text file (.txt) . All csv files have same header which is always stored in a first row of every file. So I need to take header from the first file, but in rest of the files the first row should be skipped. I was able to find batch file which is doing exactly what I need, but I have more than 4000 csv files in a single directory and it takes more than 45 minutes to do the job.

@echo off
ECHO Set working directory
cd /d %~dp0
Deleting existing combined file
del summary.txt
setlocal ENABLEDELAYEDEXPANSION
set cnt=1
for %%i in (*.csv) do (
 if !cnt!==1 (
 for /f "delims=" %%j in ('type "%%i"') do echo %%j >> summary.txt
) else (
 for /f "skip=1 delims=" %%j in ('type "%%i"') do echo %%j >> summary.txt
 )
 set /a cnt+=1
 )

任何建议如何创建PowerShell脚本这将是比这一批code更有效率?

Any suggestion how to create powershell script which would be more efficient than this batch code?

感谢您。

约翰

推荐答案

这会将所有文件添加在一起读他们一次一个:

This will append all the files together reading them one at a time:

get-childItem "YOUR_DIRECTORY\*.txt" 
| foreach {[System.IO.File]::AppendAllText
 ("YOUR_DESTINATION_FILE", [System.IO.File]::ReadAllText($_.FullName))}

# Placed on seperate lines for readability

这人会放置一个新的生产线在每个文件条目结束时,如果你需要它:

This one will place a new line at the end of each file entry if you need it:

get-childItem "YOUR_DIRECTORY\*.txt" | foreach
{[System.IO.File]::AppendAllText("YOUR_DESTINATION_FILE", 
[System.IO.File]::ReadAllText($_.FullName) + [System.Environment]::NewLine)}

跳过第一行:

$getFirstLine = $true

get-childItem "YOUR_DIRECTORY\*.txt" | foreach {
    $filePath = $_

    $lines =  $lines = Get-Content $filePath  
    $linesToWrite = switch($getFirstLine) {
           $true  {$lines}
           $false {$lines | Select -Skip 1}

    }

    $getFirstLine = $false
    Add-Content "YOUR_DESTINATION_FILE" $linesToWrite
    }

这篇关于合并多个CSV文件合并成一个使用PowerShell的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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