Print

Print


#! /usr/bin/env python

import os
import sys

try:
  start = int(sys.argv[1])
  total = int(sys.argv[2])
  correction = int(sys.argv[3])
  template = sys.argv[4]
except (IndexError, ValueError):
  command_name = os.path.basename(sys.argv[0])
  print "usage: %s start total correction template" % command_name
  sys.exit()

for i in xrange(start, start + total):
  oldname = template % i
  newname = template % (i + correction)
  sys.stderr.write('Renaming "%s" to "%s".\n' % (oldname, newname)) 
  os.rename(oldname, newname)




Name it "renumberer" and call it like this:

    ./renumberer 1081 360 -720 CD267A_3_pk_1_%04d.img


Now you can do this any time you want without editing a script. See common unix formatting for the part after the "%" in the template. You can follow the example above and modify the program to accept a template both for the old file and the new file to increase the generality.

The downside is that you can't use it to show off the fact that you can do awk.


James



On Dec 9, 2010, at 5:51 PM, wu donghui wrote:

> Dear all,
> 
> I need a script to renumber my image. My initial image number ranges from *_1081.img to *_1440.img. There are 360 images in total. I want to renumber these images with the ranges from *_361.img to *_720.img, that means every initial image-720, but I don't know how to do it. Below is my script draft.
> 
> 
> 
> #! /bin/csh -f
> 
> @ i = 1081
> 
> while ( $i >= 1081 ) 
> while ($i <= 1440 )
> echo mv CD267A_3_pk_1_$i.img CD267A_3_pk_1_$i-720.img
>   @ i++
> end
> 
> exit
> 
> 
> 
> Can anyone help me for this script? Thank you very much.
> 
> 
> Best regards,
> 
> Donghui