CMD: piping ECHO to SET/ expanding variables in variables

后端 未结 3 673
小蘑菇
小蘑菇 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条回答
  • 2021-01-27 10:28

    You need to enable delayed expansion.

    @echo off
    setlocal enabledelayedexpansion
    
    set string=1234567890abcdef
    set substring_start=12
    set substring_length=3
    
    set substring=!string:~%substring_start%, %substring_length%!
    set command=echo !substring!
    
    !command!
    pause
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-27 10:48

    I copied the entire text below from this answer; I just changed the names of variables and particular examples to match the ones of this question:

    %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 and 3. Let's say y=12 and z=3.


    If you want to use another variables for substring position and lenght, then you must know that the replacement of variables enclosed in percents by their values is parsed from left to right; this mean that: %x:~%y%,%z%% don't give the desired result because it mean: show the value of x:~ variable, followed by y, followed by the value of , variable, etc.

    To solve this problem you must use Delayed Expansion, that is, insert setlocal EnableDelayedExpansion command at beginning, enclose substring variables in percents, and enclose the original variable in exclamation marks:

    setlocal EnableDelayedExpansion
    set x=0123456789ABCDEF
    set y=12
    set z=3
    set var=!x:~%y%,%z%!
    

    You may also use parameters of FOR commands as indexes: for /F "tokens=1,2" %%i in ("%y% %z%") do set var=!x:~%%i,%%j!.

    To get the value of a substring when the index change inside FOR/IF enclose the variable in double percents and precede the command with call. For example, to show a substring at a random y position between 0 and 12 and lenght z:

    if %some% == %test% (
       set /A y=!random! %% 13
       call echo %%x:~!y!,%z%%%
    )
    

    You may also use this method outside parentheses in order to avoid the Delayed Expansion:

    call echo %%x:~%y%,%z%%%
    

    Another way to achieve previous process is using an additional FOR command to change the delayed expansion of the index by an equivalent replaceable parameter, and then use the delayed expansion for the original variable. This method run faster than previous CALL:

    if %some% == %test% (
       set /A y=!random! %% 13
       for %%y in (!y!) do echo !x:~%%y,%z%!
    )
    
    0 讨论(0)
提交回复
热议问题