Overwrite batch variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
prasanna2166
Posts: 3
Joined: 23 Nov 2016 07:54

Overwrite batch variable

#1 Post by prasanna2166 » 23 Nov 2016 08:16

Hi,

Need help in overwriting a variable value inside a loop. Getting same value all the time for below loop.

I'm trying to get the value of App and Web from txt document. Below is the sample.

Code: Select all

@echo on
setlocal EnableDelayedExpansion
set package_check=App,Web
for  %%b in (%package_check%) do (
      for /f "tokens=1* delims=," %%x in (property_check.txt) do  (
      set %%x = %%y
           if %%x == %%b  (
           set %%b=%y% echo %%b
           )
      )
)

property_check.txt
App, App1, App2
Web, Web1, Web2

Expected Output:
App = App1, App2
Web = Web1, Web2

Note: I have added values for App and Web in property_check.txt file.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Overwrite batch variable

#2 Post by Squashman » 23 Nov 2016 08:30

Code: Select all

set %%x = %%y

Spaces to the left of the equals symbol in the SET command become part of the variable name. So your variable name actually has a space at the end of it.
So %var% is not the same as %var %.

Not sure what you are trying to do here.

Code: Select all

set %%b=%y% echo %%b


%y% is not the same as %%y.
Are you trying to echo the value of your dynamic variable name after that?

prasanna2166
Posts: 3
Joined: 23 Nov 2016 07:54

Re: Overwrite batch variable

#3 Post by prasanna2166 » 23 Nov 2016 08:59

Squashman,

Yes, I am trying to get a dynamic variable name. Let me brief it.

I have a property file like below.

Code: Select all

Filename: property_check.txt
App, App1, App2
Web, Web1, Web2
Host, Host1, Host2
path, path1, path2


In scripting path, based on input given i need to get the values corresponding to input.

For eg. if i give App and Web(set package_check=App,Web) as input in scripting, it should fetch the values of App and Web alone from property file as mentioned below.
App=App1,App2
Web=Web1,Web2

In a nutshell, i need to get the values from property file only for the inputs given.

prasanna2166
Posts: 3
Joined: 23 Nov 2016 07:54

Re: Overwrite batch variable

#4 Post by prasanna2166 » 23 Nov 2016 09:07

Squahsman,

In a nutshell, i am trying to fetch the values for the input given in the script.

Say for example, if i give App and Web as input in script, it should look for App and Web in property file and get the second part.

Property file:
App, App1, App2
Web, Web1, Web2

Expected result:
App=App1,App2
Web=Web1,Web2

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Overwrite batch variable

#5 Post by Compo » 23 Nov 2016 10:33

The first For loop gives you the output you were looking for in your original post, the second outputs the values for the saved variables for confirmation purposes.

Code: Select all

@Echo Off
SetLocal EnableDelayedExpansion

Set "package_check=App,Web"

For /F "Tokens=1* Delims=," %%A In (
   'FindStr/BR "%package_check:,=, %," property_check.txt') Do  (Set %%A=%%B
   Echo=%%A = !%%A!)

For %%A In (%package_check%) Do If Defined %%A Echo=!%%A!

Timeout -1

Post Reply