Add Filename To Last Column of CSV

October 11, 2011 at 06:03:12
Specs: Windows Vista
Hello,
i have a csv named TAXYDR1029551952390.csv and I want to add the numer of the filename in the last column of the file in every row.
I have a lot of files like this, so i wan to create a batch to do this to all files at once. All the files have the same format (CSV) and the same filename format.
Can someone help?
Thank you!

See More: Add Filename To Last Column of CSV

Report •


#1
October 11, 2011 at 18:30:20
I want to add the numer of the filename in the last column of the file in every row.

Does this mean you want to add a new column, as the last column in every row? If so do you want a header for the new column added to the header line?

Or do you want to add the number in the filename to the existing last column?


Please come back & tell us if your problem is resolved.


Report •

#2
October 12, 2011 at 00:04:04
Yes i want the added number to be the new last column. I dont mind if it has header or not because its header files will be eliminated.

Report •

#3
October 12, 2011 at 01:18:15
For testing purposes csv files were created in %temp%\input\ and output files were written to %temp%\output\ You will need to adjust paths as necessary. The script is not fully tested and does not remove header lines although it does create a new column header consisting of the numbers in the original filename. No allowance is made for any filename containing spaces.

@echo off
cls
setlocal enabledelayedexpansion

if not exist %temp%\output\ md %temp%\output\

pushd %temp%\input\

for /f "tokens=*" %%1 in ('dir /b *.csv') do (
    set filename=%%~n1&set filenbr=!filename:~6!

    for /f "tokens=*" %%A in (!filename!.csv) do (
        echo %%A,!filenbr!>>%temp%\output\!filename!.csv
  )
)



Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
October 12, 2011 at 04:38:25
I would like to thank you for your time.
Unfortunately i dont get any results after running the batch.
Thank you

Report •


Ask Question