Interact with the local file system: Difference between revisions

From Starfish ETL
Jump to navigation Jump to search
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Read files from a folder=
<source lang="vb">
Function ScriptedVariable
Dim fso, folder, files, sFolder, res
 
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = "C:\Results"
Set folder = fso.GetFolder(sFolder)
Set files = folder.Files
For each folderIdx In files
'LogMessage folderIdx.Name
res = folderIdx.Name
Exit For
Next
If res = "" Then
EndJob "No csv file to process!"
End If
ScriptedVariable=res
End Function
</source>
=Check if a file exists=
=Check if a file exists=
<source lang="vb">
<source lang="vb">
Sub VBScriptProcedure
Sub VBScriptProcedure
If Not FileExists(AttachmentPath & "@@ORG:FILENAME@@") Then  
' For some reason, the FileExists function never works for me.  See alternative below.
' If Not FileExists(AttachmentPath & "@@ORG:FILENAME@@") Then  
' GotoNextRow
' End If
 
dim f_string
f_string = ReadFile(AttachmentPath & "@@ORG:FILENAME@@")
If f_string = "" Then
LogMessage "File Does Not Exist: @@ORG:FILENAME@@"
GotoNextRow
GotoNextRow
End If
End If
End Sub
End Sub
</source>
=Create a file=
<source lang="vb">
Function ScriptedVariable
'This script will overwrite any files with the same name.
Set objFSO=CreateObject("Scripting.FileSystemObject")
fileName="aetest.csv"
outFile="c:\temp\aetest.csv"
Set objFile = objFSO.CreateTextFile(outFile,True)
ScriptedVariable=outFile
End Function
</source>
</source>


Line 13: Line 54:
Sub VBScriptProcedure
Sub VBScriptProcedure
MoveFile "@@ORG:FullName@@","C:\uploads\@@STG:1,id@@"
MoveFile "@@ORG:FullName@@","C:\uploads\@@STG:1,id@@"
End Sub
</source>
=Read from a file=
<source lang="vb">
Function ScriptedVariable
Dim LastRunTimeStamp
LastRunTimeStamp = ReadFile("C:\inetpub\wwwroot\StarfishEngine\Xref\AccountTimestamp.ids")
'LogMessage LastRunTimeStamp
ScriptedVariable = LastRunTimeStamp
End Function
</source>
=Write to a file=
<source lang="vb">
Sub VBScriptProcedure
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Note the second parameter is using a Constant set above.  In this case, I want to append to the file.
Set MyFile = fso.OpenTextFile("@@VAR:filename@@", ForAppending, True)
' Write to the file.
MyFile.WriteLine "@@ORG:id@@|@@ORG:name@@|@@ORG:billing_address_street@@|@@ORG:billing_address_city@@|@@ORG:billing_address_state@@|@@ORG:website@@"
MyFile.Close
End Sub
</source>
or
<source lang="vb">
Sub VBScriptProcedure
If Not PreviewMode Then
WriteFile "C:\inetpub\wwwroot\StarfishEngine\Xref\AccountTimestamp.ids", "@@ORG:numTimeStamp@@"
End If
End Sub
End Sub
</source>
</source>

Latest revision as of 18:33, 27 August 2018

Read files from a folder

Function ScriptedVariable
	Dim fso, folder, files, sFolder, res
  
	Set fso = CreateObject("Scripting.FileSystemObject")
	sFolder = "C:\Results"
	Set folder = fso.GetFolder(sFolder)
	Set files = folder.Files 
	For each folderIdx In files
		'LogMessage folderIdx.Name
		res = folderIdx.Name
		Exit For
	Next
	If res = "" Then
		EndJob "No csv file to process!"
	End If
	ScriptedVariable=res
End Function

Check if a file exists

Sub VBScriptProcedure
'	For some reason, the FileExists function never works for me.  See alternative below.
'	If Not FileExists(AttachmentPath & "@@ORG:FILENAME@@") Then 
'		GotoNextRow
'	End If

	dim f_string 
	f_string = ReadFile(AttachmentPath & "@@ORG:FILENAME@@")
	If f_string = "" Then 
		LogMessage "File Does Not Exist: @@ORG:FILENAME@@"
		GotoNextRow
	End If
End Sub

Create a file

Function ScriptedVariable
	'This script will overwrite any files with the same name.
	Set objFSO=CreateObject("Scripting.FileSystemObject")
	fileName="aetest.csv"
	outFile="c:\temp\aetest.csv"
	Set objFile = objFSO.CreateTextFile(outFile,True)
	ScriptedVariable=outFile
End Function

Move and rename a file

I imported files directly into a Sugar instance's database. The file had to be named the Knowledge Base Article's ID. I moved and renamed the file as the KB article was create:

Sub VBScriptProcedure
	MoveFile "@@ORG:FullName@@","C:\uploads\@@STG:1,id@@"
End Sub

Read from a file

Function ScriptedVariable
	Dim LastRunTimeStamp
	LastRunTimeStamp = ReadFile("C:\inetpub\wwwroot\StarfishEngine\Xref\AccountTimestamp.ids")
	'LogMessage LastRunTimeStamp
	ScriptedVariable = LastRunTimeStamp
End Function

Write to a file

Sub VBScriptProcedure
	Const ForReading = 1, ForWriting = 2, ForAppending = 8
	Dim fso, MyFile
	Set fso = CreateObject("Scripting.FileSystemObject")
	'Note the second parameter is using a Constant set above.  In this case, I want to append to the file.
	Set MyFile = fso.OpenTextFile("@@VAR:filename@@", ForAppending, True)

	' Write to the file.
	MyFile.WriteLine "@@ORG:id@@|@@ORG:name@@|@@ORG:billing_address_street@@|@@ORG:billing_address_city@@|@@ORG:billing_address_state@@|@@ORG:website@@"
	MyFile.Close
End Sub

or

Sub VBScriptProcedure
	If Not PreviewMode Then
		WriteFile "C:\inetpub\wwwroot\StarfishEngine\Xref\AccountTimestamp.ids", "@@ORG:numTimeStamp@@"
	End If
End Sub