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
Use a simple XSLT to read the RSS feed from a blog
Generating VBScript to read a blog
Comments
Anonymous
October 04, 2007
PingBack from http://www.artofbam.com/wordpress/?p=5236Anonymous
October 04, 2007
Hi, Anyway to do the same thing in C# 2.0? Thank youAnonymous
October 05, 2007
The comment has been removedAnonymous
October 08, 2007
Thanks for the great tip Calvin!Anonymous
October 08, 2007
In my prior post Use new XML Features of VB to generate dynamic scripts and text files , we generatedAnonymous
October 08, 2007
PingBack from http://www.itsatrap.info/generate-and-run-powershell-scripts-dynamically/Anonymous
October 08, 2007
PingBack from http://msdnrss.thecoderblogs.com/2007/10/08/generate-and-run-powershell-scripts-dynamically/Anonymous
October 23, 2007
One of the things I most dearly missed from FoxPro when I moved to VB.NET was the ability to easily dumpAnonymous
November 28, 2007
Using WPF, it’s great to use declarative XAML , but it’s also great to use dynamic code. With VB’s newAnonymous
November 28, 2007
Using WPF, it’s great to use declarative XAML , but it’s also great to use dynamic code. With VB’s newAnonymous
November 28, 2007
PingBack from http://msdnrss.thecoderblogs.com/2007/11/28/wpf-animation-use-dynamic-xaml-to-animate-movies-using-xamlreader-or-animate-in-code-using-beginstoryboard/Anonymous
December 30, 2007
PingBack from http://restaurants.247blogging.info/?p=588Anonymous
March 20, 2008
PingBack from http://frankthefrank.info/entry.php?id=kwws%3d22ehwd1eorjv1pvgq1frp2fdoylqbkvld2dufklyh2533%3a243237285%3a989%3c1dvs%7b