Link to home
Start Free TrialLog in
Avatar of ToddRod_Taylor
ToddRod_TaylorFlag for United States of America

asked on

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
https://www.experts-exchange.com/questions/20428851/batch-find-string-and-replace.html

Is there any way to make this or another script faster/better?  
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

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.
Avatar of sirbounty
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
Avatar of ToddRod_Taylor

ASKER

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
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...
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.
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 :-)
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?
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
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sweet


ok; how to I get 500 point to each of you?  just pick "Accept Multiple Solutions" ?
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"
You can only given 500 max for a question. "Accept Multiple Solutions" is the way to go and just split up the points.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanx.