VB Code to Replace Text with Certain Characters

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.
yimsoin57Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Phillip BurtonDirector, Practice Manager and Computing ConsultantCommented:
Which version of VB are you using?
Vitor MontalvãoMSSQL Senior EngineerCommented:
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

Haris DulicIT ArchitectCommented:
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

Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

yimsoin57Author Commented:
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.
Haris DulicIT ArchitectCommented:
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...
Haris DulicIT ArchitectCommented:
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

yimsoin57Author Commented:
How can I run the code?  Thanks.
Bill PrewIT / Software Engineering ConsultantCommented:
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
yimsoin57Author Commented:
I got a  message "File Not Present" after running the vbs file.  Please help.  Thank you.
Haris DulicIT ArchitectCommented:
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
yimsoin57Author Commented:
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
Haris DulicIT ArchitectCommented:
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\"
yimsoin57Author Commented:
The issue persists, "File No Present".
Haris DulicIT ArchitectCommented:
Can you try attached sample and post result...
replace.vbs
yimsoin57Author Commented:
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
Haris DulicIT ArchitectCommented:
Can you post your test.txt?

And try attached?
replace.vbs
yimsoin57Author Commented:
Please see attached error.
Object-required-error.png
Haris DulicIT ArchitectCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
yimsoin57Author Commented:
Perfect coding.  Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.