Computing.Net > Forums > Programming > Program to add filename as column

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Program to add filename as column

Reply to Message Icon

Name: nxnn14
Date: July 18, 2008 at 06:21:02 Pacific
OS: Mac OS/Windows XP/Ubuntu
CPU/Ram: intel core 2 duo with 2 G
Product: Apple Macbook with parall
Comment:

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 manager

The 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 be

year, 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,shapima01

I 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.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: July 19, 2008 at 10:08:47 Pacific
Reply:

@echo off > newfile
setLocal EnableDelayedExpansion

if %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


0

Response Number 2
Name: nxnn14
Date: July 19, 2008 at 11:07:06 Pacific
Reply:

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!


0

Response Number 3
Name: Mechanix2Go
Date: July 19, 2008 at 12:54:55 Pacific
Reply:

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


0

Response Number 4
Name: nxnn14
Date: July 19, 2008 at 13:06:25 Pacific
Reply:

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.


0

Response Number 5
Name: Mechanix2Go
Date: July 21, 2008 at 01:33:57 Pacific
Reply:

If your files are .CSV, try this:

::==============================
@echo off
setLocal EnableDelayedExpansion

for /f "tokens=* delims= " %%c in ('dir/b/a-d *.csv') do (
set FN=%%~Nc
set /a N=0

for /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


0

Related Posts

See More



Response Number 6
Name: nxnn14
Date: July 21, 2008 at 08:08:26 Pacific
Reply:

Perfect! Thank you so much you have been a great help!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Program to add filename as column

Batch needed to add column to CSV www.computing.net/answers/programming/batch-needed-to-add-column-to-csv/15724.html

Batch to add filename to txt file www.computing.net/answers/programming/batch-to-add-filename-to-txt-file/14863.html

write a program to www.computing.net/answers/programming/write-a-program-to/1696.html