Sort file into subfolder (Create folders based on pattern)

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

Moderator: Mr_Noodle

At the moment, I've created a rule for every file i've encountered. For example:

name1.info.txt
name2.info.txt

The rule would then look for the specific keyword being "name1" then create a folder called "name1" then move "name1.info.txt" into the newly created folder. Using a brute force method this can be easily achieved for every file I want to have organized in this manner but this is very tedious.

Is there a way for us to create a temporary variable that would store the pattern (if found) to be used under "Sort file into subfolder" to create a directory based off the variable (if found)?

Also if such a solution would be possible, I would also like to run something similar to "sed" against the variable where we can replace characters such as a period with an empty space before using the variable to create the directory.

If this is not possible I guess I could write a bash script to perform all these actions and have hazel execute it. But it would be nice to have this kind of flexibility built into hazel instead.

Thanks!


{EDIT}
Never mind.... I just figured out how to run an embedded shell script. What an amazing app!

For anyone that is interesting here is the code:
Code: Select all
# Remove path from name.
file=`basename "$1"`

# Strip ".S00E00*.mkv"
name=`echo $file | sed 's/\.[sS][0-9]*[eE][0-9]*.*//g'`

# Strip ".00x00*.mkv"
if [ "$file" = "$name" ]; then
  name=`echo $file | sed 's/\.[0-9]*[xX][0-9]*.*//g'`
fi

# Unknown Pattern
if [ "$file" = "$name" ]; then
  exit
fi

# Replace periods, underscores with white space.
name=`echo $name | sed 's/[\._]/ /g'`

# Capitilize first character for every word.
folder=`echo $name | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1'`

# Create & Move file to folder.
if [ ! -d "$folder" ]; then
  mkdir "$folder"
fi
mv "$file" "$folder"
sumolx
 
Posts: 5
Joined: Sun Jan 30, 2011 1:20 pm

While I'm glad you were able to use a script, a lot of what you want to do is built in. Search the help for "match patterns". I think you would still need a script to do the character replacement (I will be adding a feature for this in 3.0) but most of the other stuff can be handled without the need of a script.
Mr_Noodle
Site Admin
 
Posts: 11255
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

One reason I choose to do most of it in script was because I did not see a way to 'or' two patterns. Seems the standard symbol for OR'ing `|` is not used or supported by match pattern. Otherwise I would have to write multiple rules with each having their own unique patterns. Instead I decided to use a single script to accomplish this task.

Example:
Code: Select all
Mythbusters.S06E16.Alcohol.Myths.720p.HDTV.x264-DHD.avi
Mythbusters.6x16.Alcohol.Myths.720p.avi


From:
Code: Select all
# Strip ".S00E00*.mkv"
name=`echo $file | sed 's/\.[sS][0-9]*[eE][0-9]*.*//g'`

# Strip ".00x00*.mkv"
if [ "$file" = "$name" ]; then
  name=`echo $file | sed 's/\.[0-9]*[xX][0-9]*.*//g'`
fi


To:
Code: Select all
(• show).S(• season)E(• episode).(• episode name).(…)
                   OR
(• show).(• season)X(• episode).(• episode name).(…)


Variables:
Code: Select all
show: (...)
season: (number)
episode: (number)
episode name: (...)


Probably what would work best is to also to have the following:
Code: Select all
if (all) of the following conditions are met


as a selection that would encapsulate the (Name) (matches) [****patterns****] and have it looks something like this....
Code: Select all
(Extension) (is) [mkv]
(if) (any)
(Name) (matches) [****pattern a****]
(Name) (matches) [****pattern b****]
(fi)
sumolx
 
Posts: 5
Joined: Sun Jan 30, 2011 1:20 pm


Return to Support