Computing.Net > Forums > Programming > Batch file to replace a word

Batch file to replace a word

Reply to Message Icon

Original Message
Name: bluesdudes
Date: September 21, 2007 at 04:51:51 Pacific
Subject: Batch file to replace a word
OS: Windows XP
CPU/Ram: Pentium D/1GB
Model/Manufacturer: DELL
Comment:

Hi..

I am creating a batch file to replace a word in all the text files in a directory.

File Example:-

FILE 1:- "ISA*00* *00* *ZZ*ABCDEFGHI *ZZ*ABCDEFG "
FILE 2:- "ISA*00* *00* *ZZ*ABCDEFGHI *ZZ*ABCD "

This is the first line in all the files. ( Please note... the spaces are also included between the quotes)
I would like to change the word "ABCDEGF " or "ABCD " to "ABCDEFGHIJKLMNO" using batch file.

EXAMPLE list of filenames will be :-

POOLABCDEFG-n123445.txt
POOLABCD-n135464.txt

The characters in the filename after the word pool.. i.e, "ABCDEFG" and "abcd" and the word that needs to be changed IN EACH FILE are the same.

Can anyone help me?

Vijay


Report Offensive Message For Removal


Response Number 1
Name: dtech10
Date: September 23, 2007 at 14:09:53 Pacific
Subject: Batch file to replace a word
Reply: (edit)

Hi Bluedudes

If "ABCDEFG" and "ABCD" are always followerd by a space, Try something like this.

@echo off
setlocal EnableDelayedExpansion

type nul>File1.tmp
type nul>File2.tmp

for /f "tokens=*" %%a in (File1.txt) do (
set C=%%a
set C=!C:ABCDEFG =ABCDEFGHIJKLMO!
set C=!C:ABCD =ABCDEFGHIJKLMO!
echo !C!>>File1.tmp
)
for /f "tokens=*" %%a in (File2.txt) do (
set C=%%a
set C=!C:ABCDEFG =ABCDEFGHIJKLMO!
set C=!C:ABCD =ABCDEFGHIJKLMO!
echo !C!>>File2.tmp
)

del File1.txt
ren File1.tmp File1.txt
del File2.txt
ren File2.tmp File2.txt



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Batch file to replace a word

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge