Append string to a file in DOS shell script

I tried to append a string to a file with DOS batch script, but it did not do anything.
The command I used is
echo a_string >> %PATH1%\test.txt
The string contains %var1%, =, and a number. How should I do to achieve it?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jl66ConsultantAuthor Commented:
I wanted to append DEST_SIZE=5000 to test.ora, I issued
echo DEST_SIZE=5000 >>D:\test\10test\admin\ca\p\test.ora in a script test.bat. After running it,
the echoed info is as follows:
echo DEST_SIZE=5000 1>>D:\test\10test\admin\ca\p\test.ora

You will see 1 being added in, but nothing was done.
Steve KnightIT ConsultancyCommented:
The 1 is normal... 1> is redirect STDOUT 2> is redirect STDERR.  The 1 won't end up in the output.

Make sure you leave a space after your command and the > as otherwise if it ends in 1 or 2 then the 1/2 will end up combining with the >, .e.g.:

echo test 1>x.txt
type x.txt

results in just "test" but

echo test 1 >x.txt
type x.txt

shows "test 1"

jl66ConsultantAuthor Commented:
Thanks for info.
Even though I saw something like
echo test 1>x.txt
on the screen, I could not see anything in the file. Since I want to append it, I used >> instead of >.
What else can cause this?
I guess the following may cause the problem:
echo %VAR%_YYY=%VAR1!%\%VAR2%\%VAR3%_ZZZ >> %VA1%\%VA2%\%VA3%\test.ora
You see before echoing, it replaces a lot of variables, so the file testora has nothing appended. In Unix shell , there is a command: "eval". It means that it evaluates the variables first and then doing "echo".
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

jl66ConsultantAuthor Commented:
Are there the other commands to do the same thing as ECHO?
Steve KnightIT ConsultancyCommented:
Thats fine.  echo just sends stuff to the screen and as you know > and >> write and append to a file or other device.

Try the echo line first without the redirect and add the redirect filename to another echo so you can check what it is doing, i.e. something like this.  The # are so you can see where your strings start and end:

echo Contents to write #%VAR%_YYY=%VAR1!%\%VAR2%\%VAR3%_ZZZ#
echo File to send to #%VA1%\%VA2%\%VA3%\test.ora#

The %var1!% i presume is a typo pasting here?

jl66ConsultantAuthor Commented:
Yes. That was a typo. I am testing something.
jl66ConsultantAuthor Commented:
For a simple case it always works. For our case, it did not always work. Sometimes I put SLEEP 5 and it works. Do you have any ideas?
Steve KnightIT ConsultancyCommented:
It should always work of course, or at least in a controlled manner.  If you would like to post the script having issues I can look.  It could be any number of things.

Does the account running this have R/W access to the test.ora file?
Does some other process have the test.ora file locked at the time you run this batch file, e.g the Oracle client itself?

It looks, from your typo like you are playing with Delayed Expansion.  That should only be a factor if this is occurring inside of a loop, i.e. you are calling these variables within a FOR loop and you are setting the variables in the same loop.  If that is a factor then you need to do two things:
1. Enable Delayed Expansion - add the following at the top of your batch file
2. Drop the % and use ! for the variables you want expanded immediately.

Alternatively, you could call a label and pass the variables.

As Steve mentioned, best bet is probably to post the file.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
Thanks for selecting an answer, though not so happy with B grades... how did you solve the problem in the end?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.