1.

Solve : Edit .ini file - find and replace?

Answer»

Hi all. I am new in writing bat scripts. I need something that will find a STRING in a specific .ini file and replace it with something else.
More specific I have an ini file named DataFile.ini if I edit it with notepad I can see a string “Q4_1_2007”. I need a script that will edit this file and replace the “Q4_1_2007” with “Q1_1_2008”
Thanks
PS SORRY for my English. I wish you a Happy New Year.

This might help:

Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%i in (DataFile.ini) do (
set input=%%i
set input=!input:Q4_1_2007=Q1_1_2008!
echo !input! >> DataFile.ini.chg
)
ren DataFile.ini DataFile.ini.old
ren DataFile.ini.chg DataFile.ini

Note: The ren commands are used to re-configure the file name. Batch files cannot update text files in place.

Thank you very much. It works perfect. I added 2 lines to your code in order to catch the Q3_1_2007 and Q2_1_2007 text strings.
Thanks again and a happy new year.

@echo off
setlocal enabledelayedexpansion
for /f "tokens=* delims=" %%i in (DataFile.ini) do (
set input=%%i
set input=!input:Q4_1_2007=Q1_1_2008!
set input=!input:Q3_1_2007=Q1_1_2008!
set input=!input:Q2_1_2007=Q1_1_2008!
echo !input! >> DataFile.ini.chg
)
ren DataFile.ini DataFile.ini.old
ren DataFile.ini.chg DataFile.ini

Hi All,
This code works fine for smaller files say 1MB. But my problem is, everyday i RECEIVE a file of greater than 50MB and i have to replace \t (tab spacing) with single space. It was working fine for 1.2MB and stops. So please help me to replace the WHOLE 50MB file.

Thanks in Advance....

Wish u happy new year guys...have fun

chinnaPlease don't spam other PEOPLES posts. It's better to start your own, if only so it gets the attention it deserves.

The tab (character 9) character is difficult to enter into an editor or the command line because it will actually be carried out! This little snippet will process one line at a time keeping memory use low. It's written using the file names used by the original poster.

Code: [Select]Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

Set fso = CreateObject("Scripting.FileSystemObject")
Set filein = fso.OpenTextFile("c:\DataFile.ini", ForReading, TriStateUseDefault)
Set fileout = fso.OpenTextFile("c:\DataFile.ini.chg", ForWriting, TriStateUseDefault)
Do Until filein.AtEndOfStream
strLine = filein.ReadLine
strLine = Replace(strLine, vbTab, " ", 1, -1)
fileout.WriteLine strLine
Loop
filein.Close
fileout.Close

Set f = fso.GetFile("c:\DataFile.ini")
f.Name = "DataFile.ini.old"
Set f = fso.GetFile("c:\DataFile.ini.chg")
f.Name = "DataFile.ini"

Save the script with a vbs extension and run from the command line as cscript scriptname.vbs

Note: your post was a bit ambiguous. This script will change every tab occurrence with a space.



Discussion

No Comment Found