0

I need to replace a string in a text file using batch script. The string is:

these are the url's http://somename http://somename http://somename:1010

now in the above line I need to replace someone with anything so that the output will be

these are the url's http://anything http://anything http://anything:1010

But the text file which contains the below string is present in the server

these are the url's http://somename http://somename http://somename:1010

and the string someone will be not known to me! The goal is whatever appears after http:// it need to be replaced with new string (anything) until it finds the another http:// in the same line. So far I am able to write the script like below which is not working.

setlocal ENABLEDELAYEDEXPANSION
@echo off

@set INFILEPATH=E:\PathofTxtFile\
@set INTEXTFILE=Filetomodify.txt
@set OUTTEXTFILE="E:\PathofTxtFile\Filetomodify.txt"    
@set OLDTEXT=http://*
@set OUTPUTLINE=   
@set NEWTEXT=anything

echo %NEWTEXT%

@IF EXIST "%OUTTEXTFILE%" (
GOTO MODIFYFILE
) ELSE (
GOTO COPYFILE
)

:MODIFYFILE
for /f "tokens=1,* delims=¶" %%A in ( '"findstr /n ^^ %INFILEPATH%%INTEXTFILE%"') do (
SET string=%%A
for /f "delims=: tokens=1,*" %%a in ("!string!") do set "string=%%b"
if  "!string!" == "" (
    echo.>>%OUTTEXTFILE%
) else (
    SET modified=!string:%OLDTEXT%=http://%NEWTEXT%!
    echo !modified! >> %OUTTEXTFILE%
)
)
del %INFILEPATH%%INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%

:COPYFILE
@xcopy "C:\TempPath\FileToModify.txt" "INFILEPATH" /Q /Y

This script doesn't alter any line in the Text file. Please can anyone suggest me how to achieve the above criteria. Thanks in advance.

Basavaraju B K
  • 71
  • 2
  • 11
  • What about invoking [jrepl.bat](http://www.dostips.com/forum/viewtopic.php?f=3&t=6044)? Or a [powershell one-liner](http://stackoverflow.com/questions/17144355/string-replace-file-content-with-powershell)? – wOxxOm Aug 14 '15 at 10:35
  • Remove `@echo off` for debugging, or comment it out! – aschipfl Aug 14 '15 at 13:18
  • I am afraid I don't understand your request. Do you want to replace just _the first_ string in the line? so the result would be `http://anything http://somename http://somename:1010`? – Aacini Aug 14 '15 at 21:06
  • @Aacini No I need to replace all the 3 instances of string (`someone`) in the line... so the result will be `http://anything http://anything http://anything:1010` – Basavaraju B K Aug 15 '15 at 02:52
  • Take a look at [How can you find and replace text in a file using the Windows command-line environment?](http://stackoverflow.com/questions/60034/) There are suggestions which make the task a one-liner. – Mofi Aug 15 '15 at 12:19

0 Answers0