Created
July 25, 2018 14:31
-
-
Save dellow/d74c06930b0003008b92aa2e5f9a2b9e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if a directory has been supplied. | |
if [ -z "$1" ]; then | |
echo "You must supply a directory." | |
exit 0 | |
# Check directory exists. | |
elif [ ! -d "$1" ]; then | |
echo "That directory does not exist." | |
exit 0 | |
fi | |
# Check for options. | |
if [ -n "$2" ] && [ $2 = "-d" ]; then | |
DRY_RUN=true | |
# Output to console. | |
echo -e "---------------------------------------------------------------" | |
echo -e "$(tput setaf 5)Running in dry run mode. No files will be changed.$(tput sgr0)" | |
echo -e "---------------------------------------------------------------" | |
printf "\n" | |
else | |
DRY_RUN=false | |
# Output to console. | |
echo -e "---------------------------------------------------------------" | |
read -p "$(tput setaf 5)Running in live mode. Continue? y/n $(tput sgr0)" choice | |
echo -e "---------------------------------------------------------------" | |
printf "\n" | |
if [[ $choice = "n" ]]; then | |
exit 0 | |
fi | |
fi | |
# Array join function. | |
function join_by { local d=$1; shift; echo -n "$1"; shift; printf "%s" "${@/#/$d}"; } | |
# Path of directory that holds completed downloads. | |
COMPLETEDDOWNLOADDIR=$1 | |
# Path of directory that holds processed files. | |
PROCESSEDDIR="./processed" | |
# Path of files. | |
FILES="$COMPLETEDDOWNLOADDIR/*" | |
FILES_GLOB=$(shopt -s nullglob dotglob; echo $FILES) | |
# Check for files. | |
if (( !${#FILES_GLOB} )); then | |
# Output to console. | |
echo -e "---------------------------------------------------------------" | |
echo -e "$(tput setaf 1)$COMPLETEDDOWNLOADDIR has no files. Exiting.$(tput sgr0)" | |
echo -e "---------------------------------------------------------------" | |
printf "\n" | |
exit 0 | |
fi | |
# Loop through all files. | |
for f in $FILES | |
do | |
# Check this is not a directory. | |
if [ ! -d "$f" ]; then | |
# Output to console. | |
echo -e "---------------------------------------------------------------" | |
# Get filename of file without directory. | |
FILENAME=${f##*/} | |
# Output to console. | |
echo -e "$(tput setaf 5)* Current: '$FILENAME'$(tput sgr0)" | |
# Get filename of file without extension. | |
FILENAME=${FILENAME%.*} | |
# Replace any underscores with spaces. | |
FILENAME=$(echo "$FILENAME" | tr '_' ' ') | |
# Replace any hyphens with spaces. | |
FILENAME=$(echo "$FILENAME" | tr '-' ' ') | |
# Replace any periods with spaces. | |
FILENAME=$(echo "$FILENAME" | tr '.' ' ') | |
# Get file extension. | |
EXTENSION="${f##*.}" | |
# Create array. | |
FILENAMEARRAY=() | |
# Uppercase each word. | |
for WORD in $FILENAME; do | |
# Uppercase first character of word. | |
FILENAMEARRAY+=($(tr '[:lower:]' '[:upper:]' <<< ${WORD:0:1})${WORD:1}) | |
done | |
# Overwrite filename var with a parsed version. | |
FILENAME=$(join_by ' ' "${FILENAMEARRAY[@]}") | |
# Rename and move file. | |
if [[ $DRY_RUN = false ]]; then | |
mv "$f" "$COMPLETEDDOWNLOADDIR/$FILENAME.$EXTENSION" | |
mv "$COMPLETEDDOWNLOADDIR/$FILENAME.$EXTENSION" "$PROCESSEDDIR/$FILENAME.$EXTENSION" | |
fi | |
# Output to console. | |
echo -e "$(tput setaf 6)* Renamed: '$FILENAME.$EXTENSION'$(tput sgr0)" | |
echo -e "$(tput setaf 6)* New Location: '$PROCESSEDDIR/$FILENAME.$EXTENSION'$(tput sgr0)" | |
printf "\n" | |
# Output to console. | |
echo -e "$(tput setaf 2)* File processed successfully.$(tput sgr0)" | |
# Output to console. | |
echo -e "---------------------------------------------------------------" | |
printf "\n" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment