How to programatically create a text file containing the VB.Net code neccessary for recreating a different file programatically
-
Herm... let me try to explain it better: Say, for example, you create a ReadMe file you plan to use with your program. The file is an HTML file, because you like to use funny formats and hyperlinks and whatnot. Anyway, you want your program, when started, to check the root folder and see if that file exists there. If it doesn't exist, you want your program to create that file. What would you do? You would code your program to check for the file and create it if it doesn't exist, simple as that. Now, in the case of HTML files, you'd have to type the code to write the file line by line, as in the following example: Public Sub CopyMake() copyrightFile = Application.StartupPath & "\Copyright Info.htm" If System.IO.File.Exists(copyrightFile) = False Then FileOpen(1, copyrightFile, OpenMode.Binary) FilePut(1, "" & Environment.NewLine) FilePut(1, Environment.NewLine) FilePut(1, "" & Environment.NewLine) FilePut(1, "" & Environment.NewLine) FilePut(1, "" & Environment.NewLine) FilePut(1, "" & Environment.NewLine) FilePut(1, "Framework Download" & Environment.NewLine) FilePut(1, "" & Environment.NewLine) FilePut(1, Environment.NewLine) FilePut(1, "" & Environment.NewLine) FilePut(1, Environment.NewLine) FilePut(1, "
Coded by Mike
" & Environment.NewLine) FilePut(1, "
Project started on Wednesday, March 24, 2004
" & Environment.NewLine) FilePut(1, "
Email me at:
" & Environment.NewLine) FilePut(1, "" & Environment.NewLine) FilePut(1, "codemonkey85@hotmail.com
" & Environment.NewLine) FilePut(1, Environment.NewLine) FilePut(1, "" & Environment.NewLine) FilePut(1, Environment.NewLine) FilePut(1, "" & Environment.NewLine) FileClose(1) End If End Sub As lazy as I am, I would hate to type all of that. So I thought to myself, "Self, you know how to programatically create simple text files, and how to read text from other files. You could easily program
wow. i must be an idiot. still lost. ok, the sub will: 1 - select file to read (html file) 2 - select file to save output to (txt) 3 - take the txt in the html (input) file and saves it to the txt (output) file, including the actual vb code required to do this. ok for demonstration purposes: myHomePage.html: ---------------------------------------------------------------------------------------------- Heres my title
here is all my text for the web page.
---------------------------------------------------------------------------------------------- myDoc.txt (after program executed): ---------------------------------------------------------------------------------------------- Private Sub MakeFile() Dim inFile As StreamReader = File.OpenText(Application.StartupPath & "\myHomePage.html") Dim outFile As StreamWriter = File.CreateText(Application.StartupPath & "\myDoc.txt") outFile.Writeline("") outFile.Writeline("Heres my title") outFile.Writeline("") outFile.Writeline("
here is all my text for the web page.
") outFile.Writeline("") outFile.Writeline("") outFile.Close() inFile.Close() End Sub ---------------------------------------------------------------------------------------------- is this somewhat on the right track? ------------------------ Jordan. III
-
wow. i must be an idiot. still lost. ok, the sub will: 1 - select file to read (html file) 2 - select file to save output to (txt) 3 - take the txt in the html (input) file and saves it to the txt (output) file, including the actual vb code required to do this. ok for demonstration purposes: myHomePage.html: ---------------------------------------------------------------------------------------------- Heres my title
here is all my text for the web page.
---------------------------------------------------------------------------------------------- myDoc.txt (after program executed): ---------------------------------------------------------------------------------------------- Private Sub MakeFile() Dim inFile As StreamReader = File.OpenText(Application.StartupPath & "\myHomePage.html") Dim outFile As StreamWriter = File.CreateText(Application.StartupPath & "\myDoc.txt") outFile.Writeline("") outFile.Writeline("Heres my title") outFile.Writeline("") outFile.Writeline("
here is all my text for the web page.
") outFile.Writeline("") outFile.Writeline("") outFile.Close() inFile.Close() End Sub ---------------------------------------------------------------------------------------------- is this somewhat on the right track? ------------------------ Jordan. III
Err, not quite. First of all, I don't want to have to hardcode the source into the program, or else 1) what would be the point in selecting the source file, and 2) it would have to be modified and recompiled each time I wanted to copy a file. I want a sub that would take your example HTML file, and create a text file containing this: Private Sub MakeFile() Dim inFile As StreamReader = File.OpenText(Application.StartupPath & "\myHomePage.html") Dim outFile As StreamWriter = File.CreateText(Application.StartupPath & "\myDoc.txt") outFile.Writeline("") outFile.Writeline("Heres my title") outFile.Writeline("") outFile.Writeline("
here is all my text for the web page.
") outFile.Writeline("") outFile.Writeline("") outFile.Close() inFile.Close() End Sub This would be a text file containing the VB.Net code required to remake the source file, which is what I want the original sub to make. In other words, if I were to run this code on a program (that is to say, the code in the new text file), it would create the myHomePage.html file in the bin folder. As I sit here, I contemplate the last words of Socrates: "I drank what?".
-
Err, not quite. First of all, I don't want to have to hardcode the source into the program, or else 1) what would be the point in selecting the source file, and 2) it would have to be modified and recompiled each time I wanted to copy a file. I want a sub that would take your example HTML file, and create a text file containing this: Private Sub MakeFile() Dim inFile As StreamReader = File.OpenText(Application.StartupPath & "\myHomePage.html") Dim outFile As StreamWriter = File.CreateText(Application.StartupPath & "\myDoc.txt") outFile.Writeline("") outFile.Writeline("Heres my title") outFile.Writeline("") outFile.Writeline("
here is all my text for the web page.
") outFile.Writeline("") outFile.Writeline("") outFile.Close() inFile.Close() End Sub This would be a text file containing the VB.Net code required to remake the source file, which is what I want the original sub to make. In other words, if I were to run this code on a program (that is to say, the code in the new text file), it would create the myHomePage.html file in the bin folder. As I sit here, I contemplate the last words of Socrates: "I drank what?".
didnt u just repeat me? in your last post u copied and pasted my example for the txt document output, and said that that is what you want it to show... well thats exactly wut i was asking. i pasted that code there as an example of what the output for the txt file is that you are looking for, is that exactly what you are looking for? im working on it right now, post in afew minutes. ------------------------ Jordan. III
-
Err, not quite. First of all, I don't want to have to hardcode the source into the program, or else 1) what would be the point in selecting the source file, and 2) it would have to be modified and recompiled each time I wanted to copy a file. I want a sub that would take your example HTML file, and create a text file containing this: Private Sub MakeFile() Dim inFile As StreamReader = File.OpenText(Application.StartupPath & "\myHomePage.html") Dim outFile As StreamWriter = File.CreateText(Application.StartupPath & "\myDoc.txt") outFile.Writeline("") outFile.Writeline("Heres my title") outFile.Writeline("") outFile.Writeline("
here is all my text for the web page.
") outFile.Writeline("") outFile.Writeline("") outFile.Close() inFile.Close() End Sub This would be a text file containing the VB.Net code required to remake the source file, which is what I want the original sub to make. In other words, if I were to run this code on a program (that is to say, the code in the new text file), it would create the myHomePage.html file in the bin folder. As I sit here, I contemplate the last words of Socrates: "I drank what?".
ok.. is this more accurate: ACTUAL CODE FOR SUB: ------------------------------------------------------------------------------------------------------- Private Sub MakeFile(ByVal source As String, ByVal destination As String) Dim inFile As StreamReader = File.OpenText(source) Dim outFile As StreamWriter = File.CreateText(destination) outfile.WriteLine("Private Sub MakeFile(ByVal source As String, ByVal destination As String)") outfile.WriteLine("Dim inFile As StreamReader = File.OpenText(source)") outFile.WriteLine("Dim outFile As StreamWriter = File.CreateText(destination)") outFile.WriteLine() Do Until inFile.Peek = -1 outFile.WriteLine("outFile.WriteLine(" & Chr(34) & inFile.ReadLine & Chr(34) & ")") Loop outFile.WriteLine() outFile.WriteLine("outFile.Close()") outFile.WriteLine("inFile.Close()") outFile.WriteLine() outFile.WriteLine("End Sub") outFile.Close() inFile.Close() End Sub ------------------------------------------------------------------------------------------------------- CONTENTS OF .txt FILE AFTER PROGRAM EXECUTED: ------------------------------------------------------------------------------------------------------- Private Sub MakeFile(ByVal source As String, ByVal destination As String) Dim inFile As StreamReader = File.OpenText(source) Dim outFile As StreamWriter = File.CreateText(destination) outFile.WriteLine("") outFile.WriteLine("Heres my title") outFile.WriteLine("") outFile.WriteLine("
here is all my text for the web page.
") outFile.WriteLine("") outFile.WriteLine("") outFile.Close() inFile.Close() End Sub ------------------------------------------------------------------------------------------------------- for the .txt file part above (the outfile.writeline("")) part), is that correct? or should it display the Do/Loop as shown at the top (in the actual sub's code), in place of all the 'outFile.WriteLine' commands with the html tags ------------------------ Jordan. III
-
ok.. is this more accurate: ACTUAL CODE FOR SUB: ------------------------------------------------------------------------------------------------------- Private Sub MakeFile(ByVal source As String, ByVal destination As String) Dim inFile As StreamReader = File.OpenText(source) Dim outFile As StreamWriter = File.CreateText(destination) outfile.WriteLine("Private Sub MakeFile(ByVal source As String, ByVal destination As String)") outfile.WriteLine("Dim inFile As StreamReader = File.OpenText(source)") outFile.WriteLine("Dim outFile As StreamWriter = File.CreateText(destination)") outFile.WriteLine() Do Until inFile.Peek = -1 outFile.WriteLine("outFile.WriteLine(" & Chr(34) & inFile.ReadLine & Chr(34) & ")") Loop outFile.WriteLine() outFile.WriteLine("outFile.Close()") outFile.WriteLine("inFile.Close()") outFile.WriteLine() outFile.WriteLine("End Sub") outFile.Close() inFile.Close() End Sub ------------------------------------------------------------------------------------------------------- CONTENTS OF .txt FILE AFTER PROGRAM EXECUTED: ------------------------------------------------------------------------------------------------------- Private Sub MakeFile(ByVal source As String, ByVal destination As String) Dim inFile As StreamReader = File.OpenText(source) Dim outFile As StreamWriter = File.CreateText(destination) outFile.WriteLine("") outFile.WriteLine("Heres my title") outFile.WriteLine("") outFile.WriteLine("
here is all my text for the web page.
") outFile.WriteLine("") outFile.WriteLine("") outFile.Close() inFile.Close() End Sub ------------------------------------------------------------------------------------------------------- for the .txt file part above (the outfile.writeline("")) part), is that correct? or should it display the Do/Loop as shown at the top (in the actual sub's code), in place of all the 'outFile.WriteLine' commands with the html tags ------------------------ Jordan. III
Yayness! It works! Unfortunately, when I try to copy certain files, quotation marks tend to get in the way... but for my purposes this will do just fine. Thanks for the help! As I sit here, I contemplate the last words of Socrates: "I drank what?".
-
So yeah, I need some help. I want to create a sub that will allow me to select a source file, select a destination text (.txt) file, and write to that text file the code neccessary to programatically create the source file. Why would I do such a ludicrously convoluted thing, you ask? 1) I need to create programs that automatically detect whether or not there is a certain file in the root folder, and if that file doesn't exist, I want it to create the file (i.e. a ReadMe file, usually an HTML file). 2) Since they are usually HTML files and I know squat about HTML, it would be easier just to create the file (using OpenOffice.Org in my case), and automate the arduous task of retrieving the HTML source. 3) I don't really have a third thing, but I felt three was the neccessary number of topics to address, and figured it'd be lame to stop at two. Here's what I have so far:
Public Sub FileCopyCode() 'Declaring variables Dim FileOpen As OpenFileDialog Dim SourcePath, OutPath As String Dim FileSave As SaveFileDialog Dim SourceFile As System.IO.File Dim SourceWrite As System.IO.StreamWriter 'Starting the procedure FileOpen.ShowDialog() SourcePath = FileOpen.FileName FileSave.Filter = "Text File|txt" FileSave.ShowDialog() OutPath = FileSave.FileName SourceWrite = SourceFile.CreateText(OutPath) Dim SourceRead As System.IO.StreamReader SourceRead = SourceFile.OpenText(SourcePath) 'Now, if I could just set up a loop starting here... '::Count number of lines in source file, loop following_ '_procedure that many times:: SourceWrite.WriteLine("FilePut(1, " & SourceRead.ReadLine) '...and ending about here, then maybe I could copy_ '_each line with the FilePut thing in front, and boom! I'd have_ '_what I want End Sub 'How to programatically create a text file containing the '_VB.Net code neccessary for recreating a different file programatically
Any help is appreciated, thanks. As I sit here, I contemplate the last words of Socrates: "I drank what?".Ummm...why not just keep another copy of the files somewhere? Then write an app that can compare the last modified times of the working copies to the stored copies and if they are different, copy the stored copy back out to the working copy? RageInTheMachine9532
-
Yayness! It works! Unfortunately, when I try to copy certain files, quotation marks tend to get in the way... but for my purposes this will do just fine. Thanks for the help! As I sit here, I contemplate the last words of Socrates: "I drank what?".
-
Ummm...why not just keep another copy of the files somewhere? Then write an app that can compare the last modified times of the working copies to the stored copies and if they are different, copy the stored copy back out to the working copy? RageInTheMachine9532
Well, I was trying to keep my program as stand alone as possible, so keeping another copy of the files would be good, but I'd rather do it the dumb way. I'll have to try Chr(34) soon. Other than that, this is exactly what I wanted. As I sit here, I contemplate the last words of Socrates: "I drank what?".
-
Well, I was trying to keep my program as stand alone as possible, so keeping another copy of the files would be good, but I'd rather do it the dumb way. I'll have to try Chr(34) soon. Other than that, this is exactly what I wanted. As I sit here, I contemplate the last words of Socrates: "I drank what?".
-
i used the Chr(34) in the example i provided. simply copy and paste the exact sub from my example afew posts ago, and test it out. ------------------------ Jordan. III
The only problem I have is when I get lines like this in the output text file sub: outFile.WriteLine("") Naturally, the issue is with the quotation marks. If I could get that to look like this instead: outFile.WriteLine("") Then I wouldn't get any problems. Bear in mind that this is the product of your exact sub. In any case, it doesn't really matter, because the sub accomplishes exactly what I need it to; it gives me the code to copy smaller, more simple HTML files. Therefore, I don't think this will be an issue. I hope not. Anyway, thanks again for your help. As I sit here, I contemplate the last words of Socrates: "I drank what?".