473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Edit a text file automatically and save it.

antonopn
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!!!!
Mar 29 '08 #1
15 8176
gobblegob
133 100+
Hi antonopn

load the txt file into textbox (text1.text)
then you can save it.
Code: ( VB 6 )
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.     text1.Text = MyReplace(text1.Text, ":", ".") 'choose characters that you want to
  3.                                                  'convert
  4.     text1.Text = MyReplace(text1.Text, "3", "5")
  5. End Sub
  6.  
  7. Public Function MyReplace(strExpression As String, strFind As String, strReplace As String)
  8.     Dim intX As Integer
  9.     If (Len(strExpression) - Len(strFind)) >= 0 Then
  10.         For intX = 1 To Len(strExpression)
  11.             If Mid(strExpression, intX, Len(strFind)) = strFind Then
  12.                 strExpression = Left(strExpression, (intX - 1)) + strReplace + Mid(strExpression, intX + Len(strFind), Len(strExpression))
  13.             End If
  14.         Next
  15.     End If
  16.     MyReplace = strExpression
  17. End Function
GobbleGob.
Mar 30 '08 #2
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?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.       nik.txt = MyReplace(nik.txt, ":", ".")
  3.       nik.txt = MyReplace(nik.txt, ".3", ".5")
  4. End Sub
Thank you so much!!!!
Mar 30 '08 #3
Robbie
180 100+
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.

Expand|Select|Wrap|Line Numbers
  1. Dim FileID as integer 'Our handle to the open file
  2. Dim WholeFile as string 'String which will contain entire contents of text file
  3. FileID = FreeFile 'Pick the first available handle
  4. Open FILENAME for input as FileID
  5. WholeFile = Input(LOF(FileID), #FileID) 
  6. 'LOF() gives the length of the file, in bytes. We read this many characters and store them in WholeFile.
  7. Close FileID 'We're done with the file now for now.
  8.  
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.
Expand|Select|Wrap|Line Numbers
  1. WholeFile = Replace(WholeFile, ORIGTEXT, REPLACEMENTTEXT)
  2.  
Of course, you can repeat this for however many replacements you need to perform.

Save this string in a new file.
Expand|Select|Wrap|Line Numbers
  1. FileID = FreeFile
  2. Open FILENAME for output as FileID
  3. Print #FileID, WholeFile
  4. Close FileID
  5.  
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. =)
Mar 30 '08 #4
Well, first you'll need to open the file for input, grab its contents and close the file.

Expand|Select|Wrap|Line Numbers
  1. Dim FileID as integer 'Our handle to the open file
  2. Dim WholeFile as string 'String which will contain entire contents of text file
  3. FileID = FreeFile 'Pick the first available handle
  4.  
  5.  
Thanks for your help, I get a "freefile", "invalid outside procedure".
Any suggestions?
Mar 31 '08 #5
Robbie
180 100+
Hmm? Are you doing this within a sub/function?
e.g.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.     AllTheCodeIPosted
  3. End Sub
  4.  
Apr 1 '08 #6
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).
Apr 1 '08 #7
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.

Expand|Select|Wrap|Line Numbers
  1. Private FileStr As String, TempStr As String
  2.  
  3. Sub Main()
  4.  
  5.     'Store the first text file found
  6.     '(all text files in the application's
  7.     'directory are assumed to be clock files)
  8.     FileStr = Dir(App.Path & "\*.txt")
  9.  
  10.     'Go through all text files found
  11.     Do Until FileStr = vbNullString
  12.         'Store the current text file's path
  13.         FileStr = App.Path & "\" & FileStr
  14.         'Open the current text file for reading
  15.         Open FileStr For Input As #1
  16.         TempStr = Input$(LOF(1), 1)
  17.         'Close the file
  18.         Close #1
  19.         'Replace all occurances of ":" with "."
  20.         TempStr = Replace(TempStr, ":", ".")
  21.         'Replace all occurances of ".3" with ".5"
  22.         TempStr = Replace(TempStr, ".3", ".5")
  23.         'Open the current text file for writing
  24.         Open FileStr For Output As #1
  25.         'Write changes to the file
  26.         Print #1, TempStr
  27.         'Close the file
  28.         Close #1
  29.         'Find the next text file
  30.         FileStr = Dir
  31.     Loop
  32.  
  33. End Sub
  34.  
Apr 2 '08 #8
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
Apr 2 '08 #9
Robbie
180 100+
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.
Apr 2 '08 #10
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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. FileStr = Dir(App.Path & "\*.txt")
to:

Expand|Select|Wrap|Line Numbers
  1. FileStr = Dir("Your text file directory here\Your text file here.txt")
or:

Expand|Select|Wrap|Line Numbers
  1. FileStr = Dir("Your text file directory here\*.txt")
for all text files in that directory.

Save the project before running it.
Apr 3 '08 #11
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 :)
Apr 7 '08 #12
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?
Apr 9 '08 #13
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!
Apr 10 '08 #14
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.
Apr 15 '08 #15
Works Fine Now!!!


Thank You So Much!!!!
Apr 18 '08 #16

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

Similar topics

6
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...
4
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...
4
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...
8
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. ...
10
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...
3
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'...
4
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...
1
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...
4
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...
0
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,...
0
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$) { } ...
0
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...
0
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...
0
BarryA
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...
1
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...
0
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...
0
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...
0
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,...

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.