Page 1 of 1
Only copy files that match a .csv

Posted:
Wed Nov 28, 2012 7:12 pm
by theherrera
Hi,
I currently have a huge folder of images. I was wondering if Hazel could cross check the name of an image to a list I currently have in a spreadsheet. If the image name matches an entry in the spreadsheet, then copy the file to a new folder.
Basically I have images named:
OCT_11111.jpg
OCT_22222.jpg
OCT_33333.jpg
OCT_44444.jpg
OCT_55555.jpg
I have a column in Excel that has a list:
OCT_22222
OCT_44444
Is there a way to have Hazel read the .csv and only copy over the matching images and skip the images that are not in the list? Any help would be greatly appreciated.
Re: Only copy files that match a .csv

Posted:
Thu Nov 29, 2012 2:09 pm
by a_freyer
adding a 'passes shell script' condition with the following should get you working:
- Code: Select all
exit $([ $(grep -ci $(basename "$1" jpg) path/to/your/csv) -gt 0 ])
This assumes that all of your images have a jpg extension.
Re: Only copy files that match a .csv

Posted:
Thu Nov 29, 2012 2:29 pm
by theherrera
That works perfectly!

This is going to save so much time. I spent all afternoon yesterday trying to figure this out but I'm not really that much of a programmer. Thank you so much!
Re: Only copy files that match a .csv

Posted:
Thu Nov 29, 2012 2:32 pm
by a_freyer
No problem!
Glad it worked the first time!
Re: Only copy files that match a .csv

Posted:
Thu Nov 12, 2015 4:43 am
by zio4321
Hello,
I have similar case, except it is list of videos of mkv, mp4 etc..
I did the same thing with something like this:
exit $([ $(grep -ci $(basename "$1") /the/file/path.csv) -gt 0 ])
and it worked well except for files with spaces!!
Can someone help me to tweak the command please. i am not good with coding.
thanks
Re: Only copy files that match a .csv

Posted:
Thu Nov 12, 2015 12:12 pm
by Mr_Noodle
Try "basename $1".
Re: Only copy files that match a .csv

Posted:
Sat Nov 14, 2015 6:25 pm
by zio4321
Mr_Noodle wrote:Try "basename $1".
Many thanks Mr Noodle for your great work and support honestly.
Actually, I figured to match the whole path "$1" instead of just the basename before I read your reply. and it seems working fine with me (well, I am assuming the time of the matching process is not much different between a whole path or just the basename I guess!!)
Still got an issue though if you can help, it doesn't work with filename contains square brackets!! "[" or "]"
Re: Only copy files that match a .csv

Posted:
Sun Nov 15, 2015 11:02 am
by zio4321
Mr_Noodle wrote:Try "basename $1".
Update: I tried it, the whole command failed.
Re: Only copy files that match a .csv

Posted:
Mon Nov 16, 2015 12:46 pm
by Mr_Noodle
Search around for shell scripting sites out there. Don't know off the top of my head but there should be some way to escape the path to prevent any odd characters to cause interference.
Re: Only copy files that match a .csv

Posted:
Tue Nov 17, 2015 9:52 am
by zio4321
Mr_Noodle wrote:Search around for shell scripting sites out there. Don't know off the top of my head but there should be some way to escape the path to prevent any odd characters to cause interference.
Thanks,
it is weird because i did a long search and many sites say that this could fix it $(basename "$File"), with quotation marks on $File. and I used it on Terminal too and it worked like a charm!!
But when I do it in Hazel with this $(basename "$1"), for some reason it doesn't work with files with spaces!! So I guess I have hit a dead end.
Re: Only copy files that match a .csv

Posted:
Tue Nov 17, 2015 11:15 am
by Mr_Noodle
Maybe try assigning to a variable and then quote that variable wherever you use it.
Re: Only copy files that match a .csv

Posted:
Thu Nov 19, 2015 8:23 pm
by zio4321
Mr_Noodle wrote:Maybe try assigning to a variable and then quote that variable wherever you use it.
didn't work!!
Re: Only copy files that match a .csv

Posted:
Fri Nov 20, 2015 10:56 am
by Mr_Noodle
Break that up into separate statements instead of nesting it all in one as it makes it hard to debug. Then try "echo"-ing each part separately, assigning to variables.. If you turn on debug mode (see sticky topic), you'll see the output of the script in the logs. That way you can find out at which point things go wrong.