0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

複数のCSVファイルをマージ&ソートするバッチファイル

Last updated at Posted at 2021-04-20

<スクリプトの流れ>

  1. 全てのCSVファイルの2行目以降(more +1)の空白行以外(findstr ".")をマージし、
    一時ファイル(temp.txt)に出力
  2. CSVファイルの一つからヘッダ(タイトル)行のみ取り出して all.csv に出力
  • タイトル行が1024バイトを超えている場合に備えて、変数サイズに1024バイト制限のあるSETコマンドは使わず、echoコマンドからファイルに直接出力する
  • ファイル名に空白スペースが含まれている場合に備えて、[usebackq]オプションでファイルセットのファイル名(%%a)を二重引用符で囲めるようにする
  • タイトル行に空白スペースが含まれている場合に備えて、[delims]オプションで空白スペースを区切り文字にしない
  1. 一時ファイル(temp.txt)の中身を降順にソート(sort /r)して all.csv に追記
  2. 一時ファイル(temp.txt)を削除する

※ CSVファイルと同じフォルダに入れて実行する

CSV_Merge_Sort.bat
@echo off

set temp_file=temp.txt
set merge_file=all.csv

REM 1.
for %%a in (*.csv) do (
    type "%%a" | more +1 | findstr "." >> %temp_file%
)

REM 2.
for %%a in (*.csv) do (
    for /f "usebackq delims=" %%b in ("%%a") do (
        echo %%b> %merge_file%
        goto :label
    )
)
:label

REM 3.
sort /r < %temp_file% >> %merge_file%

REM 4.
del /q %temp_file%
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?