将字符串分为数组,将最后一个数字转换为int并减去,将原始字符串替换为批处理文件中的新INT字符串
我有一个批处理文件,该文件的参数看起来像这样:7.0.5或10.34.7.2
我想拿起字符串的最后一位数字,从中减去1,然后重新保存原始字符串,将最后一个数字替换为新的。这是我到目前为止所拥有的:
@echo off
setlocal enabledelayedexpansion
set tag=%1
echo %tag%
for %%a in ("%tag:.=" "%") do set "output=%%~a"
echo last number: %output%
set /a count=0
for /f "tokens=1-3 delims=." %%a in ("%tag%") do (
set /a count+=1
set "numbers[!count!]=%%a"
echo numbers[a]: %%a
)
for /l %%a in (1,1,3) do echo %numbers[%%a]%
set /a lastNum=%output%
echo lastNum: %lastNum%
set /a prevNum=lastNum-1
echo prevNum: %prevNum%
显然这不起作用。循环的第二个仅将仅打印第一个数字,当我到达第三个循环时,它只会打印echo关闭。
,我什至没有替换字符串。但是,如果我可以填充阵列,那么它应该很简单。
I have a batch file that takes an argument that looks like this: 7.0.5 or maybe 10.34.7.2
I want to take the last digit of the string, subtract 1 from it, then re-save the original string replacing the last number with the new one. Here is what I have so far:
@echo off
setlocal enabledelayedexpansion
set tag=%1
echo %tag%
for %%a in ("%tag:.=" "%") do set "output=%%~a"
echo last number: %output%
set /a count=0
for /f "tokens=1-3 delims=." %%a in ("%tag%") do (
set /a count+=1
set "numbers[!count!]=%%a"
echo numbers[a]: %%a
)
for /l %%a in (1,1,3) do echo %numbers[%%a]%
set /a lastNum=%output%
echo lastNum: %lastNum%
set /a prevNum=lastNum-1
echo prevNum: %prevNum%
This doesn't work, obviously. The second for loop will only print the first digit and when I get to the third for loop, it only prints ECHO is off.
And I haven't even gotten to replacing the string. But if I can get the array populated, then it should be simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像对待文件名一样对待它,变得琐碎。然后,您的最后一个令牌将是扩展名,可用于
%% 〜xa
,以前的零件将是“文件名”,可用于%% 〜na
:Treat it like a file name and it becomes trivial. Your last token would then be the extension, available as
%%~xa
, the part before would be the "file name", available as%%~na
:这起作用:
如果要理解魔术的位置,请删除
@echo Off
行,并仔细查看执行的代码...This works:
If you want to comprehend where the magic is, remove the
@echo off
line and carefully review the executed code...