Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Finding directory with largest name  (Read 5784 times)

0 Members and 1 Guest are viewing this topic.

99miles

  • Guest
Finding directory with largest name
« on: May 02, 2006, 01:52:47 PM »
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!
Mac

DosItHelp



    Intermediate
    Re: Finding directory with largest name
    « Reply #1 on: May 02, 2006, 08:33:52 PM »
    99miles,

    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!  ;)

    kartan

    • Guest
    Re: Finding directory with largest name
    « Reply #2 on: May 03, 2006, 06:19:52 AM »
    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)

    DosItHelp



      Intermediate
      Re: Finding directory with largest name
      « Reply #3 on: May 03, 2006, 07:03:23 PM »
      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?

      kartan

      • Guest
      Re: Finding directory with largest name
      « Reply #4 on: May 04, 2006, 06:29:03 AM »
      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!