there was a possibility that a file like ~blah.xlsx could overwrite blah.xlsx, which is a huge issue. don't want to scrap any active files like that, so now we are checking at different stages in the rename process to ensure there are at least no overwrites.
sub rename_lower {
# go through the list of files passed in.
foreach $current (&glob_list(@_)) {
-#print "unfiltered: '$current'\n";
+#print "renlower--unfiltered: '$current'\n";
if ($current =~ /[A-Z]/) {
#print "current is '$current'\n";
local $old_name = $current;
#print "command A is: rename [$old_name] [$intermediate_name]\n";
#print "command B is: rename [$intermediate_name] [$new_name]\n";
#print "\n";
+
+ # safety rails here--we don't want to rename over top of existing files.
+ if (-e "$intermediate_name") {
+ print("error: file called '$intermediate_name' already exists; skipping so we do not overwrite.\n");
+ next;
+ }
+ if (-e "$new_name") {
+ print("error: file called '$new_name' already exists; skipping so we do not overwrite.\n");
+ next;
+ }
+
rename($old_name, $intermediate_name)
- || die "failed to do initial rename";
+ || die "error: failed to do initial rename";
rename($intermediate_name, $new_name)
- || die "failed to do secondary rename";
+ || die "error: failed to do secondary rename";
print "'$old_name' => '$new_name'\n";
}
}
exit 1
fi
+# allow error reporting from inside our loop.
+retval=0
+
while [ $# -gt 0 ]; do
file="$1"; shift
# first turn spaces into underscores. then process characters we don't want
# underscore dash underscore into just dash.
newname="$(echo "$file" | tr -s ' ' '_' | tr -d "\$\!|@&#%}{)(][\\\~',:?><\"" | sed -e 's/__/_/g' | sed -e 's/\([0-9]\)_\./\1./g' | sed -e 's/_-_/-/' )"
if [ "$file" != "$newname" ]; then
- # we've effected a name change, so let's actually do it.
- echo "'$file' => '$newname'"
- mv "$file" "$newname"
+ if [ ! -e "$newname" ]; then
+ # we've decided on an effective name change, so let's actually rename.
+ echo "'$file' => '$newname'"
+ mv "$file" "$newname"
+ else
+ echo "error: skipping rename '$file' => '$newname' due to existing file."
+ retval=1
+ fi
fi
done
+exit $retval
while [ $# -gt 0 ]; do
arg="$1"; shift
- if [[ $arg =~ ~* ]]; then
-echo "skipping tilde style name: '$arg'"
+ if [[ $arg == ~* ]]; then
+#echo "skipping tilde style name: '$arg'"
continue
fi
echo "=> did not find a file or directory named '$arg'."
continue
fi
+#echo "name to spacem out is: '$arg'"
# we capture the output of the character replacement operation for reporting.
# this is done first since some filenames cannot be properly renamed in perl (e.g. if they
# have pipe characters apparently).
intermediate_name="$(bash "$FEISTY_MEOW_SCRIPTS/files/replace_spaces_with_underscores.sh" "$arg")"
local saw_intermediate_result=0
- if [ -z "$intermediate_name" ]; then
+ if [[ $intermediate_name == error:* ]]; then
+ echo "error seen during name massage phase 1 on '$arg'"
+ continue
+ elif [ -z "$intermediate_name" ]; then
# make sure we report something, if there are no further name changes.
intermediate_name="'$arg'"
else
actual_file="$(echo $intermediate_name | sed -e "s/'\([^']*\)'/\1/")"
final_name="$(perl "$FEISTY_MEOW_SCRIPTS/files/renlower.pl" "$actual_file")"
local saw_final_result=0
- if [ -z "$final_name" ]; then
+ if [[ $final_name == error:* ]]; then
+ echo "error seen during name massage phase 2 on '$arg'"
+ continue
+ elif [ -z "$final_name" ]; then
final_name="$intermediate_name"
else
final_name="$(echo $final_name | sed -e 's/.*=> //' )"