Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
22
Help us understand the problem. What is going on with this article?
@mihonak

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

More than 3 years have passed since last update.

複数の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

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

22
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
mihonak

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
22
Help us understand the problem. What is going on with this article?