Would like help on shell scripting and renaming files.

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

Moderator: Mr_Noodle

My issue is I have too many similar rules in Hazel using the in-built file matching and the rename with pattern > replace text is cool but I wish I could use regex patterns in there versus explicitly stating every character that I want to be substituted/removed.

I'd like to learn how to create a basic shell script to check for a match of two specific file names, example here "eXPRS Plan of Care - Services Delivered Form" and "eXPRS Plan of Care - Mileage Driven Form" if no match happens the script exits and does not do anything. If a match happens act on the file(s) that match and strip desired characters with regex pattern using command-line tools. In this case, I just want to remove anything other than [a-zA-Z] (including spaces and hyphen) and lowercase the basename and extension. I liked the tr -cd [:alpha:] command I saw elsewhere on the internet but couldn't figure out how to use it to rename files with Hazel.

So, based on the little looking around I've done, I need to wrap my mind around $1 , basename, possibly handle file extension, and how to pipe/redirect commands to act on the file names and to actually rename them.

I'm able to use basename, but I'm struggling to do my own name matching in the script.

My test code so far:

Code: Select all
#!/bin/bash
FILENAME=$(basename "$1")
echo The Filename is: "$FILENAME" >> /Users/adamlogan/Desktop/basenameexerciseoutput.txt
if [ "$FILENAME"='eXPRS Plan of Care - Services Delivered Form.pdf']
then
      echo ""$FILENAME" matches the rule"  >> /Users/adamlogan/Desktop/basenameexerciseoutput.txt
else
      echo ""$FILENAME" does not match the rule" >> /Users/adamlogan/Desktop/basenameexerciseoutput.txt
fi


And my output:

Code: Select all
The Filename is: eXPRS Plan of Care - Services Delivered Form.pdf
eXPRS Plan of Care - Services Delivered Form.pdf does not match the rule
adamlogan
 
Posts: 5
Joined: Sun Jul 09, 2017 7:15 am

This is more of a general bourne/bash shell scripting issue but I believe you need to use "==" instead of "=".
Mr_Noodle
Site Admin
 
Posts: 11193
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

If python is an option... I use this simple code to strip all kind of strange characters of filenames:

Code: Select all
import sys
import ren
import os
oldname = sys.argv[1]
newname = re.sub(r'[^\w\s\-\_\.\/]+', '', oldname)
if oldname != newname:
    os.rename(oldname, newname)

Run shell script - embedded script - Shell : /usr/bin/python
geert
 
Posts: 1
Joined: Tue Oct 01, 2019 3:07 pm

Thanks. You were correct. Some other syntax errors were causing issues. Found a Bash syntax checker.

Script cleaned up is working as expected.

Code: Select all
#!/bin/bash
FILENAME=$(basename "$1")
OUTPUTFILE='/Users/adamlogan/Desktop/basenameexerciseoutput.txt'
echo The name of the input file is: "$FILENAME" >> "$OUTPUTFILE"
if [ "$FILENAME" == 'eXPRS Plan of Care - Mileage Driven Form.pdf' ] || [ "$FILENAME" == 'eXPRS Plan of Care - Services Delivered Form.pdf' ]
   then
      echo "$FILENAME" matches the rule  >> "$OUTPUTFILE"
   else
      echo "$FILENAME" does not match the rule >> "$OUTPUTFILE"
fi


This is a good start, and I can continue with bash type questions elsewhere.

One thing I noticed though was that in my output, the files were processed in an unsorted manner. I could see this being a problem someday as I do often work with sets of videos, audio files and photos etc and I'm well aware of how they can overwrite each other or get out of order if not sorted properly before batch renaming. Is there a hook or way to tell if Hazel is done processing files in a directory? I'm wondering how I'm supposed to sort the output of the files that hazel processes if they're done one at a time and not in the ideal order?

Here's the output I got when I touched text files from 1-4 and a-d.

Code: Select all
The name of the input file is: c.txt
c.txt does not match the rule
The name of the input file is: 4.txt
4.txt does not match the rule
The name of the input file is: b.txt
b.txt does not match the rule
The name of the input file is: a.txt
a.txt does not match the rule
The name of the input file is: 3.txt
3.txt does not match the rule
The name of the input file is: e.txt
e.txt does not match the rule
The name of the input file is: 2.txt
2.txt does not match the rule
The name of the input file is: 1.txt
1.txt does not match the rule
adamlogan
 
Posts: 5
Joined: Sun Jul 09, 2017 7:15 am


Return to Support