0

I need your help please. I have a problem with concatenate.

SET /A "index=1"

for /f "skip=%index%" %%G IN (anoiarseeds.txt)   DO if not defined var%index% set "var%index%=%%G"

This code is working but the problem in the next step how can I echo the value of var%index% ?

%var%%index% doesn't work.

!%var%%index%%! doesn't work either.

2

You essentially have two options.

The first one requires the use of delayed variable expansion.

@echo off
setlocal enabledelayedexpansion
SET /A "index=1"
for /f "skip=%index%" %%G IN (anoiarseeds.txt) DO if not defined var%index% set "var%index%=%%G"
echo !var%index%!

The second option is getting two phases of expansion using the CALL command.

@echo off
SET /A "index=1"
for /f "skip=%index%" %%G IN (anoiarseeds.txt) DO if not defined var%index% set "var%index%=%%G"
call echo %%var%index%%%
  • Didn't know about the second option with a call, thanks. There is one caveat with it though: try changing the name of the loop variable from %%G to %%v and see what happens. I guess it was not supposed to be used like this, the parser fails if the first letter of the variable to be expanded is the same as the loop variable. – wombatonfire May 12 '19 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.