AutoStore: AutoCapture Folder Browse


If you're looking for an AutoCapture script to browse folders, then this post is for you. It uses a String List field to provide the folder navigation, as there isn't a tree field available in AutoCapture.

To get started, prepare an AutoStore configuration using the AutoCapture and Send to Folder components. Open the AutoCapture component, and add a new Form. Then ...



  1. Add a new field. We'll use Folder for the field Name.
  2. Set the Type option to String list.
  3. Make the field Required.
  4. Enable the Run script on field change setting.
  5. Enable the Run on form load setting.
  6. Click the [...] button to open the script editor.

Now, onto the script. Here's the short-list of what it does:
  • In the script, define the starting folder.
  • Move into a folder by selecting the item.
  • With a folder selected, the list is refreshed with any of its subfolders.
  • Navigate Back, and the list is refreshed with those folders.
  • Stops Back navigation once arrived the start path.
  • The the list value is the complete folder path.

The following sample script in its entirety is suitable for copy-and-paste. It includes everything else needed, including all the event handlers and helper methods. So, you can replace everything in your script editor's template with the following.

AutoCapture_BrowseFolder.vbs
'--------------------------------------------------------------------------------
' CONSTANTS
'================================================================================

' Reference a UNC Path or local physical disk ...
Const INITIAL_PATH = "C:\Test\Vendors"

' The Form Field Names ...
Const F_LISTFOLDER = "Folder"


'--------------------------------------------------------------------------------
' EVENT HANDLERS
'================================================================================

Sub Form_OnLoad(Form)
    If FolderExists(INITIAL_PATH) Then
        Dim strLoadName, strLoadPath, strLoadLabel, strLoadValue
        strLoadName = GetFolderName(INITIAL_PATH)

        strLoadPath = INITIAL_PATH
        strLoadLabel = strLoadName
        strLoadValue = strLoadPath
        Form.Fields.Field(F_LISTFOLDER).AddListItem strLoadLabel, strLoadValue
        
        Dim strSubFolders, arrSubFolders, strFolder
        strSubFolders = GetSubFolderPath(strLoadPath)
        arrSubFolders = Split(strSubFolders, vbCrLf)
        ForEach strFolder In arrSubFolders
            arrParts = Split(strFolder, "|")

            Dim strNextName, strNextPath, strNextLabel, strNextValue
            strNextName = CStr(arrParts(0))
            strNextPath = CStr(arrParts(1))
            strNextLabel = " > [" & strNextName & "]"
            strNextValue = strNextPath
            Form.Fields.Field(F_LISTFOLDER).AddListItem strNextLabel, strNextValue
        Next
    EndIf
EndSub


Function Form_OnValidate(Form)
    
EndFunction


Sub Field_OnChange(Form, FieldName, FieldValue)
    If FieldName = F_LISTFOLDER Then

        
        Dim strPickPath
, strPickName
        strPickPath = FieldValue
        strPickName = GetFolderName(strPickPath)
        
        Form.Fields.Field(F_LISTFOLDER).RemoveAll

        
        IfNot strPickPath = INITIAL_PATH Then

            Dim strBackPath, strBackName, strBackLabel, strBackValue
            strBackPath = CStr(GetParentFolderPath(strPickPath))
            strBackName = CStr(GetFolderName(strBackPath))
            strBackLabel = "< Back to [" & strBackName & "]"
            strBackValue = strBackPath
            Form.Fields.Field(F_LISTFOLDER).AddListItem strBackLabel, strBackValue
        EndIf
        

        Dim strCurrLabel, strCurrValue
        strCurrLabel = strPickName
        strCurrValue = strPickPath
        Form.Fields.Field(F_LISTFOLDER).AddListItem strCurrLabel, strCurrValue
        

        Dim strSubFolders, arrSubFolders, strFolder
        strSubFolders = GetSubFolderPath(strPickPath)
        arrSubFolders = Split(strSubFolders, vbCrLf)
        ForEach strFolder In arrSubFolders
            arrParts = Split(strFolder, "|")

            Dim strNextName, strNextPath, strNextLabel, strNextValue
            strNextName = CStr(arrParts(0))
            strNextPath = CStr(arrParts(1))
            strNextLabel = " > [" & strNextName & "]"
            strNextValue = strNextPath
            Form.Fields.Field(F_LISTFOLDER).AddListItem strNextLabel, strNextValue
        Next
        
    EndIf
EndSub


Function Field_OnValidate(FieldName, FieldValue)
    
EndFunction


Sub Button_OnClick(Form, ButtonName)
    
EndSub


'--------------------------------------------------------------------------------
' HELPER METHODS
'================================================================================

' Return True or False if the Folder Path exists
Function FolderExists(folderPath)
    Dim blnReturn : blnReturn = False
    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(folderPath) Then
        blnReturn = True
    EndIf
    FolderExists = blnReturn
EndFunction

' Return "Name|Path" pair as newline-delimited string
Function GetSubFolderPath(folderPath)
    Dim objFSO, objFolder, objFolderCollection, objSubFolder, strReturn
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(folderPath)
    Set objFolderCollection = objFolder.SubFolders
    ForEach objSubFolder In objFolderCollection
        ' Build "Name|Path" pair values ...
        Dim strFolderName, strFolderPath, strValuePairs, strFolderList
        strFolderName = objSubFolder.Name
        strFolderPath = objSubFolder.Path
        strValuePairs = strFolderName & "|" & strFolderPath
        ' Build newline-delimited collection string ...
        IfLen(strFolderList) = 0Then
            strFolderList = strValuePairs
        Else
            strFolderList = strFolderList & vbCrLf & strValuePairs
        EndIf
    Next
    strReturn = strFolderList
    GetSubFolderPath = strReturn
EndFunction

' Return the Folder Name
Function GetFolderName(folderPath)
    Dim objFSO, objFolder, strReturn
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.GetFolder(folderPath)
    strReturn = objFolder.Name
    GetFolderName = strReturn
EndFunction

' Return the Parent Folder Path
Function GetParentFolderPath(folderPath)
    Dim objFSO, strReturn
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    strReturn = objFSO.GetParentFolderName(folderPath)
    GetParentFolderPath = strReturn
EndFunction


Only one more thing to setup. In the Send to Folder component, add the Folder RRT to the Folder Path. The value of the Folder field is the entire path of the selected folder.



Save the configuration, and start the service. Check out the results...



In closing, the script's INITIAL_PATH constant would be defined with the starting folder path for users to browse from. Keep the relationship between the AutoCapture client and the folder destination in mind. Starting at C:\ is from the server's perspective, not the user's system.

Also, if the name Folder is too generic for one's taste, you can change it in the AutoCapture Form. but you'll also need to modify the F_LISTFIELD constant in the script to be the same. This reference is handy because a single change is reflected throughout the respective parts in the script.

Enjoy!

Comments

Popular posts from this blog

VBScript: Ensure Backslash Folder Path

AutoStore: Create a Custom RRT

AutoStore: Workflow Loop Example