Simple 'date' to 001 scheme script

So since I'm looking for an easy way to numberize files in a folder according to date:

Is there an easy script (batch, windows), that will rename files like this:
.earliest creation time: 001.file
older creatiin time : 002.file
even older time : 003.file
....
...
..
.

Any help is appreciated.

te

Dear pasc,

I have a few to questions pose in response first:-

  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)

Most importantly, What have you tried so far?

Assuming that you have less than 1000 files, you could:-

  • Set up a three digit counter padded with leading zeros
  • List the files sort in date order, oldest first (reversed to normal date order) using ls -1rt
  • Use this list as input to a loop to rename each source file in turn to the appropriate numbered file
  • Increment the counter and go round the loop to the end of the list.

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.

We're all here to learn and getting the relevant information will help us all.

Kind regards,
Robin

it is not a homework assignment.

i merly had imagemagick hack an animated gif into mutiple frames,
edited em w imagemagik and tried to compile as vid with ffmpeg,
but...
sadly the order in the video is wrong
(it jumps from frame 1 to 10 etc)

Thats why I want to rename the files to 0001 0002... to prevent jumping.

Show us the script you're using to get 1.file ... 9.file 10.file ... and we'll show you how to simply modify it to produce numbers with a specified number of digits with leading zeros. Note that something like:

printf '%04d.file' $number

is likely to be at the heart of it (for four digit sequence numbers).

This is the code:

for %x in (*gif) do convert %x image_%d.gif

Ordering by date may not be reliable on extremely small time scales, many implementations only store date to the second. Just moving 1-9, 10-99, 100-999 etc to appropriate four-digit numbers with leading zeroes may be easier. This would be very simple in BASH but excruciatingly difficult in Windows CMD, could you use busybox.exe ?

yes, if it simplifies it, I could.

Not sure as this seems to be for DOS - is the %d a format string that could be replaced by e.g. %04d (as opposed to %x which is a loop variable)?

With the shell in busybox you should be able to do something like:

cnt=1
for x in *.gif
do      base="${x%.gif}"
        echo mv "$x" "$(printf '%s_%04d.gif' "$base" $cnt)"
        cnt=$((cnt + 1))
done

If that shows you a set of mv commands that do what you need to do, remove the echo and run it again to actually rename the files.

1 Like

Thanks!

Busybox seems to be rather useful :).

He could likely feed that to imagemagick next time to get files with leading zeroes from the get-go.