How to set up a "Before Connect" macro to delete the contents of a folder at Host session launch time

  • 7023500
  • 06-Nov-2018
  • 08-Aug-2019

Environment

Reflection Desktop (including Pro, for X, for IBM, or for UNIX and OpenVMS) 16.0 or higher

Situation

This article describes how to set up a "Before Connect" macro or action to delete the contents of a folder on the local drive at Host session launch time.  There may be situations where deleting the contents of a local folder may be necessary before performing some action in the Reflection Desktop Host session, which may include preparing a local folder to receive files downloaded from the Host or deleting unneeded configuration files.

Resolution

The following example shows how to set up a “Before Connect" macro which will to delete the contents of the users .pki folder, each time a Reflection host session loads.

1. Open the VBA Editor.

2.  Select the Project() section.

3.  Expand the Modules section below the Projects section.

4. Right-click and from the context menu select “Insert†then “Moduleâ€

5. In the right pane of the Visual Basic Editor insert the following code:

    Sub Delete_PKI_Cache()
        ‘ this macro will delete all files in the .pki folder for the current user
        Dim UserName As String
        Dim FolderName As String
        UserName = Environ("USERNAME")
        FolderName = "C:\Users\" & UserName & "\Documents\Micro Focus\Reflection\.pki\"
        Kill FolderName & "*.*"
    End Sub

6. Perform a File Save and then exit the Visual Basic Editor.

7. In the Reflection Host session, under “Configure Advanced Connection Settingsâ€, scroll to the bottom and check the box in front of “Run a macro or other action before the initial connectionâ€.

8. Press the “Select Action…†button.

9. Choose the “Select macro†radio button.

10. Select the macro called “Delete_PKI_Cacheâ€.

11. Press OK and save the Reflection host session.

12. Close the Reflection Host session.

13. Open the Reflection host session and the contents of the \.pki folder will be deleted BEFORE a host connection is attempted.

Additional Information

This is a VBA macro that will work for any location of the users My Documents location including redirected MyDocs and will also only clear the .pki folder for the first Host session opened in the Reflection Workspace, but not subsequent Host sessions opened:

Sub Clear_CRL_Cache_only_on_First_Host_Session_Open()
    On Error Resume Next

    ' Declare object variables for the Application and Frame and View Objects
    Dim app As Attachmate_Reflection_Objects_Framework.ApplicationObject
    Dim f As Attachmate_Reflection_Objects.Frame
    Dim myView As Attachmate_Reflection_Objects.View
 
    ' Get a handle to the Application object
    Set app = GetObject("Reflection Workspace")
   
    ' Get the Frame object and the number of views
    Set f = app.GetObject("Frame")

    ' If only one Host session is open, and before the Connect,
    ' then delete all files in the .pki folder for the current user
    If f.ViewCount = 1 Then
        Dim UserProfile As String
        Dim FolderName As String
        UserProfile = Environ("USERPROFILE")
        MsgBox UserProfile
        FolderName = UserProfile & "\Documents\Micro Focus\Reflection\.pki\"
        MsgBox FolderName
        Kill FolderName & "*.*"
    End If
End Sub