Reading CSV

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
I'm not sure what I should be using for this... PHP? Anyway, I've got a CSV that will have these columns:

id, full path(incl filename)

for example:

1,/web/abc/def/document1.doc
2,/abc/def/readme.pdf

What I want to do is this:

a. create a directory based on the id # (so that the directories should be named "1", "2", etc.)

b. move or copy that file into the newly created directory, so that it the above structure becomes 1/document1.doc , 2/readme.pdf
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
Just about any language that you know should be fine. I like perl and since you've only got two columns I'd probably just read the file, split each line on , and do a mkdir if the id directory doesn't exist and move the file into it.
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
Would this script be simple enough to build in Perl or Python, if I've got practically zero experience in both languages?
 

postmortemIA

Diamond Member
Jul 11, 2006
7,721
40
91
yes, that is point of both languages to learn them quickly, on fly: "what you don't know won't hurt you (much)"
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
OK, I started looking at Perl. Should I use this? http://search.cpan.org/~hmbran...-CSV_XS-0.57/CSV_XS.pm

Or, if anyone is kind enough to whip up a script, it would be most welcome :) Am sure it wouldn't take more than a few minutes for you expert coders out there. I don't need to do any kinds of checks to see if the directory exists. All I need to do is create the directory, and copy or move that file into said directory.
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
OK I just need one final piece of help. I was able to get the files to copy correctly, however when I create the directories, I see an extra "?" character at the end if I do ls on the directory. This happens for all directories except the last one.

For example, the directories look like this

1?
2?
3?
4

The code I used is:
mkdir ("field[2]");

Any ideas what's wrong? I couldn't figure out how to attach code here properly, so here's a screencap: http://img49.imageshack.us/img49/4485/picture1hr5.jpg

I've saved my csv as a Windows-formatted CSV using Excel for Mac:
/private/29_R1_Agenda.pdf,29_R1_Agenda.pdf,1
/private/29_R2_Simplification_Post_Adjustment.pdf,29_R2_Simplification_Post_Adjustment.pdf,2
/private/29_R3_Hedonic_Regression.pdf,29_R3_Hedonic_Regression.pdf,3
/private/29_R4_Estimation_OUT_OF_AREA.pdf,29_R4_Estimation_OUT_OF_AREA.pdf,4
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: Kai920
OK I just need one final piece of help. I was able to get the files to copy correctly, however when I create the directories, I see an extra "?" character at the end if I do ls on the directory. This happens for all directories except the last one.

For example, the directories look like this

1?
2?
3?
4

The code I used is:
mkdir ("field[2]");

Any ideas what's wrong? I couldn't figure out how to attach code here properly, so here's a screencap: http://img49.imageshack.us/img49/4485/picture1hr5.jpg

I've saved my csv as a Windows-formatted CSV using Excel for Mac:
/private/29_R1_Agenda.pdf,29_R1_Agenda.pdf,1
/private/29_R2_Simplification_Post_Adjustment.pdf,29_R2_Simplification_Post_Adjustment.pdf,2
/private/29_R3_Hedonic_Regression.pdf,29_R3_Hedonic_Regression.pdf,3
/private/29_R4_Estimation_OUT_OF_AREA.pdf,29_R4_Estimation_OUT_OF_AREA.pdf,4

Your parse_csv function is way more complex than it needs to be. Look into the split function.
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
Hmm , still not working 100%.. Here's my code: http://img207.imageshack.us/img207/5866/picture2ck0.jpg

1) I'm getting some of these errors when I execute the script:
Unsuccessful stat on filename containing newline

I found on Google I need to use "chomp"?? How/where should I insert this?

2) For files that actually copied over to the new directory, I'm getting an extra "newline" character. I guess that's how I got those extra ? in my directory names earlier. How can I avoid this? Is it a problem with my exported csv? See the extra character after pdf: http://img147.imageshack.us/img147/4535/picture3mg1.jpg
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I found on Google I need to use "chomp"?? How/where should I insert this?

chomp the filename, I would chomp both $filetobecopied and $newfile just to be safe.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: Nothinman
I found on Google I need to use "chomp"?? How/where should I insert this?

chomp the filename, I would chomp both $filetobecopied and $newfile just to be safe.

Just chomp $line right after you read from the stream.

while ( $line = <F> ) {
chomp($line);
...
 

suklee

Diamond Member
Oct 9, 1999
4,575
10
81
I tried both of your suggestions, but am still getting an extra newline character at the end.

I've reduced my CSV to 3 lines for testing. The first two files copied have an extra newline character in the destination filename; only the third one works.

edit: I added an extra column at the end of my csv (extra comma) so there's 4 columns. and now everything works properly...