Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Need to find and replace string in a file using Batch program. It is a text file (but with a different extension simple text and there are blanks in the file). I am a Mainframe programmer and new to Batch programming. I went through some of the sites to figure out how this could be done but in vain. I did follow the steps provided in one of the forums of this site (CHANGE.COM) but it did not work for me. Could you some one provide me a sample code which i could use for this? Would appreciate if it is available with a detailed explanation.

@echo off > newfile & setLocal enableDELAYedexpansion
for /f "tokens=* delims= " %%a in (myfile) do (
set str=%%a
set str=!str:Huey=Dewey!
echo !str!>> newfile
)
=====================================
Helping others achieve escape felicityM2

Eg: My file XXX.TXT (around some 200 chars) . Has a string that looks like this:
AAAAAAA ******** AAAAAA ******** AAAA**************************** XXXXXXXXXXXXX**************** XXXXXXX **************** XXXXXXXXXXXX **************** ************************************** "
There is no change in the length of the string but lot of blanks and "*****" characters. Starting from say 50th character it needs to be changed till the 120th character.
I seriously did not understand ur example code...Possible could u please explain?

The code I posted changes Huey to Dewey.
=====================================
Helping others achieve escape felicityM2

you have provided your example input string, but what kind of output are you looking for? show them.

Here is the actual one:
Input string is:
TST123 ************ THIS IS A TEST ******************** HEADER LINE START ******************* HEADER LINE 1 ********* HEADER LINE 2 ************* TRAILER LINE 1 *************** TRAILER LINE 2 *******OUtput String should be:
TST123 ************ THIS IS A TEST ******************** CHANGE ****************************** FIRST SET OF CHG*************1234567# DONE ******** TRAILER LINE 1 *************** TRAILER LINE 2 ******Both the strings are of the same length. But i need to change the characters starting from a particular position (XX) till a particular postion (YY). Thanks for the help...

It seems to work. Test it some more.
@echo off
setlocal enabledelayedexpansion
set strg1=my string
set strg2=my other string
set file=my file.xyz
set newfile=newfile.xyz
del "%newfile%"
set pos1=24
set pos2=49
::numbers 24 and 49 are arbitrary
::count positions starting from 0
set /a range=%pos2%-%pos1%
for /f "usebackq delims=" %%a in ("!file!") do (
set line=%%a
set strline=!line:~%pos1%,%range%!
echo !strline!>"%temp%\#"
call :findx
)
goto :eof:findx
findstr "%strg1%" "%temp%\#">nul
set change=no
if %errorlevel%==0 call :change
if %change%==no echo !line!>>"!newfile!
goto :eof:change
set change=yes
set line=!line:%strline%=BaAtTcChHh!
set strline=!strline:%strg1%=%strg2%!
set xline=!line:BaAtTcChHh=%strline%!
echo !xline!>>"%newfile%"
goto :eof

It did work. that was just wonderful. Thanks!!!
I am new to batch programming and wrote so many small codes which resulted in CRAZY outputs. PLEASE COPYRIGHT the code :-)
Thanks a ton!!

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |