windows - Batch: Return filename with highest integer -


if have directory full of text files such

01.text.sql 02text.sql 3text.sql 

how return file name highest integer e.g. 3.text.sql? can see numbers might not prefixed 0 , might missing . after integer. know have loop through directory this

choose highest numbered file - batch file

however, not take account different file name formats. there way batch script can loop through directory , automatically pull file highest integer or have store file names , compare them each other in separate loop?

currently have returns 3text instead of 3

setlocal enabledelayedexpansion set max=0 %%x in (*.sql) (   set "fn=%%~nx"   set "fn=!fn:*-=!"   if !fn! gtr !max! set max=!fn! ) echo highest script number %max% 

updated loop:

setlocal enabledelayedexpansion set scriptmax=0 %%x in (*.sql) (   set "fn=%%~nx"   set a=1000!fn!   set /a fn=a %% 1000   if !fn! gtr !max! set max=!fn! ) 

set /a a=b interpret in b first non numeric character number, so

set fn=03text.txt set /a a=fn 

will set %a% nicely 3. have additional problem here: numbers prefixed 0 interpreted octal, 08 , 09 illegal. fix this:

set fn=09text.txt set t=1!fn! set /a a=t-100 

will yield 9.

however, work if know how many digits expect, maybe have check leading 0 first (if "%fn:~0,1%" == "0" ...), and/or chop off leading zeroes (set fn=%fn:~1%)

edit: better way:

set fn=09text.txt set a=1000!fn! set /a fn=a %% 1000 

will work 999.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -