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
Prod114000So 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:\xxxxxxxxfor /D %%G in ("Prod*") do if %%G GTR %latestdir:~4,10% set latestdir=%%G
Thanks for the help!
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.
Could probably optimize by setting "delims=PpRrOoDd".
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
Thanks for coming back to report your success.
Please come back & tell us if your problem is resolved.
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |