Append string to a file in DOS shell script

jl66
jl66 used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jl66Consultant

Author

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.
Commented:
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"

Steve
jl66Consultant

Author

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".
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

jl66Consultant

Author

Commented:
Are there the other commands to do the same thing as ECHO?
Commented:
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#
pause

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

Steve
jl66Consultant

Author

Commented:
Steve,
Yes. That was a typo. I am testing something.
jl66Consultant

Author

Commented:
Steve,
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?
Commented:
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.

Steve
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
setlocal ENABLEDELAYEDEXPANSION
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.

Commented:
Thanks for selecting an answer, though not so happy with B grades... how did you solve the problem in the end?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial