选择编号最高的文件 - 批处理文件 [英] Choose Highest Numbered File - Batch File

查看:120
本文介绍了选择编号最高的文件 - 批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录充满.jar文件,逐步命名像这样:

I have a directory full of .jar files, named progressively like so:

 version-1.jar
 version-2.jar
 version-3.jar

我想选择最高编号的文件。有没有真正简单的方法来做到这一点?
因为这样做 \\版本的* .jar 导致一个错误,presumably由于多个文件?

I am trying to select the highest numbered file. Is there any really simple way to do it? Because doing .\version*.jar causes an error, presumably due to the multiple files?

推荐答案

我们需要延迟扩展

setlocal enabledelayedexpansion

对于最大只是一个变量:

Just a variable for the maximum:

set max=0

然后在文件迭代:

Then iterate over the files:

for %%x in (version-*.jar) do (

我们需要的文件名不带扩展

We need the file name without extension

  set "FN=%%~nx"

和删除版本 - 自启动:

  set "FN=!FN:version-=!"

现在 FN 应该只包含的数量,我们可以比较一下:

Now FN should contain just the number and we can compare:

  if !FN! GTR !max! set max=!FN!
)

和我们就大功告成了:

echo highest version: version-%max%.jar

完整的批处理文件:

The complete batch file:

@echo off
setlocal enabledelayedexpansion
set max=0
for %%x in (version-*.jar) do (
  set "FN=%%~nx"
  set "FN=!FN:version-=!"
  if !FN! GTR !max! set max=!FN!
)
echo highest version: version-%max%.jar

这篇关于选择编号最高的文件 - 批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆