Jump to content

How to merge variables in a batch file?


Recommended Posts

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 ;)

Link to comment
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?

Link to comment
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%

Link to comment
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]
Link to comment
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%

Link to comment
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.
×
×
  • Create New...