473,503 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Visual Basic procedure to find and replace text

6 New Member
This is a shot in the dark, but I'm pretty sure VB can do this. I'll give some background on the problem first.
I work at a casino and we use embossers to print cards for players. We currently 3 different embossers that can be switched to in order to print different cards. We have a program that is run that asks a simple A, B or C to switch it. Unfortunately, we don't know who created the original file, but we figured out what it does. Basically it goes into our Oasis.ini file and searches for the line that says this:
[Card]
embossprinter=\\acijpnt32\ACIJPPRN103

The last number can either be a 3, 4, or 5. We are attempting to move these embossers to our new print server, but to do so, we have to recreate a program that can go into the Oasis.ini file, search for the above mentioned line, and change the last part of the code. The new line would need to look something like this:
[Card]
embossprinter=\\acijpnt30\ACIJPPRN090

the last can be 091, 092, 093, 094 or 095 (we are planning to add a few embossers in as well so we are including the range for that).
A friend of mine looked some stuff up and said that we could set the first part as the constant that it looks for and set the last part as the variable that we can change. Unfortunately, any of the coding I've found hasn't helped at all in doing this. If anyone has some insights on this, that'd be great.

Thanks!
May 20 '07 #1
14 10438
HaggardSmurf
12 New Member
Just paste it all into word and press ctrl H and type what you want to find and what you want to replace it with.

(I think thats what your asking anyways)
May 20 '07 #2
hiattech
6 New Member
No, not quite. we know how to change the line manually, but the people we're setting this up for don't. The program will go in and change the line without them knowing. basically, they open the app, click a button with the name of the printer they want, and in the background (without them seeing anything that happens) it goes into the file, and changes the line to the correct printer. does that make more sense?
May 20 '07 #3
Dököll
2,364 Recognized Expert Top Contributor
No, not quite. we know how to change the line manually, but the people we're setting this up for don't. The program will go in and change the line without them knowing. basically, they open the app, click a button with the name of the printer they want, and in the background (without them seeing anything that happens) it goes into the file, and changes the line to the correct printer. does that make more sense?
Hello, hiattech!

Here is a link to Google results:

http://www.google.com/search?q=Using+a+VB+program+to+find+and+replace+te xt++in+.ini+file&rls=com.microsoft:en-us:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7SUNA

I have not done anything in .ini so I cannot help you there. I do have a code that replaces characters in a text file though (.txt). Let me know if it can be helpful.

Good luck with the project!
May 20 '07 #4
hiattech
6 New Member
well, lets see if the replacing text in a .txt works on an ini. can you give me a few lines to try? I'm semi rusty on VB, but I can change things around if need be. Basically, on button click, it has to run the changing of a line in the file.
Thanks.
May 20 '07 #5
Dököll
2,364 Recognized Expert Top Contributor
well, lets see if the replacing text in a .txt works on an ini. can you give me a few lines to try? I'm semi rusty on VB, but I can change things around if need be. Basically, on button click, it has to run the changing of a line in the file.
Thanks.
Please stay tuned, I have to fetch for it. It goes something like:

Expand|Select|Wrap|Line Numbers
  1. Dim ReplaceThis As String
  2. Dim GoFetchIt As String
  3. Private Sub GoReplace_Click()
  4. GoFetchIt = Text1(0).Text
  5. ReplaceThis = Text1(1).Text 'Perhaps your file loaded here
  6. Do While InStr(LCase(GoFetchIt), ReplaceThis)
  7. ...something in that kind of a line
  8.  
Can't recall the rest of as of now. You'd need to tell VB to fetch the extreme right of the file though.

In a bit!
May 22 '07 #6
Killer42
8,435 Recognized Expert Expert
How about this for a "quick and dirty" approach?

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Sub DumpFile_V02(ByVal FileName As String, ByVal strChangeFrom As String, ByVal strChangeTo As String)
  4.   Dim FileNo As Long
  5.   Dim FileSize As Long
  6.   Dim Buffer As String
  7.   Dim CharNo As Long, Char As String * 1
  8.  
  9.  
  10.   ' *** Step 1: Read the current file into a string variable...
  11.  
  12.   FileNo = FreeFile ' Get next available file number.
  13.   Open FileName For Binary Access Read Write Lock Write As #FileNo
  14.   FileSize = LOF(FileNo) ' Determine how large the file is (in bytes).
  15.   Buffer = Space$(FileSize) ' Set our buffer (string) to that length.
  16.   ' The length of the string (Buffer) determines how many bytes are read...
  17.   Get #FileNo, , Buffer ' Grab a chunk of data from the file.
  18.   Close #FileNo
  19.  
  20.   ' *** Step 2: Make the change in memory...
  21.   Buffer = Replace(Buffer, strChangeFrom, strChangeTo)
  22.  
  23.   ' *** Step 3: Overwrite the file...
  24.  
  25.   FileNo = FreeFile ' Get next available file number.
  26.   Open FileName For Output Access Write Lock Write As #FileNo
  27.   Close #FileNo
  28.  
  29. End Sub
Just paste this into a code module, and you've got a simple Text file string replacement routine. Just pass it the name of the INI file, the old string and the new string (for safety you should pass the full line, not just "003" or whatever).

Note that it has none of the niceties such as error handling, and so on.

Oh, and yes, an INI file is simply a plain ASCII text file with a different extension.
May 22 '07 #7
Killer42
8,435 Recognized Expert Expert
Oh rats!

That will only work if you know the exact value that's already there.

Hm... how about this...

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Sub ReplaceInFile(ByVal FileName As String, ByVal strFindThis As String, ByVal lngCharsToChange As Long, ByVal strChangeTo As String)
  4.   Dim FileNo As Long
  5.   Dim Buffer As String
  6.   Dim Length As Long
  7.   Dim strLine As String
  8.  
  9.   Length = Len(strFindThis)
  10.   If Length = 0 Then Exit Sub ' Drop out if o search string passed.
  11.  
  12.   ' *** Step 1: Read the current file, doing the switch as we go...
  13.  
  14.   FileNo = FreeFile ' Get next available file number.
  15.   Open FileName For Input Access Read Lock Write As #FileNo
  16.   Do Until EOF(FileNo)
  17.     Line Input #FileNo, strLine
  18.     If Left$(strLine, Length) = strFindThis Then
  19.       strLine = Left$(strLine, Len(strLine) - lngCharsToChange) & strChangeTo
  20.     End If
  21.     Buffer = Buffer & IIf((Buffer = ""), "", vbNewLine) & strLine
  22.   Loop
  23.  
  24.   ' *** Step 2: Write back the modified file...
  25.  
  26.   FileNo = FreeFile ' Get next available file number.
  27.   Open FileName For Output Access Write Lock Write As #FileNo
  28.   Print #FileNo, Buffer
  29.   Close #FileNo
  30.  
  31. End Sub
This is untested, so use it with care. It has the potential to totally stuff up pretty much any file you tell it to.

The idea is that you would give it the first, constant part of the line, the number of character to replace at the end (um... I think it was 3?) and the string to replace them with.
May 22 '07 #8
hiattech
6 New Member
Oh rats!

That will only work if you know the exact value that's already there.

Hm... how about this...

This is untested, so use it with care. It has the potential to totally stuff up pretty much any file you tell it to.

The idea is that you would give it the first, constant part of the line, the number of character to replace at the end (um... I think it was 3?) and the string to replace them with.
What version of VB is this using? I copied this in and received a lot of errors. As I went thorugh trying to find the spots I needed to replace, I found this:
'Line' statements are longer supported. File I/O functionality is available as 'Microsoft.VisualBasic.FileSystem.LineInput' and the graphics functionality is available as 'System.Drawing.Graphics.DrawLine'.

I'm currently using Visual Studios 2005, but I have 2003 on my laptop. Which would be the better to use?
May 25 '07 #9
hiattech
6 New Member
Here's a screenshot of the process they go through to change the embossers. They double click a file called NewEmbosser.exe, it opens a dos-like window like below. That's what they see, and then what happens in the background that they do not see. It's simple, yet not.

May 25 '07 #10
Killer42
8,435 Recognized Expert Expert
Sorry, this was Vb6 code. If you have a choice, I suppose it's better to use the later version (2005) unless this introduces problem in backward compatibility with your other system.

I'd suggest you either use 'Microsoft.VisualBasic.FileSystem.LineInput' as suggested by the error messages, or (probably the better option) look up how to do line by line file input in VB.Net. It probably involves using StreamReader, or FileSystemObject (or both).

I hope to soon have information about this sort of simple operation available in the Articles section, but so far we only have the Vb6 version.
May 25 '07 #11
Dököll
2,364 Recognized Expert Top Contributor
Nuts, this was a wonderfully written code too. Are you aware of session videos for VB 2005 Express, why don't you take a listen, watch/get tips while we try to come up with something, it's sure probable to read ini through VB 2005:

http://msdn.microsoft.com/vstudio/express/vb/features/

Scroll down to watch video session.

Please stay tuned!
May 25 '07 #12
Dököll
2,364 Recognized Expert Top Contributor
Nuts, this was a wonderfully written code too. Are you aware of session videos for VB 2005 Express, why don't you take a listen, watch/get tips while we try to come up with something, it's sure probable to read ini through VB 2005:

http://msdn.microsoft.com/vstudio/express/vb/features/

Scroll down to watch video session.

Please stay tuned!
I am on a hunt for a solution for you, just started learning VB 2005, give me a month or so to come with the code. Picked up VB 2005 Cookbook by O'Reilly, looks promissing...
May 31 '07 #13
danp129
323 Recognized Expert Contributor
Here's a regular expression approach. I didn't build the expression to only match within any particular [section] though.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit On
  2. Imports System.Text.RegularExpressions
  3. Public Class Form1
  4.  
  5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.         Dim sMyIniPath As String = "c:\myini.ini"
  7.         Dim sPrinterName As String = "printserver001"
  8.         Dim sMyIniText As String = My.Computer.FileSystem.ReadAllText(sMyIniPath)
  9.         sMyIniText = Regex.Replace(sMyIniText, "embossprinter=[^\r\n]+", "embossprinter=" & sPrinterName)
  10.         My.Computer.FileSystem.WriteAllText("c:\myini.ini", sMyIniText, False)
  11.     End Sub
  12. End Class
Jun 4 '07 #14
hiattech
6 New Member
Hmm, i'll give that a shot. Haven't had a chance to work on this project lately so hopefully i can put a bit more into getting it done now.

Thanks!


Here's a regular expression approach. I didn't build the expression to only match within any particular [section] though.
.....
Jun 8 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

0
6678
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
16
2123
by: Nathan Sokalski | last post by:
I have Visual Studio .NET and SQL Server Desktop Engine on my computer. I have created an empty database using Visual Studio .NET's Server Explorer. However, I am having trouble connecting to the...
6
1525
by: Bala | last post by:
Hi, This is my stored procured. i try to pass the parametre like this. i am getting error. any one please tell me how to pass the parameter? vb code: ..Connection = New...
2
3130
by: Michael | last post by:
Running DB2 v7 UDB ("DB2 v7.1.0.93", "n031208" and "WR21333") on Windows XP, I am unable to find out why the "Build for Debug" option within Stored Procedure Builder is not enabled on Java stored...
27
35892
by: code_wrong | last post by:
Visual Basic (not dot net) what is the best way to check the User has entered an integer into an InputBox? isNumeric() checks for a numeric value .. but does not notify of numbers with decimal...
1
13781
by: Groningen | last post by:
Hi, I was wondering if someone could help me on this one. I've made a stored procedure in SQL Server Enterprise Manager. When I call the stored procedure in Visual Basic I didn't succeed to...
56
2763
by: paz | last post by:
Hi, Does anybody know how to start new C project with microsoft visual c++? I've created a file, but Tools-Run is inActive. How can I make it Active?
0
2546
by: bernhard.nowara | last post by:
Hello, I found a bug in Visual Basic used with Visual Studio Macros (Microsoft Visual Studio 2005 + SP1). Description: If I apply the simple Visual Basic statement...
10
3096
by: Esmael | last post by:
Hi to all, /*****************************/ OS-WIn XP SP2 VB6 SP6 /*****************************/ Is their anyone who can help me with this: Source code written on VB6.
0
7292
jwwicks
by: jwwicks | last post by:
Introduction This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.