Results 1 to 8 of 8
  1. #1
    Join Date
    Mar 2004
    Posts
    8

    Exclamation Unanswered: MS-DOS Style Rename

    Deleted
    Last edited by zeta_acosta; 04-02-04 at 08:04.

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

  3. #3
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

  4. #4
    Join Date
    Mar 2004
    Posts
    8

    Smile

    Thank you Jean Pierre your a dude!

  5. #5
    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!

  6. #6
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

  7. #7
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    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.

  8. #8
    Join Date
    Mar 2004
    Posts
    8

    Talking

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

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •