Use new XML Features of VB to generate dynamic scripts and text files
There's a very useful feature that FoxPro users have had for over 2 decades: being able to output text in any format, with WYSIWYG and the ability to embed expressions (The TEXT…ENDTEXT command)
Generating XML, HTML, Bat files, PowerShell Script, etc is a breeze with this command. Free form text between the "TEXT" and "ENDTEXT" commands is allowed, with embedded expressions
You can also just copy text from an existing HTML/XML, etc. source file and paste it directly into VB or Fox, modifying it to embed an expression.
FoxPro even allows you to change the delimiters (default "<<" and ">>") of embedded expressions with the SET TEXTMERGE DELIMITERS command . Unfortunately, it only allows a max of 2 chars as the delimiters, whereas the VB XML Expression hole requires 3 chars for the left delimiter "<%=".
You can use the XML features of VB 2008 to create such a raw text file!
In VB, to embed a "<" you can use "<" or <%= "<" %>
Fox code sample:
SET TEXTMERGE DELIMITERS TO "<%","%>"
sOutputFile="d:\t.txt"
sScriptFile="d:\t.bat"
ERASE (sOutputFile)
ERASE (sScriptFile)
TEXT TO myvar TEXTMERGE
rem This is a bat file that does a dir on a path and redirects output to a file
rem the following line is something like "dir <path> >> d:\t.txt"
dir >> <% sOutputFile %>
echo This text goes to the output <%TRANSFORM(DATETIME()) %> <% sOutputFile %>
ENDTEXT
STRTOFILE(myvar,sScriptFile)
run cmd /c &sScriptFile
IF FILE(sOutputFile)
?FILETOSTR(sOutputFile)
ENDIF
VB Code Sample:
Module Module1
Sub Main()
Dim sOutputFile = "d:\t.txt"
Dim sScriptFile = "d:\t.bat"
If My.Computer.FileSystem.FileExists(sOutputFile) Then
My.Computer.FileSystem.DeleteFile(sOutputFile)
End If
Dim BatFile = _
<myxml>
rem This is a bat file that does a dir on a path and redirects output to a file
rem the following line is something like "dir <path> >> d:\t.txt"
dir "<%= My.Application.Info.DirectoryPath %>" >> <%= sOutputFile %>
echo This text goes to the output <%= DateTime.Now.ToString %> >> <%= sOutputFile %>
</myxml>
Console.WriteLine(BatFile.Value)
My.Computer.FileSystem.WriteAllText(sScriptFile, BatFile.Value, False, Text.Encoding.ASCII)
Dim proc = System.Diagnostics.Process.Start("cmd", "/c " + sScriptFile)
proc.WaitForExit()
Dim sOutputString = ""
If My.Computer.FileSystem.FileExists(sOutputFile) Then
sOutputString = My.Computer.FileSystem.ReadAllText(sOutputFile)
End If
MsgBox("Proc Done " + sOutputString)
End Sub
End Module
VB Code output
Volume in drive C is Calvinh1C
Volume Serial Number is A815-FF6E
Directory of C:\Documents and Settings\calvinh\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\bin\Debug
10/04/2007 09:40 AM <DIR> .
10/04/2007 09:40 AM <DIR> ..
10/04/2007 09:40 AM 15,872 ConsoleApplication1.exe
10/04/2007 09:40 AM 40,448 ConsoleApplication1.pdb
10/04/2007 09:39 AM 9,568 ConsoleApplication1.vshost.exe
09/24/2007 08:47 PM 490 ConsoleApplication1.vshost.exe.manifest
10/04/2007 09:40 AM 127 ConsoleApplication1.xml
5 File(s) 66,505 bytes
2 Dir(s) 56,774,217,728 bytes free
This text goes to the output 10/4/2007 9:40:18 AM
See also:
Use temporary projects in Visual Studio
LINQ Cookbook, Recipe 8: Querying XML Using LINQ
Start using XML and XSLT to create HTML