0

I am trying to make a batch file that will write other files.
This is my file:

@echo off
set x=100
echo %x% >> output.txt

What this does is create a text file called "output.txt".
In this file it will display "100"

This is not what I want it to, I want the file to contain "%x%" literally.

The reason I am trying to do this is because I would like a batch file to create other complex batch files.
I could use copy or move to move another file containing what I want, but I need this to be a single file.

I would really appreciate some help, but it is understandably really difficult to understand.

Thanks Jason

phuclv
  • 37,963
  • 15
  • 156
  • 475
Jason Moore
  • 239
  • 1
  • 2
  • 7
  • I strongly suggest you to change the title of this question to something like "How to write a percent sign in a Batch file". Current title is nonsense. – Aacini Sep 02 '14 at 04:19
  • Possible duplicate of [Ignore percent sign in batch file](https://stackoverflow.com/questions/1907057/ignore-percent-sign-in-batch-file) – phuclv Nov 18 '17 at 06:02

2 Answers2

2

Inside a batch file, double up your percent signs:

@echo off
set x=100
echo %%x%% >> output.txt
indiv
  • 17,306
  • 6
  • 61
  • 82
0

Just set x after you create the file:

@echo off
echo %x% >> output.txt
set x=100

This should work because you don't need to set x that early in the program (at least under those circumstances you don't).

ZaneStudios
  • 43
  • 1
  • 9
  • this may work directly at command line, but not in batch files. – Stephan Nov 18 '17 at 10:23
  • I don't believe you. If `%x%` is not defined, the batchfile executes `echo >> output.txt`, which writes `Echo is off.` to the file. – Stephan Nov 28 '17 at 09:30
  • yes, on command line, `echo %undefinedVariable%` outputs literally `%undefinedVariable%`. You may want to delete or change your answer, before it gets downvoted. – Stephan Nov 28 '17 at 17:47