-2

I have a folder with some subfolders, like:

C:\Users\User\Desktop\ABC\V1_0_1_win64
C:\Users\User\Desktop\ABC\V1_1_1_win64
C:\Users\User\Desktop\ABC\V1_1_4_win64
C:\Users\User\Desktop\ABC\V1_2_1_win64
C:\Users\User\Desktop\ABC\V1_3_0_win64

I want to open with the .bat file the latest revision, here: V1_3_0_win64. How can I open always the latest revision automatically with the .bat file?

Compo
  • 36,585
  • 5
  • 27
  • 39
Dowinder
  • 3
  • 1
  • So you are basically looking for something similar to this, I guess (skipping the incrementation part): [Incrementing file version numbers with batch file](https://stackoverflow.com/q/17320090). This is also related: [Batch to find highest version-string from amount of lines](https://stackoverflow.com/q/31110044)... – aschipfl Sep 17 '19 at 13:59

1 Answers1

0

Here's an example using the existing comments as a base.

This version additionally uses to ensure that the dir command wildcards actually match single digits.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "BaseDir=%UserProfile%\Desktop\ABC"
PushD "%BaseDir%" 2>NUL&&(Set "LatestVer=")||Exit /B
For /F Delims^=^ EOL^= %%A In (
    '"Dir /B/AD-L/O-N "V?_?_?_win64" 2>NUL|"%__AppDir__%FindStr.exe" /I "V[0-9]_[0-9]_[0-9]_win64""'
)Do If Not Defined LatestVer Set "LatestVer=%%A"&GoTo OpenIt
Echo Latest version directory not found!&"%__AppDir__%Timeout.exe" /T 3 /NoBreak>NUL&Exit /B
:OpenIt
Start "" "%SystemRoot%\Explorer.exe" "%LatestVer%"

The example above assumes by "open" you actually meant in Windows Explorer, so I have included that as a fully qualified command, despite Start "" "%LatestVer%" also being valid. You may need to adjust that command, as well as the path between the = and closing " on line 3, (to suit your chosen base directory source location).


To read the usage information for the commands used, open a Command Prompt window and enter the command name followed by its help option. e.g. echo /?, setlocal /?, set /?, pushd /?, exit /?, for /?, dir /?, findstr /?, if /?, goto /?, timeout /? and start /?.
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Wow thanks. This works perfectly for single-digit version numbers. Is there a way to decide between to two and one-digit numbers at the same time? Like: `V2_10_3_win64` vs `V3_0_0_win64` -> open `V3_0_0_win64` – Dowinder Sep 18 '19 at 08:59
  • Yeah, I have already tried this, but there is a Problem. There are two folders like `V0_0_10_win64` and `V3_0_0_win64' and the script starts with 'V?_?_??_win64' then `V?_?_?_win64` the Program would choose `V0_0_10_win64`. I'm sorry, because yesterday I did not think about the problem with two and one- digits. – Dowinder Sep 18 '19 at 09:19
  • @Dowinder, the code I suggested in my previous comment to allow for more than one digit between the first and second underscore, can easily be replicated to allow for more than one digit between the second and third underscore. Please make an effort yourself, this is not a personal code writing or private tuition service. Also you should be aware that alphabetically, _(by name, `/O-N`)_, `Dir` may see `9` as newer than `10`, _(because `9` is a bigger number than the leading`1`)_, so once you start mixing or increasing the possible number of digits, you may start to see failed results! – Compo Sep 18 '19 at 10:41
  • I know that this is not a code service and now the code works for me, thank you very much! I don't really understand this batch logic. Normally I use it to start a Python script. I usually write in Python and C++ and batch is completely different I think. :) – Dowinder Sep 18 '19 at 11:53
  • @Dowinder, it also appears to me that there may be a better or more robust/efficient way of dealing with your issue too. `V…win64` suggests version numbers of contained executables of a specific program, and you're simply trying to ensure that your end user uses the most recent of those. Each would therefore hold different version numbers or modification date and time stamps, so code which specifically looks for those would be a somewhat better option. If that's the case, a new question providing a better overview of the entire task or actual intent would provide you with a more useful answer. – Compo Sep 18 '19 at 12:03