Register
It is currently Fri Aug 01, 2014 9:54 am

trimSpaces from filenames (or any other char)


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Tue May 17, 2005 8:09 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
Code:
#!/bin/bash

# Author: Joshua Bailey
# Script: trimFilename
# *** means works
# Syntax: trimFilename "<filename>" <whatToRemove>
# Example: trimFilename "some picture.jpg"
# Will return: somepicture.jpg
# Example: trimFilename some_picture.jpg _
# Will return: somepicture.jpg
# *** Example: trimFilename --space
# Will: Remove all spaces from all filenames in the directory
# *** Example: trimFilename --underscore
# Will: Remove all underscores from all filenames in the directory

fSlash='/'
STRING=""
LENGTH=0
CHAR=''
FileName=""
dir=""
record=""
NewString=""
rmChar=''

function notAForwardSlash
{
   if [[ $1 != $fSlash ]]
        then
      return 0
        else
           return 1
        fi
}
### end notAForwardSlash ###

function getFileName
{
   STRING=$1
        LENGTH=${#STRING}
   for ((n=0;n <= $LENGTH; n++))
        do
      CHAR=${STRING:$n:1}
      if notAForwardSlash $CHAR
      then
         FileName=$FileName$CHAR
      else
         FileName=""
      fi
   done
}
### end getFileName ###

function readWriteError()
{
   echo "*** You do not have read/write access to $1! ***"
}

function getDirectory()
{
   read -p "Please enter the directory you would like to edit: " dir
   if [[ ! -e $dir ]]
   then
      echo "*** $dir does not exist! ***"
      echo "*** Aborting! ***"
      exit
   fi
   echo "*** About to edit $dir ***"
}
### end getDirectory ###

function checkReadWriteAccess()
{
   ls $1 > /dev/null
   if [[ $? -eq 1 ]]
   then
      return 1
   else
      touch $1/bob.yea\ do0d 2> /dev/null
      if [[ $? -eq 1 ]]
      then
         rm $1/bob.yea\ do0d 2> /dev/null
         return 1
      else
         rm $1/bob.yea\do0d 2> /dev/null
         return 0
      fi
      
   fi
}
### end checkReadWriteAccess ###

function rmAllUnderScores()
{
   ls *_* > uFileNames
   
   until ! read record
   do
      trimspaces "$record" '_'
      mv "$record" "$NewString"
   done < uFileNames
}
### end rmAllUnderScores ###

function rmAllSpaces()
{
   ls *\ * > sFileNames
   
   until ! read record
   do
      trimspaces "$record"
      mv "$record" "$NewString"
   done < sFileNames
}
### end rmAllSpaces
   
function trimspaces()
{
   NewString=""
   STRING="$1"
   rmChar=$2
   if [[ $rmChar = "" ]]
   then
      rmChar=' '
   fi
   for ((i=0; i<${#STRING}; i++))
   do
      CHAR=${STRING:$i:1}
      if [[ $CHAR != $rmChar ]]
      then
         NewString=$NewString$CHAR
      fi
   done
}
### end trimspaces ###

if [[ $1 != "" ]]
then
   arg="$1"
   if [[ ${arg:0:2} = "--" && $2 = "" ]]
   then
      case $arg in
         --space) getDirectory;
             if checkReadWriteAccess $dir
             then
                cd $dir;
               rmAllSpaces;
               cd ~-;
            else
               readWriteError $dir;
               exit;
            fi;
            ;;
         --underscore) getDirectory;
            if checkReadWriteAccess $dir
            then
               cd $dir;
               rmAllUnderScores;
               cd ~-;
            else
               readWriteError $dir;
               exit;
            fi;
            ;;
         *) echo "not made yet";
            ;;
      esac
   else
      arg2="$2"
      if [[ ! -e $arg2 ]]
      then
         echo "*** $arg2 does not exist! ***"
         echo "*** Aborting! ***"
         exit
      else
         getFileName "$arg2"
         #### must get directory ####
      fi
   fi
fi
### must finish single file changing ###


Top
 Profile  
 PostPosted: Fri Oct 28, 2005 9:29 pm   

Joined: Sat Jun 04, 2005 3:16 am
Posts: 16
Location: That Lunar Colony That Everybody Denies the Existance of.
just an FYI for everybody out there, the "rename" command lets you do sed-like stuff on filenames. Its a perl script and comes in handy. (jam handy in fact)


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: Bing [Bot] and 6 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