Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
I was wondering if anyone knew how to take a comma
delimited table and add the file name of the table as a
new column for each of the rows. Here is an example of
what I would like to do:I have a bunch of files put together like this,
year, team, title
2001, cleveland indians, general manager
2002,cleveland indians, general manager
2003,cleveland indians, general manager
2004,cleveland indians, general manager
2005,cleveland indians, general manager
2006,cleveland indians, general manager
2007,cleveland indians, general manager
2008,cleveland indians, general managerThe file name for this would be shapima01 which is mark
shapiro's id
name in this database. So in order to make the files more
useful I
would like to add this ID that is the filename as a column.
The ideal
output would beyear, team, title,id
2001, cleveland indians, general manager, shapima01
2002,cleveland indians, general manager,shapima01
2003,cleveland indians, general manager,shapima01
2004,cleveland indians, general manager,shapima01
2005,cleveland indians, general manager,shapima01
2006,cleveland indians, general manager,shapima01
2007,cleveland indians, general manager,shapima01
2008,cleveland indians, general manager,shapima01I have been doing some work with batch files to rename
the files,
delete extra files, etc. I was thinking maybe this method
could also
be applied here. Although I am not sure. Anyway thanks
for looking
at it.

@echo off > newfile
setLocal EnableDelayedExpansionif %1'==' echo which file ? && goto :eof
for /f "tokens=* delims= " %%a in (%1) do (
set /a N+=1
if !N! equ 1 (
echo %%a, id >> newfile
) else (
echo %%a, %1 >> newfile
)
)
=====================================
If at first you don't succeed, you're about average.M2

Thank you very much for the response. I to some degree
how this script works, but i am having trouble with the
overall execution. Would it be possible to explain your
script. Thank you very much!

The checking for a parameter is, I hope, obvious.
In the FOR loop this:set /a N+=1
'keeps track of what line it's on'
For line 1 it puts 'id' at the end of the line in newfile. For all subsewuent lines it puts the filename at the end of the line.
=====================================
If at first you don't succeed, you're about average.M2

Thanks that part is clear, but what about the filenames, do I
just insert the file I want as new file. Do I need to do
anything with which file ? and also how would I have this
repeated for all the files in the directory. Thanks so much
for all of your help and bearing with my lack of knowledge in
this area I really appreciate it.

If your files are .CSV, try this:
::==============================
@echo off
setLocal EnableDelayedExpansionfor /f "tokens=* delims= " %%c in ('dir/b/a-d *.csv') do (
set FN=%%~Nc
set /a N=0for /f "tokens=* delims= " %%a in (%%c) do (
set /a N+=1
if !N! equ 1 (
echo %%a, id > !FN!.new
) else (
echo %%a, !FN! >> !FN!.new
)
)
)
=====================================
If at first you don't succeed, you're about average.M2

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |