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.
CodePudding user response:
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...
CodePudding user response:
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