Page 1 of 1
Move file to a variant folder name

Posted:
Tue Nov 27, 2018 10:07 am
by RMeira
Hi,
I need your help to something I can't figure out how to implement it. I want to move a file to a folder. However, folder name is not going to be always the same. In fact, folder name is a text in the file name - e.g. 2018 - Customer - Document.pdf.
I want to move the file to the appropriate customer folder. I know It will demand a shell script. Can you help me?
Thanks!!!
Re: Move file to a variant folder name

Posted:
Tue Nov 27, 2018 10:58 am
by luomat
Whether or not this can be accomplished will depend on a few things and how consistent they are.
Looking at your example: "2018 - Customer - Document.pdf" will the format always be:
1. The Year
2. A Dash
3. The Customer Name
4. A Dash
5. Some other word or words (I assume it will not be "Document")
6. The file extension
Can #3 have spaces in it, or will it always be one word?
Can #3 also have dashes in it?
These would be easy to match:
"2018 - JohnSmith - Files.pdf"
"2018 - EdBarry - Taxes.pdf"
"2018 - ChristinaW - Contract.pdf"
these would be less easy to match:
"2018 JohnSmith.pdf"
"2018 - Ed Barry Taxes.pdf"
"2018 - Christina - W - Contract.pdf"
Re: Move file to a variant folder name

Posted:
Tue Nov 27, 2018 11:06 am
by RMeira
Filename will be always consistent with this standard. I set up this way to make things easier.
Here, all answers you requested:
Yes, #3 could have spaces.
No, #3 will never have dashes.
Re: Move file to a variant folder name

Posted:
Tue Nov 27, 2018 11:20 am
by luomat
Ok, here is what I came up with:
- Code: Select all
#!/bin/zsh -f
#
# From: Timothy J. Luoma
# Mail: luomat at gmail dot com
# Date: 2018-11-27
#
# Purpose: Script meant to be used with Hazel to move a file into a sub-folder based on the filename
# e.g. Given a filename "2018 - Christina W - Contract.txt" it will move the file to a folder named "Christina W"
# See <https://www.noodlesoft.com/forums/viewtopic.php?f=4&t=10077&p=26650> for more details
#
# Use entirely at your own risk.
PATH='/usr/local/scripts:/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin'
# We start by creating a loop that will run this script once for each input (filename)
for i in "$@"
do
# We check to make sure that the input we received
# corresponds to an actual, existing file
if [ -f "$i" ]
then
# ':r' removes the extension from the variable "$i"
#
# Then there are two sed commands in one
# (the ';' is what separates the sed commands)
#
# 's#[0-9][0-9][0-9][0-9] - ##g' means
# "Look for 4 digits in a row,
# followed by one space
# followed by a "-"
# followed by one space
# and replace it with nothing (i.e. remove/delete it)"
#
# 's# - .*##g' means
# "look for one space,
# followed by a dash
# followed by one space
# followed by anything (that is what is meant by '.*')
# and replace it with nothing (i.e. remove/delete it)"
#
MIDDLE=$(echo "$i:r" |\
sed 's#[0-9][0-9][0-9][0-9] - ##g ; s# - .*##g')
# this will create the directory, if needed
mkdir -p "$MIDDLE"
# this will move the file to the directory
# the "-n' will prevent it from overwriting a file
# by the same name, if one already exists
mv -n "$i" "$MIDDLE"
fi # This closes the 'if' portion of the loop
done # this ends the loop
exit 0
#EOF
Enter that into Hazel as "Run Shell Script" and then use the "Embedded Script" feature.
Click the "Edit script" button and paste the script into the window that pops up.
There may be a more "pure Hazel" way to solve this, but I tend to see everything through the eyes of "Can I write a shell script for that?"
Tested it here and it seemed to work, but standard disclaimers apply: use at your own risk.
Feel free to ask questions if you have any.
Re: Move file to a variant folder name

Posted:
Tue Nov 27, 2018 12:57 pm
by Mr_Noodle
I think you can do this without a shellscript. Using match patterns (search for them in the help), you can pick apart the name, using a custom attribute to match the customer name, and then use that custom attribute in a Sort into Subfolder pattern.
Re: Move file to a variant folder name

Posted:
Tue Nov 27, 2018 6:29 pm
by RMeira
Mr.Noodle, I tried your suggestion. Point here, source folder is "X". I want to move the file to "A/b/c" where "c" is the name of the customer in the filename. I used sort into with pattern Customer\My Pattern here.
Hazel moved the file do a new folder Customer\My Pattern here in the source folder. It was not in the path "a/b/c".
As far as I understood, sort into only create or work with subfolder direct on the source folder. Is it right?
Thanks.
Re: Move file to a variant folder name

Posted:
Tue Nov 27, 2018 6:30 pm
by RMeira
luomat wrote:Whether or not this can be accomplished will depend on a few things and how consistent they are.
Looking at your example: "2018 - Customer - Document.pdf" will the format always be:
1. The Year
2. A Dash
3. The Customer Name
4. A Dash
5. Some other word or words (I assume it will not be "Document")
6. The file extension
Can #3 have spaces in it, or will it always be one word?
Can #3 also have dashes in it?
These would be easy to match:
"2018 - JohnSmith - Files.pdf"
"2018 - EdBarry - Taxes.pdf"
"2018 - ChristinaW - Contract.pdf"
these would be less easy to match:
"2018 JohnSmith.pdf"
"2018 - Ed Barry Taxes.pdf"
"2018 - Christina - W - Contract.pdf"
I'll test today. Thank you very much!!!
Re: Move file to a variant folder name

Posted:
Tue Nov 27, 2018 8:53 pm
by RMeira
Mr_Noodle wrote:I think you can do this without a shellscript. Using match patterns (search for them in the help), you can pick apart the name, using a custom attribute to match the customer name, and then use that custom attribute in a Sort into Subfolder pattern.
It worked!!! I forgot about > item. Thanks!
Re: Move file to a variant folder name

Posted:
Wed Nov 28, 2018 1:03 pm
by Mr_Noodle
What are a/b in this case? Is that a fixed folder? If so, you can use the Move action to move the file there then do the sort. Otherwise, I'll need more info on what the intermediate folders are.