Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-03-29
15
Medium Priority
?
4,876 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Turn your laptop into a mobile console!

The CV211 Laptop USB Console Adapter provides a direct Laptop-to-Computer connection for fast and easy remote desktop access with no software to install.

 

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 1000 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 1000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

715 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