Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UNIX rename script, DOS Style

Status
Not open for further replies.

abeath08

Programmer
Apr 20, 2008
2
GB
HI,
as part of my college work, I have to create a UNIX command which can rename in the style of the DOS rename. I have the basic code working, which will rename the files. But i cant seem to get the file extension, rename bit sorted. I think it might be my regex but not sure, any help would be good. Here is the code i have so far:
Code:
#!/bin/sh
#
# - A DOS STYLE RENAME
#
case $# in
0)			echo "Usage: dosRename file [ ... ] target"
			exit
			;;
#2)			echo "dosRename: $file: Can't have anything after target"
#			exit
#			;;
'..')		echo "dosRename: $file: Can't have more than one dot"
			exit
			;;
'**')  		echo "dosRename: $file: Can't have more than one asterisk"
			exit
			;;
#[!-f])  	echo "dosRename: $file: Doesn't exist"
#			exit
#			;;
6)  		echo "dosRename: $file: Must have a dot"
			exit
			;;
7)			echo "dosRename: No target"
			exit
			;;
*)			for file
			do
				case $file in
				[A-Za-z0-9]*\.\*) 	filePart=name
									;;
				\*\.[A-Za-z0-9]*) 	filePart=etx
									;;
				esac
			done
			endFile=$file
			fileName=`echo $endFile | sed -e 's/\.\*//'`
			extension=`echo $endFile | sed -e 's/\*\./\./'`
			while [ $# -ne 1  ]
			do
					case $filePart in
						'name')     result=`echo $1 | sed -e "s/[A-Za-z0-9]*\.\(.*[A-Za-z0-9]*\)/$fileName\.\1/"`
									mv $1 $result
									;;
						'ext')    	result=`echo $1 |sed -e "s/\.[A-Za-z0-9]*/$extension/"`
									mv $1 $result
									;;
					esac
				 shift 1
			done
			 ;;
esac
ps. I am not aloud to use python as we have not covered it in this section of the course
 
its ok i had a spelling mistake, all sorted thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top