ak28
ak28

Reputation: 15

unable to assign output of FOR loop into another variable in a batch file

I have used FOR /F to access the value in the file "last_bkp_date.txt" inside a batch file.

The result of %%a is okay, but i am unable to assign value to v_abc.

I have used set v_abc = before the for loop to especially delete / remove any past assignment done to this variable.

Can somebody help me on this?

@SETLOCAL EnableDelayedExpansion
@echo off

set v_abc =

FOR  /F "tokens=1 usebackq" %%a IN (last_bkp_date.txt) DO (  
  set v_abc =%%a
  echo value of a: %%a
  echo value of abc: !v_abc!
)

Upvotes: 1

Views: 1942

Answers (2)

BDM
BDM

Reputation: 3900

Simple rookie mistake of adding a space after a variable name (set v_abc =%%a).

You can fix it by either

  1. Changing set v_abc =%%a to set v_abc=%%a

    Or

  2. Changing echo Value of abc: !v_abc! to echo Value of abc: !v_abc !.

Simple.

Upvotes: 2

PA.
PA.

Reputation: 29339

The syntax is SET variable=value. Your problem is the space between the variable name and the = sign.

Just try this

set v_abc=%%a

and voilà!

The reason for this is that in Windows environment variable names can contain spaces. But it is not recommended, so as an alternative solution to your problem you could access the variable including the space, this way

echo value of abc: !v_abc !

Upvotes: 2

Related Questions