I need to read a directory for all tab-delimited files (they will have a .csv extension) and insert a new column that contains the file name(without the path) and write the modified file back to the original file name. I've tried using ParseExcel/WriteExel , CTable, AnyData, and Text::xSV and usually get stuck on creating and merging the new column or rewrtiing a corrupted file (for excel). Here's one non-working version.....
#!/usr/bin/perl -w my $dir = 'C:/Programs' ; use strict; use Data::CTable; #Read Directory for all '.csv' file opendir(TD, $dir) || die "Can not open directory: $!:"; my @FILES=grep(/\.csv/i, readdir TD); print @FILES; closedir(TD); my $file = $FILES[0]; my $Full = $file; my $People = Data::CTable->new("roy.csv" ) || die "Can not open file: +$!:"; $People->clean_ws(); $People->col(PCSFileName => "hello"); $People->write(_FileName => "Roy_results.csv", _LineEnding => "dos"); foreach my $file (@FILES) { my $t = Data::Table::fromCSV($file) || die "Can not open file: $! +:"; $t->addCol($file, "PCS Filename") || die "Can not add Column "; print $t->csv || die "Can not write output"; }
Here's another.......
#!/usr/bin/perl -w my $dir = 'C:/Programs' ; use strict; use Data::Table; #Read Directory for all '.csv' file opendir(TD, $dir) || die "Can not open directory: $!:"; my @FILES=grep(/\.csv/i, readdir TD); print @FILES; closedir(TD); my $file = $FILES[0]; my $t = Data::Table::fromTSV($file,1) || die "Can not open file: $!:"; print $t->tsv foreach my $file (@FILES) { my $t = Data::Table::fromCSV($file) || die "Can not open file: $! +:"; $t->addCol($file, "PCS Filename") || die "Can not add Column "; print $t->csv || die "Can not write output"; }

In reply to insert filename into multiple tab delimited files by eagleroy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.