Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate string and number in batch file

How to concatenate number and string in for loop? I've tried like this but it doesn't work:

SET PATH=C:\file
FOR /L %%x IN (1,1,5) DO (
    SET "NUM=0%%x"
    SET VAR=%PATH%%NUM%
    ECHO %VAR%
)
like image 702
la lluvia Avatar asked Dec 25 '22 14:12

la lluvia


1 Answers

Modify your code like this:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET PATH=C:\file
FOR /L %%x IN (1,1,5) DO (
    SET "NUM=0%%x"
    SET VAR=%PATH%!NUM!
    ECHO !VAR!
)

You always have to use SETLOCAL ENABLEDELAYEDEXPANSION and !...! instead of %...% when working with variables which are modified inside a loop.

like image 186
MichaelS Avatar answered Jan 09 '23 13:01

MichaelS