Register
It is currently Wed Nov 26, 2014 6:40 am

Presenting: nicename v.0.1 - makes filenames nice!


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Sat Apr 07, 2012 8:15 am   

Joined: Fri Apr 06, 2012 7:04 pm
Posts: 1
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



You do not have the required permissions to view the files attached to this post.


Top
 Profile  
 PostPosted: Mon Apr 09, 2012 4:19 pm   

Joined: Mon Apr 09, 2012 1:45 pm
Posts: 3
If you intend this as a multipurpose script, esecially if you want it to be useful for generating nice filenames, I would suggest you not change "_" to " " but, indeed, I would do the opposite. There are lots of places where a space will mess things up but none that I know of where a _ will. Or you could use zenity and let the user choose between different versions of nice with one of the options being nice for filenames, perhaps even a real strict version nice for 8.3 filenames.

Whatever you do about that, tr will do everything you want. I use this:

Code:
#!/bin/bash -x
#
#Takes whatever is SELECTED (you don't have to copy it, just highlight it), strips out the problem characters and puts the cleaned string in the x clipboard.
#To make it use what is COPIED instead of what is SELECTED insert "  -selection clip-board" after the "-o" in the first command.
xclip -o | tr [:space:] '_' | tr -c [:print:] '_' | tr '/?*~:$' '-' | tr '\012\015\134\135\047\042\054\140' '-' | tr -s '-' '-' | tr -s '_' '_' | xclip -i -selection clip-board


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: TurnitinBot [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP