Edit a line through BATCH scripting

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

KARTHUAsked:
Who is Participating?
 
Renato Montenegro RusticiIT SpecialistCommented:
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
 
InteractiveMindCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.