#!/bin/bash # FixFileExtensions.sh # Description: scans a given directory for files without file extensions, reads the resource fork using GetFileInfo, then adds the appropriate file extension # Written by: Scott Russell, IT Support Engineer, University of Notre Dame # Created on: Fri May 9 17:33:15 EDT 2008 # Last Modified: Wed Oct 1 23:05:21 EDT 2008 ## The "GetFileInfo" utility must be installed. This normally gets installed into /usr/bin/ as part of the Developer Tools installation. TestOnly=true ## Initialize this to "true" so that you are forced to enter "change" on the command-line doChangeName=false if [[ ! -e /usr/bin/GetFileInfo ]] ; then echo echo " \"/usr/bin/GetFileInfo\" must be installed for this script to work." echo " Please install it and then re-run this script." echo exit 1 fi ## You may pass only 1 or 2 parameters if [[ $# -gt 2 || $# == 0 ]] ; then echo echo "Usage: `basename $0` (TEST mode - default behavior)" echo " `basename $0` change (CHANGE mode)" echo exit 1 ## If there's only one parameter, it had better be a valid directory elif [[ ! -d "${1}" ]] ; then echo echo " You must pass a valid directory to scan." echo " `basename ${0}` " echo echo " NOTE: This ALWAYS runs in TEST mode unless you pass the \"change\" flag at the end of the command:" echo " `basename ${0}` change" echo exit 1 ## If there are 2 parameters, the first had better be a valid directory and the second should be "change" elif [[ "${2}" == "change" ]] ; then TestOnly=false ## We're doing it for real, baby! ## Any other $2 will simply be ignored and the script will continue in TEST mode. fi if [[ "${TestOnly}" == true ]] ; then echo echo " Running in TEST mode only. No items will be renamed but you will see the list of changes that would be made." echo echo " NOTE: This ALWAYS runs in TEST mode unless you pass the \"change\" flag at the end of the command:" echo " `basename ${0}` change" echo echo "Press any key to continue or hit -c to quit." echo read Nonsense fi fixFileExtensions() { FileFullPath="${1}" FileName=`basename "${1}"` DirName=`dirname "${1}"` originalFileName="${FileName}" ## Set this here so we can refer to it if/when we try to change the file name FileType=`/usr/bin/GetFileInfo -t "${1}" | tr -d \"` FileCreator=`/usr/bin/GetFileInfo -c "${1}" | tr -d \"` FileExt="" ## Always initialize it to blank #echo "${FileFullPath}" #echo "${FileType} ... ${FileCreator}" #echo if [[ "${FileCreator}" != "" || "${FileType}" != "" ]] ; then if [[ "${FileName}" != *.* ]] ; then case "${FileType}" in 8BPS ) FileExt="psd" ;; EPSF ) FileExt="eps" ;; FIN3 ) FileExt="MUS" ;; FMP5 ) FileExt="fp5" ;; FMP7 ) FileExt="fp7" ;; GIFf ) FileExt="gif" ;; JPEG ) FileExt="jpg" ;; M4A ) FileExt="m4a" ;; MPEG ) FileExt="mpeg" ;; MPG3 ) FileExt="mp3" ;; MooV ) FileExt="mov" ;; Mp3 ) FileExt="mp3" ;; PDF ) FileExt="pdf" ;; PNGf ) FileExt="png" ;; PPSS ) FileExt="pps" ;; RTF ) FileExt="rtf" ;; SIT5 ) FileExt="sit" ;; SITD ) FileExt="sit" ;; SLD3 ) FileExt="ppt" ;; SLD8 ) FileExt="ppt" ;; TIFF ) FileExt="tif" ;; TaxR ) FileExt="tax" ;; W6BN ) FileExt="doc" ;; W8BN ) FileExt="doc" ;; WCAT ) FileExt="acq" ;; WDBN ) FileExt="doc" ;; WPD1 ) FileExt="wpm" ;; WPD2 ) FileExt="wpm" ;; WPD3 ) FileExt="wpm" ;; WPD4 ) FileExt="wpm" ;; XLS4 ) FileExt="xls" ;; XLS5 ) FileExt="xls" ;; XLS8 ) FileExt="xls" ;; ZIP ) FileExt="zip" ;; devi ) FileExt="dmg" ;; devr ) FileExt="dmg" ;; esac if [[ -z "${FileExt}" ]] ; then ## if we didn't add an extension yet case "${FileCreator}" in ALD4 ) FileExt="tif" ;; ARTZ ) FileExt="eps" ;; CARO ) FileExt="pdf" ;; DmWr ) FileExt="dwt" ;; FMP5 ) FileExt="fp5" ;; FMP7 ) FileExt="fp7" ;; GCon ) FileExt="gif" ;; IICA ) FileExt="ica" ;; MIT6 ) FileExt="tax" ;; MIT7 ) FileExt="tax" ;; RSED ) FileExt="rsrc" ;; SIT! ) FileExt="sit" ;; SITx ) FileExt="sitx" ;; TVOD ) FileExt="mov" ;; ddsk ) FileExt="dmg" ;; dplt ) FileExt="app" ;; hDmp ) FileExt="dmg" ;; icnC ) FileExt="icns" ;; mAmp ) FileExt="mp3" ;; mMPG ) FileExt="mpeg" ;; ogle ) FileExt="tif" ;; prvw ) FileExt="pdf" ;; esac fi fi fi ## Check for bad characters in the file name and translate them to dashes for eachBadChar in \" \* \< \> ? \\ \| do if [[ "${FileName}" == *"${eachBadChar}"* ]] ; then FileName=`echo "${FileName}" | tr "${eachBadChar}" -` doChangeName=true fi done [[ -n "${FileExt}" ]] && doChangeName=true ## Make all the necessary changes in one fell swoop if [[ "${doChangeName}" == true ]] ; then ## check to see if the new file name exists already EndTag=1 newFileName="${FileName}" if [[ -n "${FileExt}" ]] ; then ## we're definitely adding a file extension; maybe we're changing the name while [[ -e "${DirName}/${newFileName}.${FileExt}" ]] do newFileName="${FileName}-${EndTag}" let EndTag+=1 done FileName="${newFileName}" ## If we made any corrections, capture them back into FileName else ## we're merely changing the name, not adding a file extension ## In this case, we'll probably never hit a conflict. A conflict would only result if they had two files named: ## bogus?name.txt (which we will change to bogus-name.txt) and ## bogus-name.txt (already exists; incrementing the EndTag counter prevents us from clobbering it) ## If, in fact, this scenario does exist, they'll end up with "bogus-name.txt-1", which should be fine anyway while [[ -e "${DirName}/${newFileName}" ]] do newFileName="${FileName}-${EndTag}" let EndTag+=1 done FileName="${newFileName}" ## If we made any corrections, capture them back into FileName fi echo -n " $(date): Changing \"${DirName}/${originalFileName}\" to \"${FileName}" [[ -n "${FileExt}" ]] && echo -n ".${FileExt}" echo "\"" [[ -n "${FileExt}" ]] && FileName="${FileName}.${FileExt}" if [[ "${TestOnly}" == false ]] ; then # Check to make sure the new file name doesn't already exist; rename if necessary mv "${FileFullPath}" "${DirName}/${FileName}" fi ## Reset the "doChangeName" flag to "false" doChangeName=false fi } ### APPLICATION ### FileList=`find "${1}" -type f` echo "${FileList}" > /tmp/FileList-${StartTime} echo { while read eachFile do fixFileExtensions "${eachFile}" done } < "/tmp/FileList-${StartTime}" echo exit 0 NOTE: The "GetFileInfo" utility must also be installed. This normally gets installed into /usr/bin/ as part of the Developer Tools installation.