A script to make a folder and then move certain files into the folder based on information in CSV file.

I would like a script to go through a directory of images and make folders and move images into that folder based on lines in a csv file.

For example:

The csv file would have column names like FolderName, Image1, Image 2, Image3, Image4

FolderName = 18-001
Image1 = 18-001.jpg
Image2 - 18-002.jpg
Image 3 = 18-003.jpg
Image 4 = personsname.jpg

So based on that wtihin the folder where the .csv file would be located and all of these images would be located I want to make a folder named "18-001" and then move Image1-4 into that folder.

With one catch.  I might sometimes have more than 4 images.  Each csv file might have different images.  So a "while" statement would be needed to read the .csv Image columns - they do all start with "Image"
Who is Participating?
First, this is not a csv, as there are no commas separating values.
The name (lastname, firstname) would pose an issue
Hopefully it is a tab separated..

<STDIN>;# to skip the header line
while (<STDIN>){
@array=split(/\t/); #split the line based on a tab character
# $array[0] is the folder name
# Elements 4-7+are images, testing that they are defined, length >0...
# $#array will return the number of elements in the array, if/when dealing with images, you could loop from 4 to $#array
#the main concern deals with whether the column separators, (tab, or comma, have to not appear in any value, or that will alter fields) accounting for the possibility if exists......
#here you could add the logic to test whether the folder exists -d "$array[0]",...


Open in new window

If the data is in a database, MySQL, mango, PostgreSQL, etc.
Perl could connect and pull the data without worrying about the separators...
perl is likely the best tool for this

Example of the csv data in the exact format as it exists.
Does each line have the same number of elements?
The short logic, go line by line from the csv
Split on comma
The item that is the folder, execute mkdir -p foldername to create it if missing, or you can rest if it exists, .....
Then do what is needed with the files.

It could be done with awk, though my familiarity with it is not sufficient to deal with defining the block, but the logic is the same.
jaw0807Author Commented:
Here is an example of the file.  The first column FolderName would be the name of the folder.  Then Image 1 -?

No, each line does not have the same number of elements.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

The text example is missing.
jaw0807Author Commented:
Should be attached now.  But here is an example of two lines.

FolderName      FullName      County      OriginalURL      Image1      Image2      Image3      Image4      Image5      Image6      Image7
089-018-001      Gravely, Robert Marion.      Henry County (Va.)      http://image.lva.virginia.gov/WWI/pages/018/0077.html      018-00134.jpg      018-00135_left.jpg      018-00135_right.jpg      018-00136.jpg      
089-018-002      Manning, Luther Leonard.      Henry County (Va.)      http://image.lva.virginia.gov/WWI/pages/018/0145.html      018-00137.jpg      018-00138_left.jpg      018-00138_right.jpg      018-00139.jpg
jaw0807Author Commented:
Thank you.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.