PPWIZARD is a free preprocessor for HTML, REXX, Visual Basic or any text files.
[Bottom][Contents][Search][Prev]: Importing fields into 3 HTML documents[Next]: #import - EXCEL via VbScript
\ -> Examples / Tips -> Importing Fields, Each Record To Its Own File

Importing Fields, Each Record To Its Own File

The follwoing shows the easiest way of solving the problem:

;--- Import the information (store in memory) ---
#NextId
#define IMPORT_#DATA TmpCsv    ;;Import to memory
#(
   #import "SomeCommaSeparatedFile.CSV" CMA ""
           "EmailAddress"      ;;CSV Field #1
           "Name"              ;;CSV Field #2
           "HomePage"          ;;CSV Field #3
#)

;--- Create a HTML page per CSV record ---
#{ FOR @@Record = 1 to <?Data:TmpCsv.?>
   ;--- Collect Information to be passed to the #included file ---
   #define+ CSV_NAME    <?Data:TmpCsv.@@Record.2>
   #define+ CSV_EMAIL   <?Data:TmpCsv.@@Record.1>
   #define+ CSV_WEBPAGE <?Data:TmpCsv.@@Record.3>

   ;--- Now create the HTML page (inline - would normally use #include) ---
   #output "out\CSV_<$CSV_NAME>.htm" ASIS
       <html>
       <head>
           <title>BASIC page for "<$CSV_NAME>"</title>
       </head>
       <body>
           ;--- Heading ------------------
           <center><h1>BASIC page for "<$CSV_NAME>"</h1></center>

           ;--- Page body ----------------
           <p><$CSV_NAME> has an email address of "<$CSV_EMAIL>" and a
           web address of "<$CSV_WEBPAGE>".

           ;--- Footer -------------------
           <hr size=1 color=red>
           <center><?CompileTime><br><$CSV_NAME></center>
       </body>
       </html>
   #output
#}

Not Using #DATA structures

I wrote this example for someone who wanted to do pretty much what you see here, each record of a CSV file creates its own HTML page. The name of the file is determined by the record number. This was written before #data structures were available.

You should also have a look at the other multiple import example. The example on this page is similar but does things in a completely different way.

;----------------------------------------------------------------------------
;    MODULE NAME:   IMPORT.IT
;
;    DESCRIPTION:   Example file of import of a comma delimited file where
;                   each record goes into its own file. The line number is
;                   used to name the file.
;
;
;                   Imported file looks like:
;                   ~~~~~~~~~~~~~~~~~~~~~~~~~
;                   Dennis,Bareis,Programmer
;                   Wendy,Buxton,Librarian
;                   Fred,Nerk,Idiot
;
;----------------------------------------------------------------------------


;--- Start and end main HTML file -------------------------------------------
<HTML>
<HEAD>
   <TITLE>MAIN FILE</TITLE>
</HEAD>
<BODY>
<H1>MAIN FILE</H1>
<P>This is a fairly basic example kept simple on purpose to hopefully
make things easier to understand.
Basically we imported a comma delimited
file (could have been fixed or other format) and generate the record into a
new html page whose name depends on the line Number.


;--- Initialize a line count ------------------------------------------------
#RexxVar RecordLineCount = 0


;--- Define HTML to start the import data files -----------------------------
#define StartsImportFiles                           \
        ;--- Open the record's file ------------   -\
        #output 'file_{$LineNum}.htm' ASIS         -\
                                                   -\
        ;--- Output Start of HTML page ---------   -\
        <HTML>                                     %\
        <HEAD>                                     %\
           <TITLE>IMPORTED: {$Desc}</TITLE>        %\
           <?PpwizardGeneratorMetaTags>            %\
        </HEAD>                                    %\
        <BODY>                                     %\
        <H1>IMPORTED: {$Desc}</H1>

;--- Define HTML to end the import data files -------------------------------
#define EndsImportFiles                             \
        ;--- Output end of html page -----------   -\
        </BODY></HTML>                             -\
                                                   -\
        ;--- Close this HTML file --------------   -\
        #output


;--- Prepare for import -----------------------------------------------------
#evaluate ''  'NL = d2c(10)'                        ;;NL = Newline Code
#define     IMPORT_PROTECT_START                    ;;We don't want imported data "protected" or #output etc won't get executed
#define     IMPORT_PROTECT_END
#define     IMPORT_HEADER                           ;;We will output our own headers
#define     IMPORT_BEFORE
#define     IMPORT_AFTER                            ;;We will output our own trailers
#DefineRexx IMPORT_RECORD_FILTER
        ;--- Which output file should contain this record? ---
        RecordLineCount = RecordLineCount + 1;

        ;--- Make sure output will go to correct file --------
        Codes =          '<' || '$StartsImportFiles LineNum="' || RecordLineCount || '" Desc="FILE #' || RecordLineCount || '">'
        Codes = Codes || NL;
        Codes = Codes || '  <P>Column 1 = ' || Column.1;
        Codes = Codes || NL;
        Codes = Codes || '  <P>Column 2 = ' || Column.2;
        Codes = Codes || NL;
        Codes = Codes || '  <P>Column 3 = ' || Column.3;
        Codes = Codes || NL;
        Codes = Codes || '<' || '$EndsImportFiles>';
        Codes = Codes || NL;
        Codes = Codes || NL;
        ThisRecordsCodes = Codes;
#DefineRexx


;--- Import the data into the 'x' files -------------------------------------
#import INPUT.CMA CMA "" "First Name" "Surname" "Job"


;--- Report on the number of files created/records imported -----------------
<P>Thats all folks, created <??RecordLineCount $$AddComma> files.

;--- Finish off HTML for "control" page -------------------------------------
</BODY></HTML>


email me  any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Search][Prev]: Importing fields into 3 HTML documents[Next]: #import - EXCEL via VbScript


PPWIZARD Manual
My whole website and this manual itself was developed using PPWIZARD (free preprocessor written by Dennis Bareis)
Sunday November 05 2017 at 10:00am