Reverse numerotation of files !

  • Thread starter Thread starter Jeremy
  • Start date Start date
J

Jeremy

Hello,

I have many files in one folder. Here is one exemple of file name :
pics - 05/02/2005 001.jpg
....
pics - 05/02/2005 300.jpg

I wish I could reverse the numerotation of the files :
pics - 05/02/2005 001.jpg will become pics - 05/02/2005 300.jpg
....
pics - 05/02/2005 300.jpg will become pics - 05/02/2005 001.jpg

Is it possible ?
I don't really know how to script but I really need to do that!!
Could somebody help me please ?

Thanks a lot.

Jeremy.
 
Hello,

I have many files in one folder. Here is one exemple of file name :
pics - 05/02/2005 001.jpg
...
pics - 05/02/2005 300.jpg

I wish I could reverse the numerotation of the files :
pics - 05/02/2005 001.jpg will become pics - 05/02/2005 300.jpg
...
pics - 05/02/2005 300.jpg will become pics - 05/02/2005 001.jpg

Is it possible ?
I don't really know how to script but I really need to do that!!
Could somebody help me please ?

This demo assumes that all the files match the wildcard pics*.jpg
(where * means any number of characters). It also assumes that
the final numbering characters match a three digit range 001-999.

It should prompt you for the start number (in your example = 1)
and the finish number (in your example=300) and then rename
each file to (Start+Stop)-(file's current number) with lead zeros for
numbers less than 100. So in your example, 001 becomes 300,
300 becomes 001 and so on.

Note that the demo does an intermediate REName to *.JP_
(assumes you have no files with extension .JP_ at present), to
avoid clashing with existing files during the process. At the end
of the run all the *.JP_ files are RENamed back to *.JPG extension.

Try it out on a few copied example files first to see if it does what
you want.

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL
SET /p START="Enter start of file numbering "
SET /p STOP="Enter end of file numbering "
SET /a TOGGLE=%START%+%STOP%
:: Assumes files match pics*.jpg
FOR %%F IN (pics*.jpg) DO CALL :_RENAME "%%F"
ECHO. Renaming all *.JP_ to *.JPG
REN *.JP_ *.JPG
GOTO :EOF

:_RENAME
SET BN=%~n1
SET FirstPart=%BN:~0,-3%
SET LastThree=%BN:~-3%
SET /a LastThree=1000+%TOGGLE%-%LastThree%
SET LastThree=%LastThree:~-3%
ECHO. Renaming %1 to "%FirstPart%%LastThree%.JP_"
REN %1 "%FirstPart%%LastThree%.JP_"

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
 
William Allen said:
in message

This demo assumes that all the files match the wildcard pics*.jpg
(where * means any number of characters). It also assumes that
the final numbering characters match a three digit range 001-999.

It should prompt you for the start number (in your example = 1)
and the finish number (in your example=300) and then rename
each file to (Start+Stop)-(file's current number) with lead zeros for
numbers less than 100. So in your example, 001 becomes 300,
300 becomes 001 and so on.

Note that the demo does an intermediate REName to *.JP_
(assumes you have no files with extension .JP_ at present), to
avoid clashing with existing files during the process. At the end
of the run all the *.JP_ files are RENamed back to *.JPG extension.

Try it out on a few copied example files first to see if it does what
you want.

Instead of renaming in-place, I would think it easier to enumerate them in
reverse order, copying (or moving) them to another folder using an
increasing sequence for the filename, something like the following untested,
incomplete, air-code:

set/a outno = 1000
for /f %%F in ('dir/o-n pics*/jpg') do call:copynext %%F
goto:eof

:copynext
set/a outno += 1
(set outseq=%outno:~1,3%)
(set outname="workingfolder\pics - 2005-05-02 %outseq%")
copy %1 %outname%
goto:eof



Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF

<snip>
 
Hi William

Thanks for your script.
I've been testing it.
It works good, but for numbers 8 and 9, it doesn't work.
Do you know why ?

Thanks
Jeremy.
 
I'd guess it is because the set/a command interprets any number with a
leading zero digit as it if were an octal or base eight number. Base 8
digits include '0' through '7', but do not include '8' or '9'

/Al
 
I see...

So what should I do to make it work ?


Al Dunbar said:
I'd guess it is because the set/a command interprets any number with a
leading zero digit as it if were an octal or base eight number. Base 8
digits include '0' through '7', but do not include '8' or '9'

/Al
 
I see...

So what should I do to make it work ?

Sorry, my fault, I forgot about the octal support in SET /a.
All you have to do is pad any possible lead-zero strings
to force a decimal interpretation. So, in this case, just
change the original line:

SET /a LastThree=1000+%TOGGLE%-%LastThree%
to become the arithmetically equivalent:
SET /a LastThree=2000+%TOGGLE%-1%LastThree%

The resulting Batch demo (for files in the 1 to 999 numbering
range) becomes:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SETLOCAL
SET /p START="Enter start of file numbering (omit lead zeros) "
SET /p STOP="Enter end of file numbering (omit lead zeros) "
SET /a TOGGLE=%START%+%STOP%
:: Assumes files match pics*.jpg
FOR %%F IN (pics*.jpg) DO CALL :_RENAME "%%F"
ECHO. Renaming all *.JP_ to *.JPG
REN *.JP_ *.JPG
GOTO :EOF

:_RENAME
SET BN=%~n1
SET FirstPart=%BN:~0,-3%
SET LastThree=%BN:~-3%
SET /a LastThree=2000+%TOGGLE%-1%LastThree%
SET LastThree=%LastThree:~-3%
ECHO. Renaming %1 to "%FirstPart%%LastThree%.JP_"
REN %1 "%FirstPart%%LastThree%.JP_">NUL

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

** Remember not to enter lead-zero numbers when you are
prompted for the start and stop counts (I've added a reminder
to the SET /p input prompts).
 
in message
....snip
REN %1 "%FirstPart%%LastThree%.JP_">NUL

.... although it doesn't make any difference, you won't need the
redirect to NUL ( >NUL ) - it was left there by accident while I
was testing the revised demo.
 
William,

I tested your new script, but now i only works for the first file and not
for the others...

Sorry to ask you so much !

Thanks
Jeremy.
 
William,

I tested your new script, but now i only works for the first file and not
for the others...

Give me an exact example of a (smallish) list of file names
that it fails for, and also post back the exact script you were
running against those names, and the responses you gave
to the two prompts.
 
Sorry for this nightmare !!!

here is a set of files (actully just text doc renamed in jpg...)
when I was testing I change the " FOR %%F IN (pics*.jpg) "
to "FOR %%F IN (*.jpg) "
Anyway, thanks again.

Jeremy
 
Back
Top