?
Solved

Edit a line through BATCH scripting

Posted on 2006-04-04
4
Medium Priority
?
218 Views
Last Modified: 2013-11-13
I am creating a .BAT file.In that I have opened a file(.DAT).
How can I edit a line in that opened file through BATCH SCRIPT.

What command I should use ?

Eg : I have a line in the opened  file as " SERIAL_NUMBER :100"
I want to change the value 100 to 101  as after change the line should looks like
 " SERIAL_NUMBER :101"

Here 100 and 101 are coming as parameters.

Please give a solution .

Thanks in advance

0
Comment
Question by:KARTHU
2 Comments
 
LVL 11

Accepted Solution

by:
Renato Montenegro Rustici earned 252 total points
ID: 16370305
User Windows Scripting.

1) Open a file for read and another for write.
2) Use the replace method to replace the string.
3) Output to the temp file (the write file)
4) Close both files
5) Delete the original file or change its extension (like bak)
6) Rename the temp file to the production name

Your are done.
0
 
LVL 25

Assisted Solution

by:InteractiveMind
InteractiveMind earned 248 total points
ID: 16373116
It can be done in a batch file ...

The process is to simply read in the file one line at a time. If the line matches the search criteria, then output the replacement string to a temporary file — otherwise, if it doesn't match the search criteria, output a copy of the input line to the temporary file.
Then simply replace the input file with the temporary file.


@Echo OFF
Setlocal EnableDelayedExpansion
:# Variables
Set InputFile=filename.dat
Set TempFile=temp.txt
Set Search=SERIAL_NUMBER :100
Set Replace=SERIAL_NUMBER :101
:# Perform the operations
For /F "delims=" %%I In (%InputFile%) Do Call :Process "%%~I"
Del /F %InputFile% >Nul
Ren "%TempFile%" "%InputFile%" >Nul
:# Done.
Exit /B
:Process
Set Line=%~1
If /i "%Line%" == "%Search%" (
    Echo %Replace%>>%TempFile%
) ELSE (
    Echo %Line%>>%TempFile%
)
Exit /B
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
Simple Linear Regression
Six Sigma Control Plans

850 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