Welcome to MSFN

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.


mf_2

How to merge variables in a batch file?

9 posts in this topic

Hello,

I have the following batch file:

SET %one% = "test"

SET %two% = "program"

Now I want %three% to contain somethign like this:

SomeWordstestprogram

That means that I want to have a string, attach %one% and then %two% to it and then write it into %three%.

How is that possible?

I tried PHP syntax but that didn't work ;)

0

Share this post


Link to post
Share on other sites

Hello,

This worked:

SET three = "SomeWord" + %one% + %two%

However, when I want to see whats in %three%, I don't get anything.

echo %three%

gives me

"echo is turned ON"

or something like that (it's a German Windows, so I had to translate it).

What am I doing wrong?

0

Share this post


Link to post
Share on other sites

i found THIS for you

i never worked with batch commands but looking at some examples, i think you should try like this:

SET one = string1

SET two = string2

SET three = "some other string " + one + " " + two

ECHO %three%

0

Share this post


Link to post
Share on other sites

You've got the wrong syntax for SET.

The correct syntax is: SET variable=string

SET one=test
SET two=program
SET three=SomeWord%one%%two%

echo %one%
echo %two%
echo %three%

Edited by [deXter]
0

Share this post


Link to post
Share on other sites

set three = someword%one%%two%

echo %three%

leave no spaces and exclude "+" between %one% and %two%.

Edit: dexter beat me to it.

Edited by spacesurfer
0

Share this post


Link to post
Share on other sites

Thanks for the link, this finally worked:

SET THREE = SomeWords%one%%two%

SET THREE

The second line is used instead of the echo command.

0

Share this post


Link to post
Share on other sites

thank You very very much 'mormoloc' :thumbup

I was looking for a site like this

Edited by NaDer_GenKO
0

Share this post


Link to post
Share on other sites

Good practice notes:

  • Set your variables local to the running batch session
  • To prevent the inclusion of unwanted whitespace in your variables, enclose them in parentheses (lines 3, 4, 5)

@Echo Off
Setlocal
(Set one=test)
(Set two=program)
(Set three=SomeWords%one%%two%)
Echo/%%three%%=%three%

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.