We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Edit a line through BATCH scripting

Medium Priority
241 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

Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Top Expert 2006
Commented:
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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.