0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CSVファイルをPowerShellで分割

Last updated at Posted at 2024-12-26

CSVファイルをPowerShellで分割する処理をChatGPTで出力しました

$InputFile = "C:\path\to\large.csv"  # CSVファイルのパス
$OutputDir = "C:\path\to\output"    # 分割後のファイル出力先
$RowLimit = 1000000                 # 1ファイルあたりの行数

# 出力ディレクトリを作成(存在しない場合)
if (!(Test-Path $OutputDir)) {
    New-Item -ItemType Directory -Path $OutputDir
}

# ファイルを読み込む
$CsvContent = Import-Csv -Path $InputFile
$TotalRows = $CsvContent.Count
$FileIndex = 1

# 分割処理
for ($i = 0; $i -lt $TotalRows; $i += $RowLimit) {
    $OutputFile = Join-Path $OutputDir ("split_file_" + $FileIndex + ".csv")
    $Chunk = $CsvContent[$i..([math]::Min($i + $RowLimit - 1, $TotalRows - 1))]
    $Chunk | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
    $FileIndex++
}

Write-Host "CSVファイルが分割されました: $OutputDir"```
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?