Skip to content

Watch Command: watch

The Watch command is used to monitor a folder for specific types of files. It detects files that match given criteria, such as file type or prefix, and returns their file paths for further automation steps.

Example: Suppose you want to monitor the “C:\Reports” folder for the presence of a .pdf file that starts with a prefix “Invoice”.

Steps to configure:

  1. Select Set a Variable Value from the Action dropdown.
  2. Enter a variable name in Element Key (e.g., NewFilePath). This variable will store the path to the detected file.
  3. Click on Form, select Functions, and choose File Handler Functions.
  4. In the Cmd field, select Watch and provide these parameters:
    • Folder: Specify the folder to monitor (e.g., C:\Reports).
    • Extension: Specify the file type to watch for (e.g., pdf).
    • Prefix: Provide the prefix of the file name to match (e.g., Invoice).
    • Order By: Specify the sorting order (e.g., date).
    • Track: Set the sorting direction (e.g., “dsc” for Descending).
    • Timeout: Set the maximum wait time in seconds (e.g., 60). You can adjust it according to the expected file arrival time.
    • Recursive: Specify whether the Watch command should monitor files inside nested folders.
      • If set to true – The command will track files in the specified folder and all its subfolders.
      • If set to false – The command will only monitor files in the specified folder, ignoring subfolders.
  5. Click Save.
scrn-watch-command-tabular-view
Tabular View
scrn-watch-command-card-view
Card View

Outcome on execution:

  • The path of the first matching file detected in the folder is returned in the variable defined in the Element Key. This variable can be used in later steps of your automation process to reference the stored information using the syntax ${VariableName} (e.g., ${NewFilePath}).
  • If no file is found within the timeout period, the variable remains empty.

Watch command additional Use Cases

1. Getting the Number of Files in a Folder Using the Watch Command

The Watch command can also be used to count the number of files in a folder that meet specific criteria, such as prefix or file type. This is useful for monitoring and verifying the presence of multiple files.

Example: You want to count the number of .txt files with the prefix Report in the folder C:\Logs.

Steps to Configure:

  1. Configure the Watch Command:
    • Use the standard Watch command setup with the appropriate parameters for folder path, file extension, and prefix.
scrn-watch-command-count-number-of-files-in-folder-tabular-view
Tabular View
scrn-watch-command-count-number-of-files-in-folder-card-view
Card View
  1. Add a Step to Count the Files:
    • Add a new step and select Set a Variable Value from the Action
    • In the Element Key, define the variable as “count”. This variable will hold the count of matching files.
    • In the Parameters section, use the syntax ${TxtFiles.count}, where:
      • TxtFiles is the variable defined in the Watch command that stores the path of the first matching file detected in the folder.
      • count retrieves the count of matching files.
    •  Click Save.
screen-watch-command-step-to-count-files-tabular-view
Tabular View
screen-watch-command-step-to-count-files-card-view
Card View

Outcome on Execution:

  • The step will return the count of files matching the specified criteria in the folder.
  • The count is stored in the variable count, which can be referenced in later steps of your automation process using the syntax ${VariableName} (e.g., ${count}).

2. Getting the Number of Subfolders in a Folder Using the Watch Command

The Watch command can be used to count the number of subfolders within a specified folder. This is useful for monitoring folder structures or verifying the presence of subfolders.

Example: You want to count the number of subfolders in the folder C:\Projects.

Steps to Configure:

  1. Set Up the Watch Command:
    • Configure the Watch command with the folder path you want to monitor.
    • Leave the Extension field blank, as subfolders do not have file extensions.
scrm-watch-command-count-number-of-subfolders-tabular-view
Tabular View
scrm-watch-command-count-number-of-subfolders-card-view
Card View
  1. Add a Step to Count the Subfolders:
    • Add a new step and select “Set a Variable Value” from the Action dropdown.
    • In the Element Key, define the variable as “count”. This variable will hold the count of subfolders.
    • In the Parameters section, use the syntax ${FolderVariable.count}, where:
      • FolderVariable is the variable defined in the Watch command that stores the path of the first detected subfolder.
      • count retrieves the count of subfolders in the folder.
    • Click Save.
scrm-watch-command-count-number-of-subfolders-add-step-tabular-view
Tabular View
scrm-watch-command-count-number-of-subfolders-add-step-card-view
Card View

Outcome on Execution:

  • The step will calculate and store the count of subfolders in the specified folder in the count variable.
  • This variable can be referenced in later steps of your automation process using the syntax ${VariableName} (e.g., ${count}).

3. Monitoring all Files regardless of their Extensions

To monitor all files in a folder—regardless of their extension—you can enter files in the Extension field. This instructs the command to check for the presence of any file that includes an extension (e.g., .pdf, .txt, .jpg or any other).

Example: You want to monitor all files with the prefix Release irrespective of the file extension in the folder C:\MyFolder\.

Steps to Configure:

  1. Add a new step in your automation.
  2. From the Action dropdown, select Set a Variable Value.
  3. In the Element Key field, enter a variable name to store the path of the detected file (e.g., DetectedFilePath).
  4. Click on Form, select Functions, and choose Watch.
  5. In the Parameters section, enter values as shown below:
    • source=C:\Documents
    • extension=files
  6. Click Save.
Tabular View
Tabular View
Card View
Card View

Note: The keyword “files” apply only to files with extensions. Files without extensions and folders will not be detected using this keyword/method.

Expected Outcome on Execution:

  • The automation monitors the specified folder (e.g., C:\Documents).
  • As soon as a file with any extension and defined prefix is detected, its path is stored in the variable defined in the Element Key (e.g., DetectedFilePath).
  • You can use this variable in subsequent steps for processing, validation, or transfer using the syntax ${VariableName} (e.g., ${DetectedFilePath}).

4. Monitor All Files and Folders (Including Items Without Extensions)

Example: You want to monitor an inbound data folder that may contain subfolders and files without extensions (e.g., README, Logs, batch123).

Steps to Configure:

  1. Add a new step in your automation.
  2. From the Action dropdown, select Set a Variable Value.
  3. In the Element Key field, enter a variable name to store the detected item path (e.g., DetectedItem).
  4. Click Form, select Functions, and choose Watch.
  5. In the Parameters section, provide the following details:
    • Folder=C:\Data\Inbound
    • Extension=*
  6. Click Save.
Tabular View
Tabular View
Card View
Card View

Note: Use “*” in the Extension field to monitor everything inside the folder, including files without extensions and all subfolders.

Expected Outcome on Execution:

  • The folder C:\Data\Inbound is monitored for all types of content—files, folders, or files without extensions.
  • The path to detected sub-folders and files (even without extension type) is stored in the variable defined in the Element Field (e.g., DetectedItem)
  • This path can then be used in the automation using the syntax ${VariableName} (e.g., ${DetectedItem}).

5. Handling multiple files and folders returned in Watch Command

When the Watch command returns multiple files or folders, you can retrieve each result individually using the syntax defined below. This is especially helpful when the monitored folder contains several items that match your criteria (e.g., prefix or file extension).

HuLoop supports syntax to both count and access the detected items by their index.

Approach 1: Access Files or Folders Using Index Notation

  1. Configure the Watch Command:
    • Set up the Watch command with either * or files keyword or with any required extension in the Extension field, depending on what you want to monitor.
    • Store the output in a variable (e.g., Output).
  2. Add a Manual Step to Retrieve File Count:
    • Add a new step and toggle the Type to Manual (This step is just to display the output on the agent, not mandatory in actual automation process).
Tabular View
Tabular View
Card View
Card View
    • Enter the count syntax in Comments box: ${Output.count}

where, Output is the variable that stores the output of the watch command you configured in step 1.

This returns the total number of items detected by the Watch command. This is helpful for previewing how many files were found before you begin iteration.

wc-output

  1. Access Each File by Index:
    • Use index notation to reference each file individually in your subsequent steps:

              ${Output.0}, ${Output.1}, ${Output.2}, …

This is how it would look like:

Tabular View
Tabular View
Card View
Card View

Approach 2: Use a FOR Loop to Iterate Over Multiple Items

Instead of manually referencing each indexed file, you can loop through the results using a FOR Loop.

Click here to learn how to configure a FOR Loop in HuLoop.

This approach is especially efficient when working with a dynamic or unknown number of files.

Back To Top