且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在PowerShell中将一个大的文本文件分割成多个文件

更新时间:2022-12-20 11:36:31

这正是我在Bob McCoy帮助下寻找的东西。 b
$ b

Here is exactly what I was looking for based on help from Bob McCoy

#  Split-File.ps1

$src = "C:\Ephemeral\bigfile.csv"
$dstDir = "C:\Ephemeral\files\"

# Delete previous output files
Remove-Item -Path "$dstDir\\*"

# Read input and create subordinate files based on column 8 content
$header = Get-Content -Path $src | select -First 1

Get-Content -Path $src | select -Skip 1 | foreach {
    $file = "$(($_ -split ",")[7]).txt"
    Write-Verbose "Wrting to $file"
    if (-not (Test-Path -Path $dstDir\$file))
    {
        Out-File -FilePath $dstDir\$file -InputObject $header -Encoding ascii
    }
    Out-File -FilePath $dstDir\$file -InputObject $_ -Encoding ascii -Append
}



这个代码只是小问题。花了差不多80分钟把我的大文件分成1800个小文件,所以如果有人有一些建议如何提高这个代码的性能,将不胜感激。 Mayby这将有助于bigfile按第8列按字母顺序排序。所有小文件的名字也存储在#8列。

There is just small issue with this code. It took almost 80 minutes to split my big file into 1800 small files so if anybody has some suggestion how to increase performance of this code it would be highly appreciated. Mayby it would help that "bigfile" is sorted alphabetically by column#8. And names for all small files are also stored in column#8.