Hello to everyone!
I'm a newbie here, I just started my VB "carier".. :)
Here is my question (might me simple to you but I 've found it a bit hard).
I have a txt file in which I want to replace every ":" with a "." !!!
After this replacement I want to replace every ".3" with a ".5" !!!
In fact the txt is a clock file and there are a lot of them here... I want to do this task automatically and not by opening every sigle file with notepad and replacing them manually.
THANK YOU FOR YOUR TIME AND YOUR PATIENCE!!!!
15 8176
Hi antonopn
load the txt file into textbox (text1.text)
then you can save it.
Code: ( VB 6 ) - Private Sub Command1_Click()
-
text1.Text = MyReplace(text1.Text, ":", ".") 'choose characters that you want to
-
'convert
-
text1.Text = MyReplace(text1.Text, "3", "5")
-
End Sub
-
-
Public Function MyReplace(strExpression As String, strFind As String, strReplace As String)
-
Dim intX As Integer
-
If (Len(strExpression) - Len(strFind)) >= 0 Then
-
For intX = 1 To Len(strExpression)
-
If Mid(strExpression, intX, Len(strFind)) = strFind Then
-
strExpression = Left(strExpression, (intX - 1)) + strReplace + Mid(strExpression, intX + Len(strFind), Len(strExpression))
-
End If
-
Next
-
End If
-
MyReplace = strExpression
-
End Function
GobbleGob.
Thanks a lot for your help!!!!!
I've got only one problem. I get a runtime error 424. "Object required"
Lets suppose that my text file is nik.txt and is in C:\ directory.
C:\nik.txt
How should I write the following in order to get it done? - Private Sub Command1_Click()
-
nik.txt = MyReplace(nik.txt, ":", ".")
-
nik.txt = MyReplace(nik.txt, ".3", ".5")
-
End Sub
Thank you so much!!!!
I have a txt file in which I want to replace every ":" with a "." !!!
After this replacement I want to replace every ".3" with a ".5" !!!
In fact the txt is a clock file and there are a lot of them here... I want to do this task automatically and not by opening every sigle file with notepad and replacing them manually.
THANK YOU FOR YOUR TIME AND YOUR PATIENCE!!!!
Well, first you'll need to open the file for input, grab its contents and close the file. -
Dim FileID as integer 'Our handle to the open file
-
Dim WholeFile as string 'String which will contain entire contents of text file
-
FileID = FreeFile 'Pick the first available handle
-
Open FILENAME for input as FileID
-
WholeFile = Input(LOF(FileID), #FileID)
-
'LOF() gives the length of the file, in bytes. We read this many characters and store them in WholeFile.
-
Close FileID 'We're done with the file now for now.
-
FILENAME should be the full path and filename of the text file. Or, it could just be the filename, if the text file is in the same folder as the program (App.Path).
To replace characters, use VB's Replace() function. -
WholeFile = Replace(WholeFile, ORIGTEXT, REPLACEMENTTEXT)
-
Of course, you can repeat this for however many replacements you need to perform.
Save this string in a new file. -
FileID = FreeFile
-
Open FILENAME for output as FileID
-
Print #FileID, WholeFile
-
Close FileID
-
NOTE: The file will be completely erased and re-built when that code executes. Also note that the 'Print' statement adds new-line and carriage-return characters to the end of the file (chr(13) and chr(10)).
Hope that helps you on your way. =)
Well, first you'll need to open the file for input, grab its contents and close the file. -
Dim FileID as integer 'Our handle to the open file
-
Dim WholeFile as string 'String which will contain entire contents of text file
-
FileID = FreeFile 'Pick the first available handle
-
-
Thanks for your help, I get a "freefile", "invalid outside procedure".
Any suggestions?
Hmm? Are you doing this within a sub/function?
e.g. -
Private Sub Form_Load()
-
AllTheCodeIPosted
-
End Sub
-
I always get a error when entering the path of my file. No matter if I do it using your code or Googlebob's!
For example the filename and directory is C:\nik.txt
I get an error on ":" implying that an "AS" is expected.
No matter what I do i get the same error.
Thanks for your patience!!!
VB is not my expertise (you can see for yourself).
Single clock file?
Open the clock file in Notepad.
Goto "Edit" > "Replace".
In the "Find what" field, type: :
In the "Replace with" field, type: .
Click "Replace All".
In the "Find what" field, type: .3
In the "Replace with" field, type: .5
Click "Replace All".
Goto "File" > "Save".
Done.
Multiple clock files?
This VB code assumes that all text files found in the application's directory are clock files. If you have other text files within the same directory, the code will need to be adjusted. -
Private FileStr As String, TempStr As String
-
-
Sub Main()
-
-
'Store the first text file found
-
'(all text files in the application's
-
'directory are assumed to be clock files)
-
FileStr = Dir(App.Path & "\*.txt")
-
-
'Go through all text files found
-
Do Until FileStr = vbNullString
-
'Store the current text file's path
-
FileStr = App.Path & "\" & FileStr
-
'Open the current text file for reading
-
Open FileStr For Input As #1
-
TempStr = Input$(LOF(1), 1)
-
'Close the file
-
Close #1
-
'Replace all occurances of ":" with "."
-
TempStr = Replace(TempStr, ":", ".")
-
'Replace all occurances of ".3" with ".5"
-
TempStr = Replace(TempStr, ".3", ".5")
-
'Open the current text file for writing
-
Open FileStr For Output As #1
-
'Write changes to the file
-
Print #1, TempStr
-
'Close the file
-
Close #1
-
'Find the next text file
-
FileStr = Dir
-
Loop
-
-
End Sub
-
Hello everyone! I find this site really interesting!
It is a useful script.. Though I do not have clock files but other type of files. I change "," with "." in my files!
But I do not know anything about VB. :(
I copy-paste this code in VB, in a new standard EXE. And press the "play" button.
But nothing happens. (though there are no errors in the code after compilation)
My files are in directory C:\ktr\
Please help me...
I know it is a funny question for you :)
Kisses Katerina
I always get a error when entering the path of my file. No matter if I do it using your code or Googlebob's!
For example the filename and directory is C:\nik.txt
I get an error on ":" implying that an "AS" is expected.
No matter what I do i get the same error.
Thanks for your patience!!!
VB is not my expertise (you can see for yourself).
Ah! You just need to surround the filename in quotes, like "this". ;)
Sorry, I didn't think to show that in my example.
Hello everyone! I find this site really interesting!
It is a useful script.. Though I do not have clock files but other type of files. I change "," with "." in my files!
But I do not know anything about VB. :(
I copy-paste this code in VB, in a new standard EXE. And press the "play" button.
But nothing happens. (though there are no errors in the code after compilation)
My files are in directory C:\ktr\
Please help me...
I know it is a funny question for you :)
Kisses Katerina
Katerina,
Ha ha, "clock files"... I don't think that this is an actual file type, but it could become one. Anyway;
In the example I have provided previously, find: - TempStr = Replace(TempStr, ":", ".")
"TempStr" is the string variable in which you want the replacement text to be stored. Replace is the function in Visual Basic that allows a character or string of characters to be replaced in a particular string. In the code above, the function's first parameter "TempStr" is also the variable that you want the Replace function to search, while the second parameter (in this case :) is the character (or string) you want replaced, and the third parameter (in this case .) is the character (or string) you want the second parameter to be replaced with.
If you have the MSDN Library installed on your computer, you can look up more information on the Replace functions, as well as many other functions, methods, properties, etc. in Visual Basic. The MSDN Library usually comes with a Visual Studio package. If not, you can always try the net.
Unless you save the Visual Basic project in the same directory as your text files (in your case, "C:\ktr\"), or compile your project into an .exe file using the "File" > "Make..." menu option, and place the EXE into "C:\ktr\", the code won't work.
If you don't want your text files to be in the same place as your program is, change: - FileStr = Dir(App.Path & "\*.txt")
to: - FileStr = Dir("Your text file directory here\Your text file here.txt")
or: - FileStr = Dir("Your text file directory here\*.txt")
for all text files in that directory.
Save the project before running it.
Well thank you for the advice!
It did not work to me! None of the solutions. I do not know if Katerina succeeded.
The code has no errors but nothing is done!
I save it inside the txt file's directory and run it but nothing. It might be a missing library (project->prefences).
This is the only I can imagine, because I created a DTS package in SQL SERVER 2000 and run it through VB (save as -> VB script). The table was created but data from txt did not copy! This sounds like a problem that does not allow txt files to be used (edited, copied) through VB.
A library? I do not know. I'm tired with this!
THANK YOU ALL!!!
p.s. Katerina if you made it, I'll eat my hat :)
Antonopn, I'm not sure if this program would work as a Visual Basic Script. I used Visual Basic 6 to create it, and it works fine with that.
Do you have Visual Basic 6?
yes I do! Visual Basic 6.
What do you create?
1)standard exe
2)activeX exe
3)dll
Something's wrong. Do not know what. I feel totally idiot with that :)
Thanks for your patience!
A Standard EXE program will do.
Remove the form that it puts in automatically for you, and instead add a module (Project > Add Module). Place the previous code I gave you in the module, save the project in the same directory as your clock files, and then try running the program (click the triangular play button).
Once you are satisfied that the program works, goto "File > Make "your project name"", type a name for your program EXE file, and then click OK. You can now run the program from the EXE file.
Works Fine Now!!!
Thank You So Much!!!!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: matt |
last post by:
I am using a text file as a database, each field delimited by ||
I want to be able to print all lines to a page, then by selecting one
(with, say, a radio button) it will load into the form at the...
|
by: Option^Explicit |
last post by:
What I'm trying to do:
Open a text file and display the contents in a text box (I've done this)
Need to be able to edit the file from within the textbox and have it save
back to the source...
|
by: Glenn M |
last post by:
I have a shared XML file on a server .
i also have one xslt file that performs a simple transform on in to
view the data.
now i want to have another page that lets users modify the shared xml...
|
by: John Salerno |
last post by:
I figured my first step is to install the win32 extension, which I did,
but I can't seem to find any documentation for it. A couple of the links
on Mark Hammond's site don't seem to work.
...
|
by: setar |
last post by:
How can I edit an xml file which has 250MB? I tried to use UltraEdit, Visual
Studio, Eclipse, Stylus Studio and XMLSpy editors but these programs can't
read this file because it is too big. SmEdit...
|
by: cyberco |
last post by:
I must be overlooking something here... I'm trying to edit a line in a
text file. I thought this was easy with fileinput, but all examples do
not write the line back to the file but simply 'print'...
|
by: moondaddy |
last post by:
I need to edit the text in many files so I'm writing a small routine to do
this.
First I have a method that loops through all the files in a directory and
passes the full file path to another...
|
by: Xah Lee |
last post by:
Text Processing with Emacs Lisp
Xah Lee, 2007-10-29
This page gives a outline of how to use emacs lisp to do text
processing, using a specific real-world problem as example. If you
don't know...
|
by: gator |
last post by:
Hello,
I am trying to edit a specific node in an XML file using C#. I have
searched around for example code to do this. The only solutions I
have come across either add a new node to the doc...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
| |