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.