CMD: piping ECHO to SET/ expanding variables in variables

后端 未结 3 679
小蘑菇
小蘑菇 2021-01-27 09:58

%x:~12,3% Returns 3 characters starting at the 12:th character in x variable. What I have been trying to accomplish is using variables instead of 12 an

3条回答
  •  猫巷女王i
    2021-01-27 10:34

    @ECHO OFF
    SETLOCAL
    
    SET "x=abcdefghijklmmopqrstuvwxyz"
    
    SET /a start=12
    SET /a length=3
    
    CALL SET "var=%%x:~%start%,%length%%%"
    ECHO var=%var%
    CALL echo %%x:~%start%,%length%%%
    
    SETLOCAL ENABLEDELAYEDEXPANSION
    SET /a start=6
    SET /a length=4
    
    SET "var=!x:~%start%,%length%!"
    ECHO var=%var%
    echo !x:~%start%,%length%!
    
    GOTO :EOF
    

    Two methods - the first in standard mode and the second using delayedexpansion. There are hundreds of examples on SO about delayedexpansion.

提交回复
热议问题