Solved

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

Posted on 2007-03-29
15
4,867 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

NTFS file system has been developed by Microsoft that is widely used by Windows NT operating system and its advanced versions. It is the mostly used over FAT file system as it provides superior features like reliability, security, storage, efficienc…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

813 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

17 Experts available now in Live!

Get 1:1 Help Now