Edit a line through BATCH scripting

Posted on 2006-04-04
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

Here 100 and 101 are coming as parameters.

Please give a solution .

Thanks in advance

Question by:KARTHU
    LVL 11

    Accepted Solution

    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.
    LVL 25

    Assisted Solution

    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
    Set Line=%~1
    If /i "%Line%" == "%Search%" (
        Echo %Replace%>>%TempFile%
    ) ELSE (
        Echo %Line%>>%TempFile%
    Exit /B

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now