Solved

Vbscript to search and replace numbers

Posted on 2013-11-20
10
412 Views
Last Modified: 2013-12-02
I need a vbscript which will read a text file at c:\file folder and replace a set of numbers it finds with another set of numbers.  The file is a csv, or a .txt file.  For example i want to replace numbers 6789101112 with 12345678.
0
Comment
Question by:100questions
[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
  • 2
10 Comments
 

Author Comment

by:100questions
ID: 39664630
I tried a script similar to this, however it does not work, it is not replacing any of the numbers
I want replaced.
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Script\file.CSV", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "1223234343434 ", "102343433434334 ")

Set objFile = objFSO.OpenTextFile("C:\Script\file.CSV", ForWriting)
objFile.WriteLine strNewText
objFile.Close
0
 

Author Comment

by:100questions
ID: 39666623
What does blind link comments refer to?
0
 
LVL 16

Expert Comment

by:cantoris
ID: 39667459
Add a debugging line in to display the contents of strText to ensure it's what you expect.  Sometimes there can be file encoding screwing this sort of thing up.

Try adding the Tristate value for Unicode when opening the file if strText looks wrong:
http://msdn.microsoft.com/en-us/library/314cz14s(v=vs.84).aspx

ie
Const ForReading = 1
Const FSO_Unicode = -1
Set objFile = objFSO.OpenTextFile("C:\Script\file.CSV", ForReading, False, FSO_Unicode)
strText = objFile.ReadAll

Also, remove any "On Error Resume Next" you may be using in case an error is being thrown.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Accepted Solution

by:
100questions earned 0 total points
ID: 39682590
Something similar to this worked for me:

Const ForReading = 1

Const ForWriting = 2



Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Scripts\WorkingFiles\FILE.CSV", ForReading)



strText = objFile.ReadAll

objFile.Close


strText = Replace(strText, "19876543210235", "123445")

strText = Replace(strText, "21345678901019", "78901")


Set objFile = objFSO.OpenTextFile("C:\Scripts\WorkingFiles\FILE.CSV", ForWriting)

objFile.WriteLine strText

objFile.Close
0
 
LVL 29

Expert Comment

by:QPR
ID: 39682616
Yes, but rather than write it out in full or paste someone's work I sent the link which was subsequently deleted as a "blind comment".
I didn't know this wasn't allowed (my fault).
0
 

Author Comment

by:100questions
ID: 39683320
I want to give QPR a chance to provide a written script which works, so that i can give QPR the points instead, since we were not aware we could not include blind links.
0
 
LVL 29

Expert Comment

by:QPR
ID: 39683980
That's ok just the close the question :)
I wasn't aware of the policy but am now.
Next time I'll know to provide the important part from the link and attribute the original author. Glad you got it working
0
 

Author Closing Comment

by:100questions
ID: 39689473
It works.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Preface Having the need * to contact many different companies with different infrastructures * do remote maintenance in their network required us to implement a more flexible routing solution. As RAS, PPTP, L2TP and VPN Client connections are no…
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

630 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