Search code examples
batch-filevariablescmdtitle

Batch - Using variable inside a variable


Hello, today I was playing with .bat code. I would like to change title with changing variable in itself. The way I would like to use title command is from variable %t%. I know where is the problem - The variable %t% after set /a n=%n% + 1 is outdated and still have value n=0. Is there any way to automatically update %t% without typing again set t=title changing %n%? I Thought there will be some way to "lock" the variable %n% so it get the last possible value. (after typing itself in the %t%)

set /a n=0
set t=title changing %n%
%t%
pause
set /a n=%n% + 1
%t%
pause

sorry for not good EN, THX for any response <3


Solution

  • @echo off
    setlocal EnableDelayedExpansion
    
    set /a n=0
    set "t=title changing ^!n^!"
    %t%
    pause
    set /a n=%n% + 1
    %t%
    pause
    

    For an explanation, look for "Delayed Expansion" in this site...

    PS - You may enter set /a n=%n% + 1 in this way: set /a n=n + 1 or even in this simpler one: set /a n += 1