Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2003
    Posts
    2

    Unanswered: Limit filenames to 31 chars

    On our unix server we have a destination folder wich gets jpegs from the OPI server system.
    These names are too long for reading on the Mac-clients. Limit is 31 characters.

    How do I make a script wich watches the directory, recognizes the jpegs and limits the filenames to 31 characters while leaving the .jpg extension intact?

    I usually use the c shell on both unix and Mac OS X

    Regards,

    JB
    Last edited by jbveenstra; 11-04-03 at 03:56.

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Here's something to start you off. I don't use csh, sorry!

    maxLength=31
    while :; do
    for fileName in *jpg; do
    ((${#fileName} > maxLength)) && {
    newFileName=$(echo $fileName | sed "s/.*\(.\{$maxLength\}\)/\1/") &&
    ! test -f $newFileName &&
    mv $fileName $newFileName ||
    echo Unable to rename $fileName
    }
    done
    sleep 60
    done
    Last edited by Damian Ibbotson; 11-04-03 at 06:02.

  3. #3
    Join Date
    Nov 2003
    Posts
    2
    Thank you very much. It works very good.

    For testing purposes I've commented out the line

    while :; do

    otherwise I have to restart before the process stops.

    I have also difficulties understanding some commands

    ((${#fileName} > maxLength))

    What is this doing?

    And I don't fully understand the sed command

    sed "s/.*\(.\{$maxLength\}\)/\1/"


    .*\(.\{$maxLength\}\) is the pattern? and \1 the procedure?
    What does \1 do?

    What I want to change is that the name is truncated before .jpg and that a duplicate gets a number added 00, 01, 02 and so on.
    I lowered the variable maxlength to 29 to create space for this number.

    You 've already guessed it by now, I'm a beginner in scripting. But I'm working on it.

    Regards,

    JB

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    ((${#fileName} > maxLength))

    ${#variable} is the character length of 'variable', so this command is testing if the length is greater than maxlength.

    sed "s/.*\(.\{$maxLength\}\)/\1/"

    The above command evaluates to...

    sed 's/.*\(.\{31\}\)/\1'

    What this is doing is matching 0 or more characters ('.' is a wildcard in sed), followed by 31 more characters. The .\{31\} pattern is contained in parentheses \(\) and so the matched pattern is held in a buffer by sed. As it is the first (and in this case, only) pattern in parentheses, the related buffer is buffer 1, referenced by \1. Simply put, it is replacing the whole string with the last 31 characters of that string.

    What I want to change is that the name is truncated before .jpg and that a duplicate gets a number added 00, 01, 02 and so on.
    I lowered the variable maxlength to 29 to create space for this number.

    Hmmm... Something like this...

    maxLength=31
    typeset -Z2 fileCounter=0
    while :; do
    for fileName in *.jpg; do
    ((${#fileName} > maxLength)) && {
    newFileName=$(echo $fileName | sed "s/.*\(.\{$maxLength\}\)/\1/") &&
    while test -f $newFileName; do
    newFileName=$fileCounter$(echo $fileName | sed "s/.*\(.\{$((maxLength - 2))\}\)/\1/")
    fileCounter=$((fileCounter + 1))
    done
    mv $fileName $newFileName
    }
    fileCounter=0
    done
    sleep 60
    done
    Last edited by Damian Ibbotson; 11-04-03 at 10:45.

Posting Permissions

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