If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > MS-DOS Style Rename

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-01-04, 05:28
zeta_acosta zeta_acosta is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
Exclamation MS-DOS Style Rename

Deleted

Last edited by zeta_acosta; 04-02-04 at 07:04.
Reply With Quote
  #2 (permalink)  
Old 04-01-04, 09:34
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
Something like this ?
Code:
#
# Usage - Display error message and exit
#

Usage() {
   [ $# -ne 0 ] && echo "\n$*\n" >&2
   cat <<-EOD_USAGE >&2
        Usage: $0 file... target
        file...  List of files to rename
        target   Target name

        EOD_USAGE
   exit 1
}

#
# Get and verify arguments
#

# Argument count

[ $# -lt 2 ] && Usage "Missing arguments." 

# Files to rename

while [ $# -gt 1 ]
do
   file=$1
   case "$file" in
      *.*.*|*\**) Usage "Invalid file name : $file" ;;
   esac
   [ -e "$file" ] || Usage "File not found : $file"
   file_list="$file_list $file"
   shift
done

# Target name

target=$1
case "$target" in
   *.*.*|*\**\**) Usage "Invalid target name : $file" ;;
esac
target_nam=`echo "$target" | cut -d. -f1`
target_ext=`echo "$target" | cut -d. -f2` 

#
# Rename loop
#

for file in $file_list
do
   # Actual file name and extension

   file_nam=`echo "$file" | cut -d. -f1`
   file_ext=`echo "$file" | cut -d. -f2` 
 
   # New file name and extension

   if [ "$target_nam" = '*' ]
      then new_nam="$file_nam"
      else new_nam="$target_nam"
   fi
   if [ "$target_ext" = '*' ]
      then new_ext="$file_ext"
      else new_ext="$target_ext"
   fi
   [ -n "$file_ext" ] && file_ext=".$file_ext"
   [ -n "$new_ext" ] && new_ext=".$new_ext"
   new="${new_nam}${new_ext}"

   # Rename

   echo "Rename file $file to $new ..."
   if [ -e "$new" ]
      then echo "Not renamed, target file already exists : $new" >&2
      else mv "$file" "$new"
   fi
done
__________________
Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 04-02-04, 02:50
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
You have an extra exclamation point at the end of the line, must be :
#!/bin/sh

Now you must understand the script.
For every line try to explain what is done.
Consult your documentation and/or man pages

An exercise: replace the pipe 'echo | cut' by a variable substitution.
__________________
Jean-Pierre.
Reply With Quote
  #4 (permalink)  
Old 04-02-04, 04:11
zeta_acosta zeta_acosta is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
Smile

Thank you Jean Pierre your a dude!
Reply With Quote
  #5 (permalink)  
Old 04-02-04, 05:53
zeta_acosta zeta_acosta is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
Final Time now!

Why is this just being echoed to the screen, what have I done now! Please Please Please help beginning to hate shell!

#!/bin/sh

Usage() {
[ $# -ne 0 ] && echo "\n$*\n" >&2
cat <<-EOD_USAGE >&2
Usage: $0 file... target
file... List of files to rename
target Target name

EOD_USAGE
exit 1
}

#
# Get and verify arguments
#

# Argument count

[ $# -lt 2 ] && Usage "Missing arguments."

# Files to rename

while [ $# -gt 1 ]
do
file=$1
case "$file" in
*.*.*|*\**) Usage "Invalid file name : $file" ;;
esac
[ -e "$file" ] || Usage "File not found : $file"
file_list="$file_list $file"
shift
done

# Target name

target=$1
case "$target" in
*.*.*|*\**\**) Usage "Invalid target name : $file" ;;
esac
target_nam=`echo "$target" | cut -d. -f1`
target_ext=`echo "$target" | cut -d. -f2`

#
# Rename loop
#

for file in $file_list
do
# Actual file name and extension

file_nam=`echo "$file" | cut -d. -f1`
file_ext=`echo "$file" | cut -d. -f2`

# New file name and extension

if [ "$target_nam" = '*' ]
then new_nam="$file_nam"
else new_nam="$target_nam"
fi
if [ "$target_ext" = '*' ]
then new_ext="$file_ext"
else new_ext="$target_ext"
fi
[ -n "$file_ext" ] && file_ext=".$file_ext"
[ -n "$new_ext" ] && new_ext=".$new_ext"
new="${new_nam}${new_ext}"

# Rename

echo "Rename file $file to $new ..."
if [ -e "$new" ]
then echo "Not renamed, target file already exists : $new" >&2
else mv "$file" "$new"
fi
done

Thanks so MUCH!
Reply With Quote
  #6 (permalink)  
Old 04-02-04, 06:24
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
How do you execute your script ?

If your script file is named 'dosRename' which must be executable (chmod +x dosRename), execute your script with ($ = shell prompt)

$dosRename file target
__________________
Jean-Pierre.
Reply With Quote
  #7 (permalink)  
Old 04-02-04, 06:50
aigles aigles is offline
Registered User
 
Join Date: Jan 2004
Location: Bordeaux, France
Posts: 319
I have copied and paste the text of the script showed in your previous post. I execute it without problem.

Replace your first line by :
#!/usr/bin -vx

and execute your script.
The shell will print many lines :

-v Prints shell input lines as they are read.
-x Prints executed commands and their arguments.

Analyze the output.
__________________
Jean-Pierre.
Reply With Quote
  #8 (permalink)  
Old 04-02-04, 06:53
zeta_acosta zeta_acosta is offline
Registered User
 
Join Date: Mar 2004
Posts: 8
Talking

Many thanks thats wicked, sorry to be such a whinging newbie!

Thanks
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On