?
Solved

VB Code to Replace Text with Certain Characters

Posted on 2014-11-14
19
Medium Priority
?
158 Views
Last Modified: 2014-11-21
I would appreciate it if someone help me with the VB code (very limited knowledge of VB).  I have a file which has many lines and each line has information as follows:

xxxxxxx|xxxx|AAAAA|BBBB|


I would like to

1. Change the first pipe sign (|) into hypen (-)
2. Remove the last pipe (|) of each line
3. Change the second and third pipe (|) into semi-colon

Thank you.
0
Comment
Question by:yimsoin57
[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
19 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40442154
Which version of VB are you using?
0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40442155
You can try this code:
Dim OriginalString As String = "xxxxxxx|xxxx|AAAAA|BBBB|"
Dim ReplacedString As String = Replace(Replace(Replace(OriginalString, "|", "-",1,1), "|", ";",1,2), "|", "")

Open in new window

0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40442156
Here is the code:

dim txt 
 txt="xxxxxxx|xxxx|AAAAA|BBBB|"
WScript.Echo txt

txt=Replace(txt,"|","-",1,1)
txt=Replace(txt,"|",";",1,2)
txt=Replace(txt,"|","",1,-1)

WScript.Echo txt

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:yimsoin57
ID: 40442277
If the file name "test" is located in the c:\database.  How can I make the system knows that this is a file to convert.  Thank you.
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40442280
So , you have file with other text and you need to extract this string, do replace and then what ?

As you posted, I undestood you need the replace string... will take a look, just confirm the steps...
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40442478
Hello,

here is the code which will look at your folder and take the file "test" check if file exist and then replace the string and create new file ...

dim folder, input, output, fsObject, fObject_in,fObject_out, line

folder="c:\database"
input ="test.txt"
output ="out.txt"

Set fsObject=CreateObject("Scripting.FileSystemObject")
if fsObject.FileExists(folder & input)=false then
   wscript.echo "File not present"
   wscript.Quit
end if

Set fObject_in = fsObject.OpenTextFile(folder & input, 1)
set fObject_out = fsObject.CreateTextFile(folder & output, TRUE)

Do Until fObject_in.AtEndOfStream
line= fObject_in.ReadLine

line=Replace(line,"|","-",1,1)
line=Replace(line,"|",";",1,2)
line=Replace(line,"|","",1,-1)
 
 fObject_out.WriteLine line
Loop

fObject_in.Close
fObject_out.Close

Open in new window

0
 

Author Comment

by:yimsoin57
ID: 40448907
How can I run the code?  Thanks.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 40448919
You will need to save that as a VBS file, say EE28561925.vbs, adjust the folder / file names near the top if needed, and then at a DOS command line (or in a BAT file) do the following:

cscript //nologo EE28561925.vbs

~bp
0
 

Author Comment

by:yimsoin57
ID: 40451559
I got a  message "File Not Present" after running the vbs file.  Please help.  Thank you.
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40451714
Hello,

can you run the file attached, but before you run it you need to change few lines i.e.

folder="c:\database" you need to put the exact location of the folder containing the files in which you want to replace the string

input ="test.txt" in this line you need to put name of your file


output ="out.txt" in this line you need to put name of RESULT file i.e. file where the replaced content will be saved.

then save the edited file to some location for example c:\test\replace.vbs then open command prompt and change directory to c:\test and then just write replace.vbs and if any errors post it...


I attached how the command prompt shoul look like, and since i do not have folder C.\database i got the same error...
replace2.vbs
Capture.PNG
0
 

Author Comment

by:yimsoin57
ID: 40451968
Hello:

I have done as advised but "File no Present" still displays.  Attached are screenshot.  Sorry it takes a lot of your time.  How can I increase the points.
database-folder.png
file-not-present.png
cmd.png
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40452139
No problem about my time.... Lets just solve the issues , points are not that important ;)

I think this is the issue,

instead of line  folder="c:\database" put this folder="c:\database\"
0
 

Author Comment

by:yimsoin57
ID: 40454093
The issue persists, "File No Present".
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40454104
Can you try attached sample and post result...
replace.vbs
0
 

Author Comment

by:yimsoin57
ID: 40454124
Now, I got the c:\database\test.txt dialog box and followed by File No Present dialog box pop up.  See attached.  Nothing happened in the out.txt file (zero byte).
test.png
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40454129
Can you post your test.txt?

And try attached?
replace.vbs
0
 

Author Comment

by:yimsoin57
ID: 40454413
Please see attached error.
Object-required-error.png
0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 2000 total points
ID: 40454809
Final version of code...

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

dim folder, input, output, fsObject, fObject_in,fObject_out, line, path

folder="c:\database\"
input ="test.txt"
output ="out.txt"

Set fsObject=CreateObject("Scripting.FileSystemObject")
if fsObject.FileExists(folder & input)=false then
   wscript.echo "File not present"
   wscript.Quit
end if

Set fObject_in = fsObject.OpenTextFile(folder & input, ForReading)
set fObject_out = fsObject.CreateTextFile(folder & output, TRUE)

Do Until fObject_in.AtEndOfStream
line= fObject_in.ReadLine
line=Replace(line,"|","-",1,1)
line=Replace(line,"|",";",1,2)
line=Replace(line,"|","",1,-1)
 fObject_out.WriteLine line
Loop

fObject_in.Close
fObject_out.Close

Open in new window

replace.vbs
0
 

Author Closing Comment

by:yimsoin57
ID: 40457021
Perfect coding.  Thank you.
0

Featured Post

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

Question has a verified solution.

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

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

770 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