Thursday | 18 APR 2024
[ previous ]
[ next ]

Open + CSV

Title:
Date: 2023-03-03
Tags:  

Table of Contents

  1. Open
  2. CSV
  3. Summary

I wrote 2 utilities for work that I'm really happy with.

Open

The first is an OPEN program. We generate reports and CSVs to a temp directory which we then e-mail out or open in the browser. During testing however, we often need to get the report using FTP or by using mounted samba shares. This isn't difficult but I always wanted to be able to open a file directly from the terminal.

I wrote a utility that lets you open universe style file paths or linux style file paths.

This means you can do something like the following:

OPEN EXPORT test.csv
OPEN /home/exprot/test.csv

The OPEN program checks if your emulator is wintegrate as it uses wintegrate specific logic to do the opening. It then checks the master dictionary if it needs to find the path of the EXPORT directory. The EXPORT entry can be a F pointer to a file path or it can be a Q pointer to another MD entry. It can also be a Type 19 file and the OPEN command will try to open it.

Once we have the path, I then combine it with the ip address of the server and then I can trigger wintegrate to run a vbscript that will open the file with a specific utility. CSVs are opened with excel, pdfs with chrome and images with the windows image viewer. Anything else will be opened with notepad.

This unfortunately only works if you are on the network, I'll need to make something similar so we can open reports and files from client machines in live and staging.

CSV

The second program is CSV, this command lets you convert a Pick LIST statement into a csv. I enjoyed making this because it is something I've been thinking and wanting for a long time. I finally read the Query manual and realized that there were some commands that were available that would get me 90% of the way there.

The way it works is that you can use the SREFORMAT command in replacement of the LIST command but instead of sending the output to the screen, it sends it to a separate file. This way you can simply loop through the file and convert each record to a comma delimited string.

It is a beautifully simple way of exporting data quickly. I originally envisioned a parser that takes a LIST command and then reads in dictionary items and generates the converted values. The issue here is that I would have also needed to write a F correlative parser and an A correlative parser. I would have to evaluate every dictionary item against a value.

By using the SREFORMAT and REFORMAT commands, the dictionaries are used to build the new file and so I don't have to do anything special.

The core logic is that given a LIST command, I create a temp file, run the SREFORMAT command and then loop through the temp file creating a csv. It ends up being quite a simple program but very useful when you want something quick and dirty.

An example snippet is:

CSV INVOICE-FILE WITH CREATE.DATE = "01JAN2023" @ID CUSTOMER.# PRODUCT PRICE INVOICE.DATE

The default output goes to the screen but you can also use the redirect to send the output to a file.

CSV INVOICE-FILE WITH CREATE.DATE = "01JAN2023" @ID CUSTOMER.# PRODUCT PRICE INVOICE.DATE > EXPORT test.csv

You can redirect to a pick style path or a Linux style path.

Summary

These two utilities work together quite well. You can quickly output information from a file and then open it all from your terminal emulator. I like this as it is very reminiscent of the unix way of doing things. I like the idea of writing some very small useful utilities that can then be composed with each other. This isn't quite that but it is similar.