Computer Hope

Microsoft => Microsoft DOS => Topic started by: radema on March 02, 2018, 08:02:38 PM

Title: Increment number base on number in same folder
Post by: radema on March 02, 2018, 08:02:38 PM
Filenumber_Groupnumber_filename

I would like to increment filenumber if a duplicate Groupnumber is found. Then restart the increment with other group number.

For example:
If I will save 1_filename it will renamed as 1_1_filename.
If I will save 1_filename it will renamed as 2_1_filename.
If I will save 2_filename it will renamed as 1_2_filename.
If I will save 2_filename it will renamed as 2_2_filename.
....

@echo off
setlocal enabledelayedexpansion

REM this will identify the biggest number
set max=0
for %%x in (*.*) do (
  set "FN=%%~nx"
  set FN=!FN:~2,1!
  if !FN! GTR !max! set max=!FN!
)
rename ....

The code above will only work with only one group per folder. Would it be possible to change the script so that it can identify groupnumber from each other in same folder and then add an increment number differently from each groupnumber.

Title: Re: Increment number base on number in same folder
Post by: Squashman on March 02, 2018, 08:11:06 PM
And the other problem with your code is that it will only handle single digit numbers. Use a FOR /F command instead to break up the file name I to three chunks by using the underscore as a delimiter.
Title: Re: Increment number base on number in same folder
Post by: radema on March 02, 2018, 11:32:19 PM
Thanks for pointing that out.