Pio
Pio

Reputation: 533

Batch get last char of %%A

I want to create a variable in my batch file that contains the path to the "newest" folder on my share folder.
Each folder on that share folder has a version number at the end i.e.: "product_1.0.0.1".

My approach was to iterate though the parent folder and check the last chars of the subdirectories for which number is the highest.

set latest=\\Share\prod\1.0\tools\prod_1.0.0.0
for /d %%A in ("\\Share\prod\1.0\tools") do 
  if %%A:~1 gtr %latest:~1% 
    (set latest=%%A)

This does not work since it does not seem to get the last char %%A

Another problem that appears on my mind on writing this question is how do i handle numbers greater than 9?
As I understand :~1 gets the last char, so a better approach may be to get everything behind the last dot. How would i do that?

Upvotes: 1

Views: 956

Answers (1)

DodgyCodeException
DodgyCodeException

Reputation: 6123

You can use dir /ad /oe /b "\\Share\prod\1.0\tools". The option /ad means "list only directories (folders)", the option /oe means "Order by extension", and /b means "Bare output" (just the names). The extension is everything after the last dot. You can then put that in a loop and set latest to each result, and the variable will get the last result. Or use the option /o-e ("Order by extension in reverse order") and then break out of the loop after the first iteration:

set latest=
for /f "delims=" %%a in ('dir /ad /o-e /b "\\Share\prod\1.0\tools"') do (
    set latest=%%a
    goto exit1
)
:exit1

The problem with the above is what to do when the numbers go beyond 9. You can work around that by naming them product_1.0.0.001, product_1.0.0.002, etc.

Upvotes: 1

Related Questions