That title doesn't explain much but I couldn't summarize it quickly. Let's say I have files like this (all in the same directory)...
abc_foo_file1_morestuff.ext
abc_foo_file2_morestuff.ext
efg_goo_file1_morestuff.ext
jkl_zoo_file0_morestuff.ext
jkl_zoo_file1_morestuff.ext
jkl_zoo_file4_morestuff.ext
xyz_roo_file6_morestuff.ext
And I want them renamed to:
abc-1.ext
abc-2.ext
efg-1.ext
jkl-1.ext
jkl-2.ext
jkl-3.ext
xyz-1.ext
So basically some files in sets (abc, jkl, xyz) got removed and some got renamed to have a zero in them so they'd be listed first. But I want to resequence them to start at 1 and not have any gaps in the sequence.
I tagged this with Python because that's what I've attempted before, but if there's a simpler or cleaner approach, I'm all for it!
My general approach to this problem would be the following steps:
So, in python that would look like:
Here is a pure batch-file solution which regards all of your requirements (see also
rem
comments):The toggling of
EnableDelayedExpansion
andDisableDelayedExpansion
is required to make the script robust for any special characters that might occur in file names, like%
,!
,(
,)
,&
and^
. Typeset /?
into the command prompt to find brief information about delayed variable expansion.This approach relies on the following assumptions:
*_*_file*_*.ext
;*
contain_
characters on its own;file
are decimal numbers with up to 12 digits;_
takes precedence over the index after the wordfile
with respect to the sort order; so for instance, supposing there are two filesabc_AAA_file8_*.ext
andabc_BBB_file5_*.ext
,abc_AAA_file8_*.ext
will be renamed toabc-1.ext
andabc_BBB_file5_*.ext
toabc-2.ext
, becauseAAA
comes beforeBBB
; if this is not the desired behaviour, exchange theecho
command line in the first loop structure by this one:echo !LINI!_!LINN!_!LINL!_!LINR!^|!LINI!_!LINL!_!LINM!_!LINR!
;With the sample files from your question, the temporary file contains the following lines:
You can do this with a batch file: