Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Find and Replace text in Batch File

Original Message
Name: MagnumVP
Date: March 16, 2005 at 18:06:01 Pacific
Subject: Find and Replace text in Batch File
OS: Windows XP/2003
CPU/Ram: 3.0Ghz
Comment:
I need to figure out a method to find and replace text within a text file.

I have a file named, "myfile.txt" that has the following;

testing=1
database=500
rules=30

I want the batch file to find database=500 and insert destination=300 under it.

I want it to look like this;

testing=1
database=500
destination=300
rules=30

I'll be using the NT platform for this. (NT,2000,XP,2003)

Thanks

MagnumVP


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: March 17, 2005 at 00:40:27 Pacific
Subject: Find and Replace text in Batch File
Reply: (edit)
The following batch for NT systems fits your need

For /F "tokens=1,2 delims==" %%A in (%1) Do (
Echo %%A=%%B>> %1.tmp
If "%%A"=="database" (
Echo destination=300>> %1.tmp))
Move %1.tmp %1

I named it Atxt.bat and to run just type

Atxt Pathname/FileName (e.g. Atxt Myfile.txt)


Report Offensive Follow Up For Removal

Response Number 2
Name: MagnumVP
Date: March 17, 2005 at 13:50:20 Pacific
Subject: Find and Replace text in Batch File
Reply: (edit)
That gets me REALLY close. I appologize, I forgot one portion of the file. There is a [configuration] along with it, that seems to be messing with the batch.

Here is what it was;

[configuration]
testing=1
database=500
rules=30

Here is what I get; Is there a way to get the "=" from after the [configuration]?

[configuration]=
testing=1
database=500
destination=300
rules=30

ALSO... Is there a way to assign variables so you don't have to pull it from %1?

Example:

SET Filename=Myfile.txt
For /F "tokens=1,2 delims==" %%A in (%Filename%) Do (
Echo %%A=%%B>> %Filename%.tmp
If "%%A"=="database" (
Echo destination=300>> %Filename%.tmp))
Move %Filename%.tmp %Filename%

Thanks

MagnumVP


Report Offensive Follow Up For Removal

Response Number 3
Name: dtech10
Date: March 17, 2005 at 15:37:40 Pacific
Subject: Find and Replace text in Batch File
Reply: (edit)
Hi Magnum
Doe's this help

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

:AddText %1
set Text=%~1%
echo %Text% >> newfile.txt
if "%Text%"=="database=500" echo desination=300 >> newfile.txt
exit /b


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: Find and Replace text in Batch File

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




CPU and Graphics Upgrade Questions

VIRUS ALERT in Taskbar, HELP!

DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC