将字符串分为数组,将最后一个数字转换为int并减去,将原始字符串替换为批处理文件中的新INT字符串

发布于 2025-02-13 20:49:35 字数 673 浏览 2 评论 0原文

我有一个批处理文件,该文件的参数看起来像这样: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

格子衫的從容 2025-02-20 20:49:35

像对待文件名一样对待它,变得琐碎。然后,您的最后一个令牌将是扩展名,可用于%% 〜xa,以前的零件将是“文件名”,可用于%% 〜na

@echo off
setlocal 
set tag=%~1
echo input: %tag%
set "last=%~x1"           &REM last token ("extension")
set /a last=%last:~1%-1   &REM remove the dot and subtract one
set "prevnum=%~n1.%last%" &REM reassemble "filename"."newExtension"
echo lastNum: %~1         &REM original parameter
echo prevNum: %prevNum%   &REM new calcualted value

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
setlocal 
set tag=%~1
echo input: %tag%
set "last=%~x1"           &REM last token ("extension")
set /a last=%last:~1%-1   &REM remove the dot and subtract one
set "prevnum=%~n1.%last%" &REM reassemble "filename"."newExtension"
echo lastNum: %~1         &REM original parameter
echo prevNum: %prevNum%   &REM new calcualted value
半仙 2025-02-20 20:49:35

这起作用:

@echo off
setlocal EnableDelayedExpansion

set "tag=%1"
echo %tag%

set "last=%tag:.=" & set "out=!out!!last!." & set /A "last=%-1" & set "out=!out!!last!"

echo %out%

如果要理解魔术的位置,请删除@echo Off行,并仔细查看执行的代码...

This works:

@echo off
setlocal EnableDelayedExpansion

set "tag=%1"
echo %tag%

set "last=%tag:.=" & set "out=!out!!last!." & set /A "last=%-1" & set "out=!out!!last!"

echo %out%

If you want to comprehend where the magic is, remove the @echo off line and carefully review the executed code...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文