Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Organising photos into a list (O/T)



On Mar 9, 10:15am, zzapper wrote:
}
} I needed to organise about 60 images into a numerical list.
[...]
} (Just realised, the best way would have been to print them all and then 
} organise them on a table: but that would have taken ages)

If the file names or dates don't already reflect something close to the
ordering you want, so you have to look at the pictures to number them,
then no command-line tool is going to be of much help.  You might be
able to write a function to renumber (by renaming) a bunch of files
when you find that you need to move one of them to an earlier place in
the list, but you still need to type out at least the names of the
file to be moved and the file to move it "ahead" of.

So for example

number-files() {
  # Usage:
  #  number-files
  # or
  #  number-files list of up to 9999 file names here
  emulate -L zsh
  (( $# )) || set -- *
  local -Z 4 n
  for ((n = 1; n <= ARGC; n++)); do
    # Comma inserted here in case file names already begin
    # with numbers.  Change or omit as you prefer.
    mv $argv[n] $n,$argv[n]
  done
}

# See "man zshcontrib" for zmv
autoload -U zmv

move-before() {
  # Usage:
  #  move-before source destination
  # Assumes files of interest resulted from number-files,
  # so all file names have a four-digit prefix, and
  # file names remain unique after stripping the prefix.
  # Works only on files in the current directory.
  emulate -L zsh
  local base=${1#[0-9][0-9][0-9][0-9]}
  local min=${(M)2#[0-9][0-9][0-9][0-9]}
  if [[ $1 > $2 ]]
  then
    local max=${(M)1#[0-9][0-9][0-9][0-9]}
  else
    local max=
  fi
  mv $1 tmp$1 || return 1
  # Quoting is very important in the next line
  if zmv "(<$min-$max>)(*)" '${(l:4::0:)$(($1+1))}$2'
  then
    mv tmp$1 $min$base
  else
    # Error, put the original file back
    mv tmp$1 $1
  fi
}

Note that move-before will allow holes to appear in the numbering
if you move a file from the middle towards the end, but the files
should remain in relative number order as you move them around.
Improve as you prefer.



Messages sorted by: Reverse Date, Date, Thread, Author