Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Append string to a file in DOS shell script

Posted on 2008-11-06
10
Medium Priority
?
3,412 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:jl66
  • 5
  • 4
10 Comments
 

Author Comment

by:jl66
ID: 22900680
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.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 600 total points
ID: 22900769
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
0
 

Author Comment

by:jl66
ID: 22901873
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".
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:jl66
ID: 22901879
Are there the other commands to do the same thing as ECHO?
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 600 total points
ID: 22902653
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
0
 

Author Comment

by:jl66
ID: 22913308
Steve,
Yes. That was a typo. I am testing something.
0
 

Author Comment

by:jl66
ID: 22935776
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?
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 600 total points
ID: 22937767
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
0
 
LVL 1

Accepted Solution

by:
brookslaw earned 300 total points
ID: 22988152
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 23084264
Thanks for selecting an answer, though not so happy with B grades... how did you solve the problem in the end?
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question