Sort into subfolder using pattern matching wildcards

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

Hi!

I'm confused about how "Sort into subfolder" works. I'd love to match the subfolder name in the same way I match a file name, but I can't figure out how it's done. Here's my setup so far...

Code: Select all
If all of the following conditions are met
   Name matches (•Project Number)(...)Project_Report(...)
Do the following to the matched file or folder
   Sort into subfolder with pattern (•Project Number) NEED TO IGNORE TEXT HERE
   Sort into subfolder with pattern Reports

My goal is to match an EXISTING subfolder by the (•Project Number) token. However, I can't figure out how to ignore the text after the (•Project Number) in the desired subfolder.

For example, I want to automatically move these two files

XK23738-21 Project Report.pdf
BR84923-66 Project Report.pdf

to our server, which already has the folder structure:

RG83205-38 Haverson Habidashery
|-Timesheets
|->Reports
|->Expenses
XK23738-21 Turlock Renovation
|-Timesheets
|->Reports
|->Expenses
WT893127-09 Witter Yules
|-Timesheets
|->Reports
|->Expenses
BR84923-66 Medical Warehouse
|-Timesheets
|->Reports
|->Expenses

So when Hazel is done, it should look like this:

RG83205-38 Haverson Habidashery
|-Timesheets
|->Reports
|->Expenses
XK23738-21 Turlock Renovation
|-Timesheets
|->Reports
|----------->XK23738-21 Project Report.pdf
|->Expenses
WT893127-09 Witter Yules
|-Timesheets
|->Reports
|->Expenses
BR84923-66 Medical Warehouse
|-Timesheets
|->Reports
|----------->BR84923-66 Project Report.pdf
|->Expenses

How do I create a "Sort into subfolder" pattern that uses my (•Project Number) token to match the correct existing folder on the server?

I noticed that the pattern matching pallets are different when trying to create a token for a filename match versus a pattern for a "Sort into subfolder" match.

Image

Image
AppleSuperlatives
 
Posts: 7
Joined: Fri Dec 31, 2010 6:51 pm

You can't wildcard when sorting. You need to fully specify the folder right now otherwise Hazel would not know what to do if the folder doesn't exist (Hazel can't create a folder with a wildcard in its name).
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

I understand. It'd probably be a pain to program. I'll try to work on some crazy, hacky scripted solution. Feature request?

Thank you!
AppleSuperlatives
 
Posts: 7
Joined: Fri Dec 31, 2010 6:51 pm

Will consider it though I'd need to make sure people don't get confused with the option to create the folders as needed. Also, not clear what would happen if the folder doesn't exist since it can't just make up text to cover the wildcarded portion.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:Will consider it though I'd need to make sure people don't get confused with the option to create the folders as needed. Also, not clear what would happen if the folder doesn't exist since it can't just make up text to cover the wildcarded portion.


What the OP wants is EXACTLY what I'm trying to get working now.

I have files:

001_data.db
002_data.db

and I want to sort them into their respective folders:

001-Apple
002-Broccoli

based on the 3 digit ID at the beginning of the names. Nowhere is anything after the ID duplicated by the file or folder the file goes in.

Based on Hazel not being able to create folders with a wildcard, which is completely understandable, I would want it to do nothing if the folder does not already exist. In other words, if I have the files:

001_data.db
002_data.db

and the folders:

001-Apple
003-Carrot

Only the file 001_data.db would be sorted because there was nowhere for 002_data.db to go. This would alleviate the need for Hazel to figure out how to create a folder containing a wildcard. I'm very familiar now with matching and have used it to sort and upload files to a server with great effect, I just need it to go a step further.

The problem is that I have a large number of files with the unique 3 digit ID but I need to upload them to a server which contains folders whose names do not contain these IDs, such as:

Apple
Broccoli
Carrot

so I have to use intermediary folders with the 3 digit ID AND the server folder name for initial sorting, then rename the folders to match the server, sort into another subfolder, then upload the files.

Thanks for looking into this Mr. Noodle!

Steven
fatherfork
 
Posts: 1
Joined: Mon Oct 28, 2013 4:16 pm

Hi Steven,

I need to do exactly what you are planning to? Did you figure out a way to do what you wanted?

I had wanted to do this a few months ago. Then gave up. Was inspired to try it again today.

Quick summary of what I want to achieve:
I have a filename "2014-02-22 Nameoffile zz29.pdf".
And I want to move this to a subfolder named "29. ContextualFolderName"

Now, I have created a CustomToken in HAzel which extracts "29" by looking for zz as a pattern in the name and the number immediately following zz is the custom token.

The gap:
If I can pass the CustomToken to a shell script, and if I can pass the output back from the shell script, then the job can be done.
The shell script can be as simple as "find "*$CustomToken*" -maxdepth 0".
The result of this command is the name of the folder that begins with "29.".
If that folder name can be received into HAzel as a variable, then I can use the Sort into Subfolder capability of HAzel and accomplish what we want.

Here is a shell script that does the job if $2 can be the custom token.
Code: Select all
cd "FolderWithSubFolders"
find *$2.* -maxdepth 0 > FOLN
cat FOLN


Any ideas as to how to fill the gap - i.e. send the CustomToken, and receive the result of find command?

Thanks!
jim80
 
Posts: 23
Joined: Tue Aug 28, 2012 5:47 pm

Any insights, anybody?

Thanks!!
jim80
 
Posts: 23
Joined: Tue Aug 28, 2012 5:47 pm

One thing you can try instead is match the three digit code into a custom token. Then sort into subfolder with that custom token. Do "replace text" on that custom token, and map each three digit code with the full folder name. So, for instance "001" -> "001-Apple". Might be a bit tedious if you have a ton of these but it's another way of doing it and you'll avoid having to write a script.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Mr_Noodle wrote:One thing you can try instead is match the three digit code into a custom token. Then sort into subfolder with that custom token. Do "replace text" on that custom token, and map each three digit code with the full folder name. So, for instance "001" -> "001-Apple". Might be a bit tedious if you have a ton of these but it's another way of doing it and you'll avoid having to write a script.


That will be like creating 40 different rules. And the rule will break the moment I change a folder name.

So one follow-up question:
- Is there a way to pass the Custom-Token to a shell script?
- Can I get the output from a shell script back into Hazel?
(I am thinking I will meddle with shell script, use what I know - ls the directory, and then grep for the custom token, and return the output of grep back to hazel as a variable, and then use that variable...)

Thanks!
jim80
 
Posts: 23
Joined: Tue Aug 28, 2012 5:47 pm

Not sure where you get the idea that it will require 40 rules. Seems like one rule but with a lookup table for each case.

You can't pass anything to a script from Hazel yet and you can only export data from a script if you are using AppleScript.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

So, for what it is worth, I did manage to write a rather clumsy and long winded shell script to move files based on the name of the file.

The script below looks for a token in a file using custom token capability in Hazel, and promptly calls a shell script. The shell script receives the matched file as input and does its job.

I am sure that shell script experts can write this entire script in 5-6 lines. But hey I labored and produced this, so sharing it should it help somebody.

Code: Select all
# This script is written to identify Folder Number token from a file name.
# The Folder number token is embedded in a file name, preceded with a "zz" and ends with "."
# For example, a file name can be "2014-02 Travel plan zz20.pdf"

sourcedir="/Users/jim/Dropbox/Scans/Action"
cd "$sourcedir"

logf="/Users/jim/Dropbox/Scans/Action/log.txt"
HazelFileMoveLog="/Users/jim/Dropbox/Scans/Action/HazelFileMoveLog.txt"
errorfile="/Users/jim/Dropbox/Scans/Action/ErrorFileLog"

rm $logf
rm $errorfile

fn=$1
echo String fn is $fn > $logf

# String function to obtain the part of the string that follows after zz
fn2=${fn#*zz}
echo fn2 is $fn2 >> $logf

# String function to obtain the part of string that precedes .
fn3=${fn2%.*}

fn31=$fn3
echo fn31 is $fn31 >> $logf

# Since . is a special characeter, precede the . with a \
fn3="$fn3\."
echo fn3 is $fn3 >>$logf

# now specifying the directory that holds all the numbered folders
mydir="/Users/jim/Dropbox/jm-docs/files/"
echo mydir is $mydir >> $logf

# Now specifying the target directory using ls and grep
# One shortcoming in the program is that if ls fails to produce a result, the script will still go on
# whereas the script should stop and record lack of target directory in an error file
targetdir=`ls -d "$mydir"*/ | grep "$fn3"`
echo targetdir is $targetdir >> $logf

fn5=`echo mv "$fn" "$fn4"`
echo fn5 is $fn5 >> $logf

fn6pre=${fn%\ zz*}
echo fn6pre is $fn6pre >>$logf

fn6post=${fn2#*$fn31}
echo fn6post is $fn6post >>$logf

# With the help of fn6pre and fn6post, rebuilding the file name without the zzxx
fn6=$fn6pre$fn6post
echo fn6 is $fn6 >> $logf

# rename the file to remove zzxx
echo "Now first mv log" >> $logf
mv "$fn" "$fn6" 1>> $logf  2>>$errorfile

# Now moving the renamed file to the target directory
echo "Now second mv log" >> $logf
mv "$fn6" "$targetdir" 1>> $logf  2>>$errorfile

# Logging the move
echo time is `date` >> "$HazelFileMoveLog"
echo "Moved file \"`basename "$fn"`\"  to  \"`basename "$fn6"`\" to "$fn4"" >> "$HazelFileMoveLog"
jim80
 
Posts: 23
Joined: Tue Aug 28, 2012 5:47 pm


Return to Support