computing
  • 8

Batch To Add Filename As First Column

  • 8

Hello,

I am in desperate need of assistance. I have been browsing the forums here and have tried modifying several examples for nearly 4 hours, I’m not entirely sure of the syntax and what I am doing, thus I am not able to get the results I want.

I need to add the filename of a CSV file as the first column of the CSV, with a first row header name of “ID”. I would like to be able to run this batch on a collection of CSV files contained within a directory and have it insert the column with the respective filename in each file.

Here is a link to a unaltered CSV for reference:
http://www.mediafire.com/file/ph7c5…

I’m obviously out of my element, and I would sincerely appreciate any assistance that someone may be able to provide.

Share

1 Answer

  1. for %%a in (*.csv) do call :addId "%%~Na" "%%a"
    goto :EOF
    
    :addId
    @echo off
    for /f "usebackq delims=" %%b in (%2) do (
      > #.csv echo ID,%%b
      goto :next
    )
    :next
    for /f "usebackq skip=1 delims=" %%b in (%2) do (
      >> #.csv echo %~1,%%b
    )
    move #.csv %2

    How To Ask Questions The Smart Way

    • 0