3

I am new to the bat scripting, so I'm planning to change a word in a text file called "sampleFile.txt"

Can anyone help me? Thankx :)

Madura Dissanayake
  • 8,309
  • 5
  • 25
  • 34

1 Answers1

4

Use the batch file below like this example, to replace apple with orange in file.txt and write the changes into newfile.txt

changefile.bat "apple" "orange" "file.txt" >"newfile.txt"

Check DOS Batch - Find and Replace:

@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::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
:$source http://www.dostips.com
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.
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331