1.

Solve : Finding directory with largest name?

Answer»

Hello-
I have a directory that gets updated each day with new directories with sequential names:

myfolder_1
myfolder_2
myfolder_3
...etc

I need to get the name of the folder with the greatest number at the end, so in this sample I would want to get myfolder_3.

There may be other folders in there with different names, but I know that I want "myFolder_*" with * being the greatest number.
Any TIPS on how to pull this off? Thanks!
Mac99miles,

If it's ensured that the directory numbers line up with the creation date than you could easy look for the last created directory like this in ORDER to keep things simple:

set myfolder=
for /f %%a in ('"dir /AD /B /OD myfolder_*"') do set myfolder=%%a
echo.folder with the greatest number is "%myfolder%"



HOWEVER, what you really asking for is this:

@echo off
setlocal ENABLEEXTENSIONS
setlocal ENABLEDELAYEDEXPANSION

set /a max=0
for /f %%a in ('"dir /AD /B myfolder_*"') do (
set folder=%%a
set /a n=!folder:myfolder_=!
if !max!<!n! set /a max=n
)
echo.folder with the greatest number is "myfolder_%max%"


Hope this helps!
I'm having trouble with something similar, the problem with mine is that there are spaces in the directory names. The PROGRAM names its directories with the date and a four digit number that doesn't make any sense.

example: \archive\archive 2006060502 2349\

I figured I could use dir /X but it doesn't output the 8.3 names when used with /B (I don't know why, it just doesn't)

Any thoughts?

(oh btw, it's XP/Server 2003)Looks like the format you are dealing with is:
"archive YYYYyyMMDD hhmm"

whereas:

YYYY - Year 4 digit
yy - Year 2 digit
MM - Month
DD - Day
hh - hour in 24 hour format
mm - minute

Since the folder name always has the same number of digits in your case you can use simple string compare or sort the folder names by name in order to get the one with the latest time stamp

Also redefine the default white-space delimiter of the FOR command to "no delimiter" in order to get the full folder name including white-spaces and use parenthesis around the folder name.

Like this:

set myfolder=
for /f [highlight]"delims="[/highlight] %%a in ('"dir /AD /B [highlight]/ON[/highlight] [highlight]"[/highlight]archive *[highlight]"[/highlight] "') do set myfolder=%%a
echo.folder with the latest timestamp is "%myfolder%"
echo.YYYY = %myfolder:~8,4%
echo.yy = %myfolder:~12,2%
echo.MM = %myfolder:~14,2%
echo.DD = %myfolder:~16,2%
echo.hh = %myfolder:~19,2%
echo.mm = %myfolder:~21,2%

DOS IT HELP?Of course it helps!!

I slightly modified it to fit my paths, used the variable, and it works very slick!!

Thank you for your help once again!



Discussion

No Comment Found