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"
jaw0807Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
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.
1
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.
0
arnoldCommented:
The text example is missing.
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

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
henry.csv
0
arnoldCommented:
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..

Perl
#!/usr/bin/perl
<STDIN>;# to skip the header line
while (<STDIN>){
chomp();
@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...
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jaw0807Author Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.