0

How would I insert a variable previously established in a batch file into a text file. I have the inserting text into a text file down, i just cant figure out the insertion of a variable.

What I am doing

SET name = "Casey"
ECHO "Hey" + name  > file.txt

The result

"Hey" + name  

What I want

"Hey Casey"

2 Answers 2

1

You should do it like this:

SET name=Casey
ECHO "Hey %name%"  > file.txt

Note that there is no spaces before and after the = in

name=Casey

Sign up to request clarification or add additional context in comments.

3 Comments

did a direct copy paste and it did not work RESULT: "Hey" + "$name"
What operating system are you using? This works great on Ubuntu for me.
Sorry I mismatch bash and batch. I'll still help you to find the answer.
1

Too bad syntax, you need to forget other programming languages, this is Batch.

First you can't use spaces when assing values to variables, this is the way to do it:

SET "name=Casey"

Also you can do this:

SET "name=        Casey"

Second Batch don't have ANY concatenate operator for strings, forget + and &, & is for concatenating commands.

So this is the correct syntax:

SET "name=Casey"
(ECHO Hey %name%)> "file.txt"

Try to use () agrupation operators when Echo a numeric ending string like "MyName 2", to avoid problems in large scripts with Batch redirections.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.