使用 PowerShell 将多个 CSV 文件合并为一个
您好,我正在寻找可将目录中的所有 csv 文件合并为一个文本文件 (.txt) 的 powershell 脚本.所有 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 脚本?
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
}