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:
ps. I am not aloud to use python as we have not covered it in this section of the course
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