forked from NMAAHC/nmaahcmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename
executable file
·89 lines (81 loc) · 3.08 KB
/
rename
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/bin/bash
_report(){
local RED="$(tput setaf 1)" # Red - For Warnings
local GREEN="$(tput setaf 2)" # Green - For Declarations
local BLUE="$(tput setaf 4)" # Blue - For Questions
local NC="$(tput sgr0)" # No Color
local COLOR=""
local STARTMESSAGE=""
local ENDMESSAGE=""
local ECHOOPT=""
local LOG_MESSAGE=""
OPTIND=1
while getopts ":qdwstn" OPT; do
case "${OPT}" in
q) COLOR="${BLUE}" ;; # question mode, use color blue
d) COLOR="${GREEN}" ;; # declaration mode, use color green
w) COLOR="${RED}" ; LOG_MESSAGE="Y" ;; # warning mode, use color red
s) STARTMESSAGE+=([$(basename "${0}")] ) ;; # prepend scriptname to the message
t) STARTMESSAGE+=($(_get_iso8601) '- ' ) ;; # prepend timestamp to the message
n) ECHOOPT="-n" ;; # to avoid line breaks after echo
esac
done
shift $(( ${OPTIND} - 1 ))
MESSAGE="${1}"
echo "${ECHOOPT}" "${COLOR}${STARTMESSAGE[@]}${MESSAGE}${NC}"
[ "${LOG_MESSAGE}" = "Y" ] && _log -w "${MESSAGE}"
}
# chmod +x FILENAME
_report -q -n "Please enter a Base File Name: "
read BASE_FILE_NAME
_report -q -n "Plese drag in a list of files: "
read -e -a SO_MANY_FILES
echo
_report -d -n "Thank you. You listed:"
echo
for A_FILE in "${SO_MANY_FILES[@]}" ; do
echo " + ${A_FILE}"
done
echo
for A_FILE in "${SO_MANY_FILES[@]}" ; do
if [[ ! -f "${A_FILE}" ]] ; then
_report -r "Error: ${A_FILE} is not a file. Stopping."
exit 1
fi
done
for A_FILE in "${SO_MANY_FILES[@]}" ; do
if [[ ! -r "${A_FILE}" ]] ; then
_report -r "Error: ${A_FILE} is not readable. Stopping."
exit 1
fi
done
FILE_TEMPLATE="$(dirname "${SO_MANY_FILES[1]}")/${BASE_FILE_NAME}_ORIGINAL_FILE_NAME.csv"
echo "# Model Info - Do not edit," > "${FILE_TEMPLATE}"
echo "SI.CORE.VIDEO.MODEL," >> "${FILE_TEMPLATE}"
echo "# Headers/Properties - Do not edit," >> "${FILE_TEMPLATE}"
echo "Asset Name,Original File Name" >> "${FILE_TEMPLATE}"
echo
_report -d -n "Below is how your files will be copied and renamed."
echo
_report -d -n "The files will be copied to $(dirname "${A_FILE}")"
echo
COUNTER=1
for A_FILE in "${SO_MANY_FILES[@]}" ; do
A_EXTENSION="${A_FILE##*.}"
NEW_NAME="$(dirname "${A_FILE}")/${BASE_FILE_NAME}${COUNTER}.${A_EXTENSION}"
#echo "$(basename "${NEW_NAME}"),$(basename "${A_FILE}")" >> "${FILE_TEMPLATE}"
echo " $(basename "${A_FILE}") >> >> $(basename "${NEW_NAME}")"
COUNTER=$(( COUNTER + 1 ))
done
echo
read -p "If that looks good, please press Enter to copy and rename the files."
echo
COUNTER=1
for A_FILE in "${SO_MANY_FILES[@]}" ; do
A_EXTENSION="${A_FILE##*.}"
NEW_NAME="$(dirname "${A_FILE}")/${BASE_FILE_NAME}${COUNTER}.${A_EXTENSION}"
echo "$(basename "${NEW_NAME}"),$(basename "${A_FILE}")" >> "${FILE_TEMPLATE}"
cp -n "${A_FILE}" "${NEW_NAME}"
COUNTER=$(( COUNTER + 1 ))
done
cowsay "Done! The template is at ${FILE_TEMPLATE}. Beautiful renaming job! You are a superstar!"