Welcome to MSDN Blogs Sign in | Join | Help

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 "&lt;" 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 &lt;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

 

Published Thursday, October 04, 2007 10:50 AM by Calvin_Hsia
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Techy News Blog &raquo; Use new XML Features of VB to generate dynamic scripts and text files

# re: Use new XML Features of VB to generate dynamic scripts and text files

Thursday, October 04, 2007 9:25 PM by Chan Kok Kiet

Hi,

Anyway to do the same thing in C# 2.0?

Thank you

# re: Use new XML Features of VB to generate dynamic scripts and text files

Friday, October 05, 2007 11:24 AM by Franklin Garzon

Cool, thanks, VFP is the best.

I like to know that this VFP contributing all his power inside VB, and see as VB it will turn into an extremely powerful language.

Congratulations VFP Team.

More Fox for more stronger VB.

# re: Use new XML Features of VB to generate dynamic scripts and text files

Monday, October 08, 2007 1:49 PM by Tod McKenna

Thanks for the great tip Calvin!

# Generate and run PowerShell scripts dynamically

Monday, October 08, 2007 3:14 PM by Calvin Hsia's WebLog

In my prior post Use new XML Features of VB to generate dynamic scripts and text files , we generated

# Its A Trap &raquo; Generate and run PowerShell scripts dynamically

# MSDN Blog Postings &raquo; Generate and run PowerShell scripts dynamically

# Embed Code and Avoid Underscores in Your Multiline Strings

Tuesday, October 23, 2007 3:23 PM by Beth Massi - Sharing the goodness that is VB

One of the things I most dearly missed from FoxPro when I moved to VB.NET was the ability to easily dump

# WPF Animation: Use dynamic XAML to animate movies using XAMLReadeor or animate in code using BeginStoryBoard

Wednesday, November 28, 2007 2:09 PM by Calvin Hsia's WebLog

Using WPF, it’s great to use declarative XAML , but it’s also great to use dynamic code. With VB’s new

# WPF Animation: Use dynamic XAML to animate movies using XAMLReader or animate in code using BeginStoryBoard

Wednesday, November 28, 2007 2:16 PM by Noticias externas

Using WPF, it’s great to use declarative XAML , but it’s also great to use dynamic code. With VB’s new

# MSDN Blog Postings &raquo; WPF Animation: Use dynamic XAML to animate movies using XAMLReader or animate in code using BeginStoryBoard

# Calvin Hsia&#8217;s WebLog : Use new XML Features of VB to generate dynamic &#8230; at Restaurants

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker