Solved

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

Posted on 2007-03-29
15
4,862 Views
Last Modified: 2012-08-13
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?  
0
Comment
Question by:ToddRod_Taylor
  • 5
  • 5
  • 5
15 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18818189
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18818465
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18818492
0
 

Author Comment

by:ToddRod_Taylor
ID: 18818755
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18818789
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18818899
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
 

Author Comment

by:ToddRod_Taylor
ID: 18819092
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ToddRod_Taylor
ID: 18819153
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18819198
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
 
LVL 30

Accepted Solution

by:
SteveGTR earned 250 total points
ID: 18819204
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
 

Author Comment

by:ToddRod_Taylor
ID: 18819402
sweet


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

Author Comment

by:ToddRod_Taylor
ID: 18819420
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
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18819509
You can only given 500 max for a question. "Accept Multiple Solutions" is the way to go and just split up the points.
0
 
LVL 67

Assisted Solution

by:sirbounty
sirbounty earned 250 total points
ID: 18819910
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
 
LVL 67

Expert Comment

by:sirbounty
ID: 18846160
Thanx.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Our Group Policy work started with Small Business Server in 2000. Microsoft gave us an excellent OU and GPO model in subsequent SBS editions that utilized WMI filters, OU linking, and VBS scripts. These are some of experiences plus our spending a lo…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now