by Mofi » Wed Aug 13, 2008 4:25 am
I'm better in writing macros than scripts, so here is the macro solution. You have to adapt the 3 paths according to your local requirements before you can use the macro. Short description:
The macro first opens the template file, copies whole content into user clipboard 9 and closes it immediately.
Next it opens the CSV file, verifies the existence of a line termination at end of last line and sets the cursor to start of line 2 in the file - header line is ignored.
The loop runs now until the end of the CSV file is reached.
First the current line in the CSV file is selected, copied into user clipboard 8 and unselected.
Second a new file is opened and the line from the CSV is pasted. Next the template file content is pasted from user clipboard 9 below the line from the CSV.
Now the macro replaces the variables with the data from the CSV line. It uses always the same method: select with a simple regular expression the next data and replace all occurences of the next variable with the selected text. It is very important that the number of data per line in the CSV file is always correct. The macro makes no checks here.
After replacing all variables with the data, the first CSV column - the file name - is reselected again and copied to clipboard 8. Then the line from the CSV file is deleted.
Last the new file is saved with that file name in the directory you have to specify and with the file extension PHP. That's it.
InsertMode
ColumnModeOff
HexOff
UnixReOff
Open "path\_breed-template.php"
SelectAll
Clipboard 9
Copy
CloseFile NoSave
Open "path\database.csv"
Bottom
IfColNumGt 1
InsertLine
EndIf
Top
Key DOWN ARROW
Clipboard 8
Loop
IfEof
ExitLoop
EndIf
SelectLine
Copy
EndSelect
Key HOME
NewFile
Paste
Clipboard 9
Paste
Top
Find RegExp "[~,]+"
Find MatchCase "$filename"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$breed"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$size"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$lifeexp"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$popularity"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$akcgroup"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$shedding"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$grooming"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$training"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$energy"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$affection"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$playfulness"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$watchdog"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~,]+"
Find MatchCase "$guarddog"
Replace All "^s"
EndSelect
Key RIGHT ARROW
Find RegExp "[~^r^n]+"
Find MatchCase "$sociability"
Replace All "^s"
EndSelect
Top
Clipboard 8
Find RegExp "[~,]+"
Copy
DeleteLine
SaveAs "path\^c.php"
CloseFile NoSave
EndLoop
ClearClipboard
Clipboard 9
ClearClipboard
Clipboard 0
Add UnixReOn or PerlReOn (v12+ of UE) at the end of the macro if you do not use UltraEdit style regular expressions by default - see search configuration. Macro command UnixReOff sets the regular expression option to UltraEdit style.