Hi everyone!
OK ... so this is not a new idea, and there's thousands of forum posts on "how to" strip invalid characters using sed and tr etc.
The aim is to create a simple, friendly UI, and maybe in the future an option packed GUI:
Actions:1) Control characters stripped (NEED HELP!)
2) Underscores (_) converted to Spaces ( )
3) Leading, trailing and multiple Spaces ( ) stripped
(This is buggy: doesn't strip leading in sub-dirs.
and before file type suffixes e.g. .jpg)
4) Tildes (~) converted to Hyphens (-)
5) All punctuation other than fullstop (.) is stripped
(feature under development: any files with backslashes fails)
6) All names are converted to UTF-8 (using transliteration)
with invalid characters being stripped.
Please help me:1) strip invalid characters (e.g. 000A - feed - new line etc. see picture below)
2) get rid of backslashes (\) in file and folder names
3) rename all files in directories and subdirectories first,
then the directories and sub directories last
4) tell me - is all that license stuff I've put in even necessary?
Feel free to use totally different methods if they're better! :)
This is a picture of what filenames with invalid characters show up as:
Attachment:
sample_control_character.png
and here's the script:Code:
#!/bin/bash
#
# Nice Name v.0.1 (nicename)
#
# Written by Arman Haghi, alliedmedia.com.au
#
# Copyright 2012 Arman Haghi
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.";
#
# Note: if UTF-8 is not appropriate simply change it to ASCII :)
#
clear;
echo " ============================================";
echo "| N i c e N a m e V . 0 . 1 |";
echo "|============================================|";
echo "| Written by Arman Haghi, alliedmedia.com.au |";
echo " ============================================ ";
sleep 1;
echo "";
echo "==================================================";
echo "";
echo "Target: All file and folder names in this directory,";
echo " as well as those in all subdirectories.";
echo "";
echo "Actions:";
echo "";
echo "1) Control characters stripped (feature not yet developed)";
echo "";
echo "2) Underscores (_) converted to Spaces ( )";
echo "";
echo "3) Leading, trailing and multiple Spaces ( ) stripped";
echo " (This is buggy: doesn't strip leading in sub-dirs.";
echo " and before file type suffixes e.g. .jpg)";
echo "";
echo "4) Tildes (~) converted to Hyphens (-)";
echo "";
echo "5) All punctuation other than fullstop (.) is stripped";
echo " (feature under development: any files with backslashes fails)";
echo "";
echo "6) All names are converted to UTF-8 (using transliteration)";
echo " with invalid characters being stripped.";
echo "";
sleep 1;
echo "==================================================";
echo "";
echo " W A R N I N G !";
echo "";
echo "THERE IS NO UNDO - IF IN DOUBT: MAKE A BACKUP FIRST";
echo "";
echo "==================================================";
echo "";
echo " Would you like to continue?";
read -p "
Enter Y to start
Enter T to test
Enter N to exit
: " continue
if [ $continue == y ]
then
echo "";
echo "You have typed Y, proceeding in 2 seconds...";
sleep 2
echo "";
echo "OUTPUT:";
echo "==================================================";
find . | while read file;
do A=$( echo $file | tr "_" " " | tr "~" "-" | sed s/\"//g | sed s/\'//g | tr -d '€‚ƒ„…†‡ˆ‰‹Œ•–—˜›¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿×÷!#$%&()*+,:;<=>?@[]^_`{|}' | iconv --verbose -c -t UTF-8//TRANSLIT );
mv "$file" "$A";
done;
echo "==================================================";
echo "";
echo "Script is complete.";
echo "";
echo "Note: If sub-directory names are affected, then you may have to run this script again";
echo "";
fi
if [ $continue == t ]
then
echo "";
echo "You have typed T, proceeding in 2 seconds...";
sleep 2
echo "";
echo "TEST OUTPUT:";
echo "==================================================";
find . | while read file;
do A=$( echo $file | tr "_" " " | tr "~" "-" | sed s/\"//g | sed s/\'//g | tr -d '€‚ƒ„…†‡ˆ‰‹Œ•–—˜›¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿×÷!#$%&()*+,:;<=>?@[]^_`{|}' | iconv --verbose -c -t UTF-8//TRANSLIT );
echo $A
done;
echo "==================================================";
echo "";
echo "Test output is complete.";
echo "";
echo "Note: If sub-directory names are affected, then you may have to run this script again";
echo "";
fi
if [ $continue == n ]
then
echo "";
echo "You have type N, so the script is exiting.";
echo "";
exit
fi
if [ $continue != y ] && [ $continue != t ] && [ $continue != n ]
then
echo "";
echo "You have not typed an invalid option, so the script is exiting.";
echo "";
exit
fi