We have a batch file called SORTN over on Dostips.com that helps with numeric sorting. I am posting that code here for posterity.
@ECHO OFF
if "%~1"=="/?" (
echo.Sorts text by handling first number in line as number not text
echo.
echo.%~n0 [n]
echo.
echo. n Specifies the character number, n, to
echo. begin each comparison. 3 indicates that
echo. each comparison should begin at the 3rd
echo. character in each line. Lines with fewer
echo. than n characters collate before other lines.
echo. By default comparisons start at the first
echo. character in each line.
echo.
echo.Description:
echo. 'abc10def3' is bigger than 'abc9def4' because
echo. first number in first string is 10
echo. first number in second string is 9
echo. whereas normal text compare returns
echo. 'abc10def3' smaller than 'abc9def4'
echo.
echo.Example:
echo. To sort a directory pipe the output of the dir
echo. command into %~n0 like this:
echo. dir /b^|%~n0
echo.
echo.Source: http://www.dostips.com
goto:EOF
)
if "%~1" NEQ "~" (
for /f "tokens=1,* delims=," %%a in ('"%~f0 ~ %*|sort"') do echo.%%b
goto:EOF
)
SETLOCAL ENABLEDELAYEDEXPANSION
set /a n=%~2+0
for /f "tokens=1,* delims=]" %%A in ('"find /n /v """') do (
set f=,%%B
(
set f0=!f:~0,%n%!
set f0=!f0:~1!
rem call call set f=,%%%%f:*%%f0%%=%%%%
set f=,!f:~%n%!
)
for /f "delims=1234567890" %%b in ("!f!") do (
set f1=%%b
set f1=!f1:~1!
call set f=0%%f:*%%b=%%
)
for /f "delims=abcdefghijklmnopqrstuwwxyzABCDEFGHIJKLMNOPQRSTUWWXYZ~`@#$*_-+=:;',.?/\ " %%b in ("!f!") do (
set f2=00000000000000000000%%b
set f2=!f2:~-20!
call set f=%%f:*%%b=%%
)
echo.!f1!!f2!!f!,%%B
rem echo.-!f0!*!f1!*!f2!*!f!*%%a>&2
)
So here is the output when using the DIR command and piping it to SORTN and also the output of the normal DIR command.
C:\BatchFiles\SORTN>dir /ad /b 00* |sortn.bat
0001 - ProjectA
0002 - ProjectB
0004 - ProjectC
00010 - ProjectY
C:\BatchFiles\SORTN>dir /ad /b 00*
0001 - ProjectA
00010 - ProjectY
0002 - ProjectB
0004 - ProjectC
Insert MY DIR code into your FOR /F command.
Updated to Strip the leading zeros.
@echo off
for /f "tokens=1 delims= " %%G in ('dir /b /ad 0* ^|sortn.bat') do set latestdir=%%G
:STRIP0
IF "%latestdir:~0,1%"=="0" (
SET latestdir=%latestdir:~1%
GOTO STRIP0
)
echo %latestdir%
pause
Adding one more option that does not use SORTN.BAT
@echo off
setlocal EnableDelayedExpansion
set "latestdir="
for /D %%j in (0* 1* 2* 3* 4* 5* 6* 7* 8* 9*) do (
FOR /F "tokens=1 delims= " %%G IN ("%%j") DO set num=%%G
set j=0000000!num!
set name[!j:~-8!]=%%~nxj
)
for /F "tokens=2 delims== " %%j in ('set name[') do set latestdir=%%j
:STRIP0
IF "%latestdir:~0,1%"=="0" (
SET latestdir=%latestdir:~1%
GOTO STRIP0
)
echo %latestdir%
pause