New Search    ITScriptNet Home
  ITScriptNet Knowledge Base
Article ID: 10104
SAMPLE: Saving data to GS (Group Separator) Delimited file with headers using Data Processing Scripts
Sample
This sample shows how to create and append to a GS(Group Separator) Delimited file format (with headers) during processing of collected data. Code to accomplish this task must be written in VBScript because it executes in the Data Processing Scripts which only interpret VBScript Language.

This functionality can be used to generate output files of a specific format which can be used to integrate into another system.

Details
BEFORE PROCESSING DATA

Place the following code in the 'Before Processing Data' script of the Data Processing scripts (Program->Configure Receive->Edit Scripts...).
'FILE SYSTEM OBJECT AND VBSCRIPT REFERENCE
'http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/sgfsosample.asp

'CREATE FILE SYSTEM OBJECT
Set oFSO = CreateObject("Scripting.FileSystemObject")

'GET CURRENT FOLDER
Set oFolder = oFSO.GetFolder(".")

'GET THE PATH OF THE CURRENT FOLDER
sDir = oFSO.GetAbsolutePathName(oFolder)

'CLEAN UP FOLDER OBJECT
Set oFolder = Nothing

'VARIABLE DECLARATION FOR FILE OPERATIONS
ForReading = 1
ForWriting=2
ForAppending=8

'CHECK FOR FILE EXISTENCE
IF NOT oFSO.FileExists(sDir & "\GS Delimited H.txt") THEN
    SET FLAG TO WRITE HEADERS
    fWriteHeaders = true
End If

'OPEN TEXT FILE
Set oFILE = oFSO.OpenTextFile(sDir & "\GS Delimited H.txt", ForAppending, True)

IF fWriteHeaders THEN
    'WRITE HEADERS
    oFILE.Write "EMP" & Chr(29)
    oFILE.Write "LOC" & Chr(29)
    oFILE.write "ITEM" & Chr(29)
    oFILE.Write "QTY" & Chr(29)
    oFILE.Write "TERMINAL" & vbCRLF
END IF

'ADD OBJECTS TO OBJLIST (PASS TO OTHER SCRIPTS)
Set ObjList("FSO") = oFSO
Set ObjList("FILE")= oFILE


The Script Above accomplishes the following tasks:
  • Gets the Current Folder
  • Gets the FULL Path of the Current Folder
  • Checks if the file Outfile already Exists.
  • Opens Output file
  • If the File did not already exist, headers are written to the file
  • Saves oFSO and oFILE to ObjList to be used by other tswo scripts
FOR EACH RECORD

Place the following code in the 'For Each Record' script of the Data Processing scripts (Program->Configure Receive->Edit Scripts...).
'GET OBJECTS FROM OBJLIST
Set oFSO = ObjList("FSO")
Set oFILE = ObjList("FILE")

'WRITE EMP TO FILE AND THEN GS CHARACTER
oFILE.write Trim(ResponseList("Emp")) & chr(29)

'WRITE LOCATION TO FILE AND THEN GS CHARACTER
oFILE.write Trim(ResponseList("Location")) & chr(29)

'WRITE ITEM TO FILE AND THEN GS CHARACTER
oFILE.write Trim(ResponseList("Item")) & chr(29)

'WRITE QTY TO FILE AND THEN CARRIAGE RETURN AND LINE FEED
oFILE.write Trim(ResponseList("Qty")) & vbcrlf


The Script Above accomplishes the following tasks:
  • Writes Prompt value followed by a GS Character
  • Writes a Carriage Return and Line Feed Character after last prompt value
AFTER PROCESSING RECORDS

Place the following code in the 'After Processing Records' script of the Data Processing scripts (Program->Configure Receive->Edit Scripts...).
'RETRIEVE OBJECTS FROM OBJECT LIST
Set oFSO = ObjList("FSO")
Set oFILE = ObjList("FILE")

'CLOSE FILE
oFILE.Close

'CLEAN UP FILE AND SCRIPTING OBJECT
Set oFILE = Nothing
Set oFSO = Nothing
The Script Above accomplishes the following tasks:
  • Closes the output file
  • Cleans up Objects

Click here to download the files for this sample.
 
 
Please help us improve the Knowledge Base by rating this article.
Not Helpful Very Helpful
(c) 2000-2010 Z-Space Technologies, Inc. All Rights Reserved.