LoginSignup
27
24

More than 5 years have passed since last update.

複数のcsvファイルを結合するバッチ(1個目のファイル以外は1行目を削除)

Last updated at Posted at 2016-11-27

複数のcsvファイルを結合するバッチファイルを書いてみました。
それぞれの1行目にあるヘッダー行は要らないので削除します。
でも出力するファイルにはヘッダー行が欲しいので、ループの1ファイル目のヘッダーを挿入しています。

join.bat
@echo off
setlocal enabledelayedexpansion

set /a counter=0

for /f %%i in ('dir /b *.csv') do (

	echo %%i
	if !counter!==0 (
		set /p _head=<%%i
		echo !_head!>>result.csv
	)
	set /a counter=!counter!+1

	for /f "tokens=* skip=1" %%b in (%%i) do (
		echo %%b>>result.csv
	)
)
pause

わかりにくかったのは遅延環境変数というもの。

setlocal enabledelayedexpansion

と書いて、変数名を「!」で囲う必要がありました。

27
24
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
27
24