Oh My God what is this??

BlitzPlus Forums/BlitzPlus Programming/Oh My God what is this??

People I am adding the whole thing, Sorry to do this but I am well and truly stuck on this.

The application will sit in the background on a NT pc and check a directory on a unix file server every 30 seconds or so.
If it finds a file with a .CTA extension it will read the file and load some variables from it into a custom type called DataCatalogue.

The point behind the program is to sit in the background and as the files/tasks are processed to move them to the relevant user directorys back in unix.

The .CTA file is a text file created in a CAD package, The code reads its variables from there:-
SpoolNO, SpoolCPC,SpoolDIR and SpoolPRJ.

P.S the save database function seems to be fine??.

.TOP
; ************************************************************************************************
; ****                PipeFAB BatchLoad Application V1.0 11/11/2003 LeeLuna                   ****
; ****                                                                                        ****
; ****                This Application is designed to BatchLoad Isometric CADDS 5             ****
; ****                CTA files from a shared UNIX directory into PipeFAB                     ****
; ****                software.                                                               ****
; ****                                                                                        ****
; ****                Program Steps:-                                                         ****
; ****                                                                                        ****
; ****                  Start as a background task in WindowsNT                               ****
; ****                  Load Files.DataCatalogue from Seq File                                ****
; ****                                                                                        ****
; ****                                                                                        ****
; ****                	Check the users\project\files\_bcd directory for *.CTA files          ****
; ****                         If New File                                                    ****
; ****                  		  Load Files.DataCatalogue with file information              ****
; ****                              CPC Number                                                ****
; ****                              Return Directory                                          ****
; ****                              Spool Number                                              ****
; ****                              Project Number                                            ****
; ****                  IMPORTED = When file has been processed by PipeFAB                    ****
; ****                  RPT      = Report file returned by PipeFAB                            ****
; ****                  MBOM     = Manufacture Bill Of Material file created by PipeFAB       ****
; ****                  TIF      = Tiff image of isometric drawing saved by user              ****
; ****                                                                                        ****
; ****                                                                                        ****
; ****                  Load cta files into PipeFAB with ExecFile() command                   ****
; ****                                                                                        ****
; ****                                                                                        ****
; ****                  Check C:\BatchLoadACP directory for *.IMPORTED files                  ****
; ****                      Update Files.DataCatalogue with file information                  ****
; ****                  Check C:\BatchLoadACP directory for *.RPT files                       ****
; ****                      Update Files.DataCatalogue with file information                  ****
; ****                  Check C:\BatchLoadACP directory for *.MBOM files                      ****
; ****                      Update Files.DataCatalogue with file information                  ****
; ****                  Check C:\BatchLoadACP directory for *.TIF files                       ****
; ****                      Update Files.DataCatalogue with file information                  ****
; ****                  If IMPReturned+RPReturned+MBOMReturned+TIFReturned=4                  ****
; ****                      Delete record from Files.DataCatalogue                            ****
; ****                                                                                        ****
; ****                  	                                                                  ****
; ****                                                                                        ****
; ****                      Check through Files.DataCatalogue                                 ****
; ****                      If SpoolIMPORTED>0 and IMPReturned=0                              ****
; ****                          Return *.IMPORTED to SpoolDIR$                                ****
; ****                          Set IMPReturned=1                                             ****
; ****                      If SpoolRPT>0 and RPTReturned=0                                   ****
; ****                          Return *.RPT to SpoolDIR$                                     ****
; ****                          Set RPTReturned=1                                             ****
; ****                      If SpoolMBOM>0 and MBOMReturned=0                                 ****
; ****                          Return *.MBOM to SpoolDIR$                                    ****
; ****                          Set MBOMReturned=1                                            ****
; ****                      If SpoolTIF>0 and TIFReturned=0                                   ****
; ****                          Return *.TIF to SpoolDIR$                                     ****
; ****                          Set TIFReturned=1                                             ****
; ****                  	                                                                  ****
; ****                  Save Files.DataCatalogue Too Seq File                                 ****
; ****                  	                                                                  ****
; ************************************************************************************************
;
;
;                *** MAIN LOOP ***
; Make CTA global so all functions can see it.
Global cta.DataCatalogue
Const WaitTime% = 30 ; 30 Seconds
Const Folder$ = "C:\PipeFAB\PipeFAB\BatchLoad" 
;                                                 This needs to change to whatever the users files
;                                                 directory is
;                                                 can this be read from a redgistry variable?
;                                                 or be part of the startup script?
;
;
Main()
;  Main Loop
;
Function Main()
LoadDatabase()
BringFiles()
win=CreateWindow ("",0,0,300,300);create window first
txtbox=CreateTextArea(0,0,200,200,win) ;create textarea in that window
CreateTimer(1)
Repeat
	Select WaitEvent()
		Case $4001 ; Timer tick (one second) has occurred
			ticks% = ticks + 1
			; if WaitTime has passed then check the folder for *.CTA, *.RPT files
			;                                                  *.MBOM, *.TIF files
			;                                                  and *.MPORTED files.
			If ticks Mod WaitTime = 0
				CheckFolder(Folder)
				;
				; Just for testing, the code below prints to a text area in a window.
				;
				For cta.DataCatalogue = Each DataCatalogue
					AddTextAreaText txtbox,cta\Name+">"+cta\SpoolCPC+">"+cta\SpoolDIR+">"+cta\SpoolNO+Chr$(13)
				Next
			EndIf
		Case $803
			SaveDatabase()
			ClearDataCatalogue()
			End
	End Select
Forever
End Function
Type DataCatalogue
	Field Name$				; CTA File Name
	Field SpoolCPC$			; This field will contain the spool CPC Number, It could be a string!!
	Field SpoolDIR$			; This field will contain the return directory for our files
	Field SpoolNO$			; This field will contain the spool number, E.G. 0123HPA
	Field SpoolPRJ$			; This field will contain the spool project number, E.G. ACPVS.
	Field SpoolIMPORTED		; Has it been imported, 0=NO 1=YES
	Field SpoolRPT			; Has it had a report created, 0=NO 1=YES
	Field SpoolMBOM			; Has it had a MBOM created, 0=NO 1=YES
	Field SpoolTIF			; Has it had a TIFF created, 0=NO 1=YES
	Field IMPReturned		; Has the IMPORTED file been returned to SpoolDIR$, 0=NO 1=YES
	Field RPTReturned		; Has the RPT file been returned to SpoolDIR$, 0=NO 1=YES
	Field MBOMReturned		; Has the MBOM file been returned to SpoolDIR$, 0=NO 1=YES
	Field TIFReturned		; Has the TIF file been returned to SpoolDIR$, 0=NO 1=YES
End Type
;CheckFolder() This function performs a recursive check of our directory for files ending in.CTA
; 			   Thanks to SoJa from the BlitzBasic.com community section for the help
;			   with this function.
Function CheckFolder(Folder$)
	; Find all CTA files in the folder
	dir = ReadDir(Folder)
	While MoreFiles(dir)
		; Found a file.  Is it CTA file?
		; Maybe change this to a select
		; on the file extension .
		file$ = NextFile$(dir)
		If Upper$(Right$(file$, 4)) = ".CTA" Then CheckCTA(file$)
		If Upper$(Right$(file$, 4)) = ".RPT" Then CheckRPT(file$)
		If Upper$(Right$(file$, 9)) = ".IMPORTED" Then CheckIMPORTED(file$)
		If Upper$(Right$(file$, 5)) = ".MBOM" Then CheckMBOM(file$)	
		If Upper$(Right$(file$, 4)) = ".TIF" Then CheckTIF(file$)			
	Wend
	CloseDir(dir)
End Function
;CheckCTA() This function checks the filename of a found CTA file against against our cta database
; 			   Thanks to SoJa from the BlitzBasic.com community section for the help
;			   with this function.
Function CheckCTA(file$)
	; Check to see if we've seen this CTA file before
	For cta.DataCatalogue = Each DataCatalogue
		If cta\Name = file$ Then Return
	Next
	; It's a new one
	cta.DataCatalogue = New DataCatalogue
	cta\Name = file$
	ReadCTA(file$)
End Function
;ReadCTA() Open the cta file and find the values for our fields.
;          Thanks to MasterBeaker, Koriolis and Floyd from the BlitzBasic.com community for 
;		   help on this Function.
Function ReadCTA(file$)
;	Name$				; Added in the CheckCTA() Function
;	SpoolCPC$			; ReadLine and find "(STLNUMMER=" value is 8 characters from this point.
;	SpoolDIR$			; ReadLine and find "## " Directory is the rest of the line, trim trailing spaces.
;	SpoolNO$			; ReadLine and find "(MSECTION=" value is from this point and closing ")" after.
	FoundFile=ReadFile(Folder$+""+file$)
	While Not Eof(FoundFile)
		GotLine$=ReadLine(FoundFile)
		Select True
			Case Instr(GotLine$,"(STLNUMMER=",1)>0
				cta\SpoolCPC=Mid$(GotLine$,Instr(GotLine$,"(STLNUMMER=",1)+11,8)
			Case Instr(GotLine$,"## ",1)>0
				cta\SpoolDIR=Mid$(GotLine$,Instr(GotLine$,"## ",1)+14,Len(GotLine$)-14)
			Case Instr(GotLine$,"(MSECTION=",1)>0
				cta\SpoolNO=Mid$(GotLine$,Instr(GotLine$,"(MSECTION=",1)+10,Len(GotLine$)-11)
			Case Instr(GotLine$,"(PROJEKT=",1)>0
				cta\SpoolPRJ=Mid$(GotLine$,Instr(GotLine$,"(PROJEKT=",1)+9,Len(GotLine$)-10)				
		End Select
		cta\SpoolIMPORTED=0:cta\SpoolMBOM=0:cta\SpoolRPT=0:cta\SpoolTIF=0
		cta\IMPReturned=0:cta\RPTReturned=0:cta\MBOMReturned=0:cta\TIFReturned=0
Wend
CloseFile(FoundFile)
;
; Process the cta file through PipeFAB Application.
; This will automatically generate the .IMPORTED file and the .RPT file
;
;ExecFile("command /c c:\pipefab\pipefab\run\isoinput.exe pipefab/pipefab@ttdb1 appluser=des password=des workproject="+cta\SpoolPRJ+" cadconstdimportbatch=ON inputfile=%1 CADCONSTD_SAVE_AFTER_IMPORT=1 USERCALL_AFTERIMPORT=EXTCALLBATCH_AFTERIMPORT")
;ExecFile("command /c rename "+cta\Name+" "+cta\SpoolCPC+".IMPORTED")
;ExecFile("command /c rename report.txt "+cta\SpoolCPC+".RPT")

End Function
; SaveDatabase() Saves the custom type .DataCatalogue to disk.
Function SaveDatabase()
	OutFile=WriteFile("C:\LeeTest\CTACAT.txt")
	For cta.DataCatalogue = Each DataCatalogue
		WriteString(OutFile,cta\Name)
		WriteString(OutFile,cta\SpoolCPC)
		WriteString(OutFile,cta\SpoolDIR)
		WriteString(OutFile,cta\SpoolNO)
		WriteString(OutFile,cta\SpoolPRJ)		
		WriteInt(OutFile,cta\SpoolIMPORTED)
		WriteInt(OutFile,cta\SpoolRPT)
		WriteInt(OutFile,cta\SpoolMBOM)
		WriteInt(OutFile,cta\SpoolTIF)
		WriteInt(OutFile,cta\IMPReturned)
		WriteInt(OutFile,cta\RPTReturned)
		WriteInt(OutFile,cta\MBOMReturned)
		WriteInt(OutFile,cta\TIFReturned)		
	Next
	CloseFile(OutFile)
End Function
; LoadDatabse() Loads the custom type .DataCatalogue from disk.
Function LoadDatabase()
	OutFile=ReadFile("C:\LeeTest\CTACAT.txt")
	While Not Eof(OutFile)
		cta.DataCatalogue = New DataCatalogue
		cta\Name=ReadString(OutFile)	
		cta\SpoolCPC=ReadString(OutFile)	
		cta\SpoolDIR=ReadString(OutFile)
		cta\SpoolNO=ReadString(OutFile)
		cta\SpoolPRJ=ReadString(OutFile)		
		cta\SpoolIMPORTED=ReadInt(OutFile)	
		cta\SpoolRPT=ReadInt(OutFile)	
		cta\SpoolMBOM=ReadInt(OutFile)	
		cta\SpoolTIF=ReadInt(OutFile)	
		cta\IMPReturned=ReadInt(OutFile)	
		cta\RPTReturned=ReadInt(OutFile)	
		cta\MBOMReturned=ReadInt(OutFile)	
		cta\TIFReturned=ReadInt(OutFile)			
	Wend
	CloseFile(OutFile)	
End Function
; DelRecords() Deletes the record after all the files have been returned to the users directory.
Function DelRecords()
	; Check to see if all the files have been returned
	For cta.DataCatalogue = Each DataCatalogue
		If cta\IMPReturned=1
			If cta\RPTReturned=1
				If cta\MBOMReturned=1
					If cta\TIFReturned=1
						Delete cta
					EndIf
				EndIf
			EndIf
		EndIf
	Next
	SaveDatabase()
End Function
; Deletes all the types at the end to free memory back to the system.
Function ClearDataCatalogue()
	For cta.DataCatalogue = Each DataCatalogue
		Delete cta
	Next
End Function
; BringFiles() Collects the files from the user batch directory to the PipeFAB\BatchLoad directory
Function BringFiles()
	ExecFile("command /c MOVE c:\LeeTest\*.cta c:\pipefab\pipefab\batchload")
End Function
; SendFiles() Sends the files to the required directory after they have been created.
;    		  in the PipeFAB\BatchLoad directory.
Function SendFiles(Extension$)
	Select Extension$
		Case ".IMPORTED"
			CopyFile folder$+cta\SpoolCPC+".IMPORTED",cta\SpoolDIR+cta\SpoolCPC+".IMPORTED"
			DeleteFile folder$+cta\SpoolCPC+".IMPORTED"
		Case ".MBOM"
			CopyFile folder$+cta\SpoolCPC+".MBOM",cta\SpoolDIR+cta\SpoolCPC+".MBOM"
			DeleteFile folder$+cta\SpoolCPC+".MBOM"
		Case ".RPT"
			CopyFile folder$+cta\SpoolCPC+".RPT",cta\SpoolDIR+cta\SpoolCPC+".RPT"
			DeleteFile folder$+cta\SpoolCPC+".RPT"
		Case ".TIF"
			CopyFile folder$+cta\SpoolCPC+".TIF",cta\SpoolDIR+cta\SpoolCPC+".TIF"
			DeleteFile folder$+cta\SpoolCPC+".TIF"
		Default ; do nothing	
	End Select
;	DelRecords() ; check to see if we can remove any records from the database e.g all returned.
End Function
;
; CheckRPT() if the file extension is .RPT set flags and call sendfiles().
Function CheckRPT(file$)
		For cta.DataCatalogue = Each DataCatalogue
			If Left$(file$,Instr(file$,".",1)-1) = cta\SpoolCPC
				cta\SpoolRPT=1
				cta\RPTReturned=1
				SendFiles(".RPT")
			EndIf
		Next		
End Function
;
; CheckIMPORTED() if the file extension is .IMPORTED set flags and call sendfiles().
Function CheckIMPORTED(file$)
		For cta.DataCatalogue = Each DataCatalogue
			If Left$(file$,Instr(file$,".",1)-1) = cta\SpoolCPC
				cta\SpoolIMPORTED=1
				cta\IMPReturned=1
				SendFiles(".IMPORTED")
			EndIf
		Next
End Function
;
; CheckMBOM() if the file extension is .MBOM set flags and call sendfiles().
Function CheckMBOM(file$)
		For cta.DataCatalogue = Each DataCatalogue
			If Left$(file$,Instr(file$,".",1)-1) = cta\SpoolCPC
				cta\SpoolMBOM=1
				cta\MBOMReturned=1
				SendFiles(".MBOM")
			EndIf
		Next
End Function
;
; CheckTIF() if the file extension is .TIF set flags and call sendfiles().
Function CheckTIF(file$)
		For cta.DataCatalogue = Each DataCatalogue
			If Left$(file$,Instr(file$,".",1)-1) = cta\SpoolCPC
				cta\SpoolTIF=1
				cta\TIFReturned=1
				SendFiles(".TIF")
			EndIf
		Next
End Function
;
;
.BOTTOM


Thanks in advance
Luna.
Any help will no doubt save my hair.