Solved Find folder with largest number by batch

September 21, 2011 at 14:07:19
Specs: Windows XP
Hi,

I am trying to write a batch file to find the folder that has the largest number in its name.

i.e. I have 3 dummy folders with alphanumeric names in the directory
Prod110000
Prod112000
Prod114000

So it should find "Prod114000" for me. I'm trying to pull just the numbers part of the folder name so I can use GTR to compare - I am having troubles pulling the numbers part out of the batch variable (%%G) though because I can't manipulate it.

I suspect there is also some problem in the "set latestdir=%%G" statement too.

What I (only) have right now is this:
g:
cd G:\xxxxxxxx

for /D %%G in ("Prod*") do if %%G GTR %latestdir:~4,10% set latestdir=%%G

Thanks for the help!


See More: Find folder with largest number by batch

Report •

#1
September 22, 2011 at 00:52:57
✔ Best Answer
Test this. Am not sure what you want to compare the folder name numbers with (or why). The script will extract the latest folder based on the folder name only, not actual creation or written time.

Good luck.

@echo off
cls
setlocal 

cd /d G:\xxxxxxxx

for /f "tokens=*" %%1 in ('dir /b /ad /on prod*') do (
    set latestdir=%%1
)

echo Latest directory name=%latestdir%
echo.
echo Numbers in latest directory name=%latestdir:~4%


Please come back & tell us if your problem is resolved.


Report •

#2
September 22, 2011 at 01:12:14
Could probably optimize by setting "delims=PpRrOoDd".

Report •

#3
September 22, 2011 at 06:39:31
Thanks! I needed to find the folder with the largest number so the batch files copies all the files from that folder into a different directory. That part I know how to do.

My problem is resolved


Report •

Related Solutions

#4
September 22, 2011 at 14:58:58
Thanks for coming back to report your success.


Please come back & tell us if your problem is resolved.


Report •

Ask Question