MS-DOS/Windows script to search and replace a string

I have 1 text file that is always less that 100 lines long.  I want to search for a string, "DIALER=XYZ", and replace it with "DIALER=123".  It is always just 1 line within the file, but can appear on different line numbers.

Restrictions: WinXP batch only, no 3rd party prgs.

While with a little change, the script on the following link works, it is terribly S-L-O-W
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_20428851.html

Is there any way to make this or another script faster/better?  
ToddRod_TaylorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveGTRCommented:
At 100 lines the process at the link you specified shouldn't be so slow. I'd suggest the same thing. Maybe sirbounty can do this quicker with VBS.
0
sirbountyCommented:
Here's a vbscript to do it...

Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
OldFile="C:\OldFile.txt"
NewFile="C:\NewFile.txt"
OldText="DIALER=XYZ"
NewText="DIALER=123
Dim objFile: Set objFile = objFSO.OpenTextFile(OldFile)
strData = objFile.ReadAll
objFile.Close
Set objFile = objFSO.CreateTextFile(NewFile)
objFile.Write Replace(strData, OldText, NewText)
objFile.Close
Set objFile = Nothing
set objFSO=Nothing
0
SteveGTRCommented:
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

ToddRod_TaylorAuthor Commented:
To date, I have 100 computers spread out in 4 states that I need this to "flip" the switch in the text file between using a VPN connection or a dialup connection.

While the VBS would work great, I don't have easy access (and therefore hard to install any programs) to all the systems that need to do this.  It is easy for me to email a batch file to them though:-)
Here is the batch file as I have it now - in case you see something I messed up:

@echo off

REM Switch dialer from VPN to Home Office

if exist PDIMSG32.NEW del PDIMSG32.NEW

for /F "delims=" %%i in (PDIMSG32.PMC) do (
set LineOut=
set LineIn=%%i
call :PROCESSLINE
)

goto EXIT

:PROCESSLINE

if "%LineIn%"=="" goto WRITEIT
if "%LineIn:~0,12%"=="DUNEntry=VPN" goto FOUNDIT

set LineOut=%LineOut%%LineIn:~0,1%
set LineIn=%LineIn:~1%

goto PROCESSLINE

:FOUNDIT

set LineOut=%LineOut%DUNEntry=Home Office
set LineIn=%LineIn:~20%

goto PROCESSLINE

:WRITEIT

echo %LineOut%>>PDIMSG32.NEW

set LineOut=
set LineIn=

:EXIT

if exist PDIMSG32.NEW copy PDIMSG32.PMC PDIMSG32.OLD
if exist PDIMSG32.NEW copy PDIMSG32.NEW PDIMSG32.PMC
0
sirbountyCommented:
Same would apply for the script - you may need to simply rename it when you send it (update.txt instead of update.vbs) and ask them to rename it once they get it.  Then all they need to is save to their desktop and double-click it...no 'software' needed...windows clients support vbscripting natively...
0
SteveGTRCommented:
The only potential problem and I don't think it will be problem because the for strips blank lines is if the first line is blank:

:PROCESSLINE

if "%LineIn%"=="" goto WRITEIT

...

:WRITEIT

echo %LineOut%>>PDIMSG32.NEW

%LineOut% will be empty and will generate "echo is off" as the output.
0
ToddRod_TaylorAuthor Commented:
sirbounty:

I did not know that VBS was supported...cool

Can it be "kicked off" like a batch file?  I have a specialized shell running that runs DOS batches - no desktop access. I don't allow access - don't want people using the system for personal purposes :-)
0
ToddRod_TaylorAuthor Commented:
SteveGTR:
part of my problem I see if the last 2 lines I added to your work:

if exist PDIMSG32.NEW copy PDIMSG32.PMC PDIMSG32.OLD
if exist PDIMSG32.NEW copy PDIMSG32.NEW PDIMSG32.PMC

how can I accomplish the rename one time at the end of processing?
0
sirbountyCommented:
You can kick it off by
1) double-click it (guess that's out)
2) pass it through wscript:
    wscript filename.vbs
3) pass it through cscript
    cscript filename.vbs
0
SteveGTRCommented:
You should move those lines after the for:

@echo off

REM Switch dialer from VPN to Home Office

if exist PDIMSG32.NEW del PDIMSG32.NEW

for /F "delims=" %%i in (PDIMSG32.PMC) do (
set LineOut=
set LineIn=%%i
call :PROCESSLINE
)

if exist PDIMSG32.NEW copy PDIMSG32.PMC PDIMSG32.OLD
if exist PDIMSG32.NEW copy PDIMSG32.NEW PDIMSG32.PMC

goto EXIT
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ToddRod_TaylorAuthor Commented:
sweet


ok; how to I get 500 point to each of you?  just pick "Accept Multiple Solutions" ?
0
ToddRod_TaylorAuthor Commented:
sirbounty:

is there a way with that VBS to look at the "DIALER=" line and read what it currently is, then change it to the other?  I only have 2 options;

If "DIALER=XYZ" THEN change to "DIALER=123"
if "DIALER=123" THEN change to "DIALER=XYZ"
0
SteveGTRCommented:
You can only given 500 max for a question. "Accept Multiple Solutions" is the way to go and just split up the points.
0
sirbountyCommented:
Maybe...something like this?

Dim objFSO: Set objFSO = CreateObject("Scripting.FileSystemObject")
OldFile="C:\OldFile.txt"
NewFile="C:\NewFile.txt"
OldText="DIALER=XYZ"
NewText="DIALER=123
Dim objFile: Set objFile = objFSO.OpenTextFile(OldFile)
strData = objFile.ReadAll
objFile.Close
Set objFile = objFSO.CreateTextFile(NewFile)
If instr(strData,oldText) > 0 Then
  objFile.Write Replace(strData, OldText, NewText)
else
  objFile.Write Replace(strData, NewText, OldText)
End If
objFile.Close
Set objFile = Nothing
set objFSO=Nothing
0
sirbountyCommented:
Thanx.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.