How to replace one line in a file with DOS shell

Posted on 2011-05-09
Last Modified: 2012-05-11
Have a formatted text file such as blank lines and tabs and spaces. Would like to replace one line with the one from command line. Some lines are over 120 characters.  I tried to loop the file, but the output lost the format. Gurus, please advise. For example,
Start here.  
This is a test.                                                                          The test is good.
I would like to recommand it to the other people.
"Seeing is believing" ....
The end.

Want to change "Seeing is believing ".... to "Seeing is still not believing" ....

How to deal with it?
Question by:jl66
    LVL 95

    Assisted Solution

    by:Lee W, MVP
    Can you explain how you tried to loop through?

    The way I would do this is:

    for /f "tokens=*" in %%a (filename.txt) do (
    if "%%a"=="Seeing is believing " (
       echo "Seeing is still not believing">>newfilename.txt
    ) else (
       echo %%a>>newfilename.txt
    ren filename.txt filename.txt.backup
    ren newfilename.txt filename.txt

    Open in new window

    LVL 9

    Expert Comment

    much better to do it with VBS, or perl, grep, sed, etc.

    LVL 11

    Assisted Solution

    Nothing is built into the basic command line tools to do this. There are some great command-line tools to easily do this though such as F.A.R.T. Everything I could explain to you can be found here:
    LVL 29

    Expert Comment

    by:Randy Downs
    try this -

    Replace - Replace a substring using string substitution
    Description: To replace a substring with another string use the string substitution feature. The example shown here replaces all occurrences "teh" misspellings with "the" in the string variable str.
     set str=teh cat in teh hat
    set str=%str:teh=the%

    LVL 25

    Assisted Solution

    by:Ron M
    findstr . "C:\inputfile.txt" | C:\replacecmd.exe -o "Seeing is believing" -n "Seeing is still not believing" > outputfile.txt
    LVL 29

    Accepted Solution

    This is probably better than my 1st link

    @echo off
    REM -- Prepare the Command Processor --

    ::BatchSubstitude - parses a File line by line and replaces a substring"
    ::syntax: BatchSubstitude.bat OldStr NewStr File
    ::          OldStr [in] - string to be replaced
    ::          NewStr [in] - string to replace with
    ::          File   [in] - file to be parsed
    :$changed 20100115
    if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
    for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
        set "line=%%B"
        if defined line (
            call set "line=echo.%%line:%~1=%~2%%"
            for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
        ) ELSE echo.
    LVL 11

    Expert Comment

    by:Pieter Jordaan

    funny URL.

    You gets lots of re-compiled Linux tools for Windows.
    They don't work as well as on Linux, but can get the job done.

    cygwin is also a nice tool if you want file scripting on Windows.

    The best way to do these things, is to create a Linux file server with samba.
    You can mass-rename files, recursive find and replace, search all files for a word and lots, lots more.

    It is also a good way to find these tools. Just google for the Linux tool that you need, then look for the windows binary using ' for windows' in your search.

    LVL 51

    Assisted Solution

    by:Bill Prew
    Can you provide a real sample input file as an attachment to a post here?

    Also, here is a list of free and/or pay utilities for this type of thing that I have accumulated.

    If you have tabs and blank lines in the file then a batch (BAT) solution will get a bit complicated.  VBS would be a better scripting tool for this, as in:

    ' Define needed constants
    Const ForReading = 1
    Const ForWriting = 2
    Const TriStateUseDefault = -2
    ' Get input file name from command line parm, if 2 parms entered
    ' use second as new output file, else rewrite to input file
    If (WScript.Arguments.Count > 0) Then
      sInfile = WScript.Arguments(0)
      WScript.Echo "No filename specified."
    End If
    If (WScript.Arguments.Count > 1) Then
      sOutfile = WScript.Arguments(1)
      sOutfile = sInfile
    End If
    ' Create file system object
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    ' Read entire input file into a variable and close it
    Set oInfile = oFSO.OpenTextFile(sInfile, ForReading, False, TriStateUseDefault)
    sData = oInfile.ReadAll
    Set oInfile = Nothing
    ' Replace commas (,) with pipes (|)
    sData = Replace(sData, "Seeing is believing", "Seeing is still not believing")
    ' Write file with any changes made
    Set oOutfile = oFSO.OpenTextFile(sOutfile, ForWriting, True)
    Set oOutfile = Nothing
    ' Cleanup and end
    Set oFSO = Nothing

    Open in new window


    Author Comment

    Thank all of you very much for the codes and tips. Since this one has to be written in BAT, i have to use it for specific this task. Everyone shed a great deal of light on it from different angle. I really appreciate it.
    I select Number-1's 2nd input, and revised it a bit and use it for my purpose. As mentioned by Bill, BAT is hard to manage tabs, spaces, etc. However it is resolved with the trick :
    type %3|find /n /v ""
    The drawback is that the rest is hard to modify.
    Everyone deserves a 500 points alone... Thanks a lot.

    Author Closing Comment

    Thanks a kot to everyone.
    LVL 51

    Expert Comment

    by:Bill Prew
    Welcome, thank you.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Are you unable to synchronize your OST (Offline Storage Table) file with Microsoft Exchange Server? Is your OST file exceeding 2 GB size limit? In Microsoft Outlook 2002 and earlier versions, there is a 2 GB size limit for the OST file. If the file …
    Can you find a fax from a vendor you saved a decade ago in seconds? Have you ever cursed your PC under your breath during an audit because you couldn’t find the requested statement or driver history?  If you answered no to the first question or yes …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    758 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

    11 Experts available now in Live!

    Get 1:1 Help Now