Solved find and replace on txt file

Amd Athlon ii x2 dual core processor
November 28, 2013 at 13:25:04
Specs: Windows XP
hi
can anyone help me

i need a bat file that open a txt searches and replace a word or phrase with an other word or phrase
like the replace all of the office
and save it again

message edited by lklo330


See More: find and replace on txt file

Report •


#1
November 28, 2013 at 21:23:45
Ok, but what about...: case sensitive, or no? Line-split possibility, or no? Is BATch a requirement, or can vbscript (which is also native MS) permitted? Vbscript can handle case, and with some attention, line-breaks. Batch could do a rough job, but I wouldn't stake my reputation on it due to the aforementioned reservations. Other options might be even easier, such as powershell.

Report •

#2
November 29, 2013 at 04:03:32
i dont need case sensitvity nor line split
i need allthought the empty lines remane as is

i allready made this but it doesnt copy empty lines and make the only the half of the txt

tx you

@echo off
if exist newfile.txt del newfile.txt
for /f "tokens=* delims=" %%a in (orig_2.txt) do call :Change "%%a"
del orig_2.txt
rename newfile.txt orig_2.txt
exit /b

:Change
set Text=%~1
if "%Text%"=="server" (
(echo server=pr)>> newfile.txt
) else (
(echo %Text%)>> newfile.txt
)
exit /b

message edited by lklo330


Report •

#3
November 29, 2013 at 20:06:43
✔ Best Answer
Ok, here's two options: bat and vbscript.
::===== begin batchscript: inputfile is test2.txt, outputfile is test3.txt, repl.strings are args 1,2
@echo off & setlocal enabledelayedexpansion
set hi=%1
set bye=%2
(for /f "tokens=2 delims=[]" %%a in ('find /v /n "" ^<test2.txt') do (
set z=%%a
set z=!z:%hi%=%bye%!
echo !z!
))>test3.txt
::========= end batch

'========= begin vbscript REPLAC.VBS (preferred method)
set fso=createobject("scripting.filesystemobject")
******* NO ERROR HANDLING IN PLACE! FILE IS OVERWRITTEN REGARDLESS ETC!
z=replace(fso.opentextfile(wscript.arguments(0)).readall,wscript.arguments(1),wscript.arguments(2))
fso.createtextfile(wscript.arguments(0)).write z
'======= end
to use from batch or commandline:
CSCRIPT REPLAC.VBS filename string1 string2
if strings contain spaces, enclose them in quotes. If strings contain quotes, tinker and good luck!


Report •

Related Solutions

#4
November 30, 2013 at 11:06:35
thank you i used vbscript works great

if i want after the line 100 to change the same word with a thrid one is it possible??


never mind i manage do doit with split and append of the txts

tx anyway

message edited by lklo330


Report •

Ask Question