From b7a6c144ac89793e426f24a0798d5ea5083e8f6c Mon Sep 17 00:00:00 2001 From: Chris Koeritz Date: Fri, 10 Apr 2015 20:57:50 -0400 Subject: [PATCH] wow, very tasty modifications to renlower to get it to handle more patterns that might have perl special characters in them, which has turned it into a nice general way of translating ls style regular expressions into perl ones. not sure i handled all the cases from ls properly yet, but at least it's working on some example files with crazy bad characters in them. --- scripts/files/filename_helper.pl | 24 ++++++++++++------- scripts/files/renlower.pl | 3 +++ .../files/replace_spaces_with_underscores.sh | 2 +- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/scripts/files/filename_helper.pl b/scripts/files/filename_helper.pl index 318e5829..a6632cf5 100644 --- a/scripts/files/filename_helper.pl +++ b/scripts/files/filename_helper.pl @@ -66,20 +66,26 @@ sub glob_list { local(@files_found) = readdir(WHERE); closedir WHERE; foreach $possible_name (@files_found) { - # zoom through the list and see if we need to add it to the ones - # matching the passed in patterns. -# if ( ($possible_name eq ".") || ($possible_name eq "..") ) { -# # skip the directory entries. -# print "skipping dir entries\n"; -# next; -# } - # we need to process this a bit; directory patterns are different. + # we need to process the pattern a bit; directory patterns are different + # from perl regular expression patterns, so we end up massaging any "ls" + # wildcards into an equivalent perl-style one below. local($match) = $chopped_filename[1]; +#hmmm: would be nice to combine the replacements into a long batch instead of separate commands, but i do not seem to know how to do that yet in perl. $match =~ s/\./\\./g; # replace periods with escaped ones. $match =~ s/\*/.*/g; # replace asterisks with dot star. $match =~ s/\+/\\+/g; # escape plusses. + $match =~ s/\?/\\?/g; # escape question marks. + $match =~ s/\|/\\?/g; # escape pipe char. + $match =~ s/\$/\\\$/g; # escape dollar sign. + $match =~ s/\[/\\[/g; # escape open bracket. + $match =~ s/\]/\\]/g; # escape close bracket. + $match =~ s/\(/\\(/g; # escape open quote. + $match =~ s/\)/\\)/g; # escape close quote. + $match =~ s/\{/\\{/g; # escape open curly bracket. + $match =~ s/\}/\\}/g; # escape close curly bracket. + $match = "^" . $match . "\$"; # make sure the whole thing matches. -#print "possibname is $possible_name\n"; +#print "possibname is '$possible_name':\n"; if ($possible_name =~ /$match/) { # this one matches so add it. push @to_return, $chopped_filename[0] . $possible_name; diff --git a/scripts/files/renlower.pl b/scripts/files/renlower.pl index 7f9f3409..d4e3f11f 100644 --- a/scripts/files/renlower.pl +++ b/scripts/files/renlower.pl @@ -33,6 +33,7 @@ exit 0; sub rename_lower { # go through the list of files passed in. foreach $current (&glob_list(@_)) { +#print "unfiltered: '$current'\n"; if ($current =~ /[A-Z]/) { #print "current is '$current'\n"; local $old_name = $current; @@ -43,8 +44,10 @@ sub rename_lower { local $new_name = $dir . $lc_name; #print "new name='$new_name'\n"; local $intermediate_name = $dir . "RL" . rand() . ".tmp"; +#print "\n"; #print "command A is: rename [$old_name] [$intermediate_name]\n"; #print "command B is: rename [$intermediate_name] [$new_name]\n"; +#print "\n"; rename($old_name, $intermediate_name) || die "failed to do initial rename"; rename($intermediate_name, $new_name) diff --git a/scripts/files/replace_spaces_with_underscores.sh b/scripts/files/replace_spaces_with_underscores.sh index 375b8274..3f26f6a0 100644 --- a/scripts/files/replace_spaces_with_underscores.sh +++ b/scripts/files/replace_spaces_with_underscores.sh @@ -12,7 +12,7 @@ fi while [ $# -gt 0 ]; do file="$1"; shift - newname="$(echo "$file" | tr -s ' ' '_' | tr -d "}{)(][\\\~',:?><\"" | sed -e 's/\([0-9]\)_\./\1./g' | sed -e 's/_-_/-/' )" + newname="$(echo "$file" | tr -s ' ' '_' | tr -d "\$@#%}{)(][\\\~',:?><\"" | 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'" -- 2.34.1