正则表达式 (RegEx) 是用于在指定路径中搜索文件的字符串模式。 使用正则表达式 (regex) 创建字符串模式时使用 FILE2 属性时。

例如: 如果您想在文件夹路径中搜索包含特定字符串的文件(例如,Hello.txt)。 您将使用 File2 属性类型输入以下 regex 模式: Hellell,以查找符合搜索条件的文件。 本主题基于以下位置中 Package SDK 中提供的 File2TypeDemo 示例:<PackageSDK>\src\main\java\com\automationanywhere\botcommand\samples\commands\basic\types\File2TypeDemo.java.

使用 FILE2

  1. 确保已导入以下软件包,以保证所有功能都能按照示例中的描述运行。
    导入 com.automationanywhere.commandsdk.annotations.*;导入 com.automationanywhere.commandsdk.model.AttributeType;导入 org.apache.logging.log4j.LogManager;导入 org.apache.logging.log4j.Logger;
  2. 添加 @BotCommand 注释,使类成为 Control Room 中的操作。 有关注释的更多信息,请参阅 注释
    @BotCommand
  3. 添加 @CommandPkg 注释,以定义所有与 UI 相关的组件 - 标签描述图标返回类型名称
    @CommandPkg(label = "[[File2TypeDemo.label]]", description = "[[File2TypeDemo.description]]", icon = "sample.svg", name = "file2TypeDemo")
  4. 在类中创建一个方法(例如:regexFile),并按如下所述定义字段。
    1. 使用 @Execute 对该方法进行注释,以便于在编译期间执行该方法。
    2. 在下面的示例中,@Idx@Pkg@NotEmpty 被定义为该方法的参数。 如果将这些参数用作类的成员,应使用设置获取方法对其进行定义。
      1. @Idx - 定义操作的索引。 (@Idx(index = "1", type = AttributeType.FILE2))
      2. @Pkg - 定义界面中显示的所有参数和成员变量 (@Pkg(label = "[[File2TypeDemo.localFile.label]]") )。 必须与 @Idx 一起使用,否则,此注释将被忽略。
      3. @NotEmpty - 定义该参数不能为空 (@NotEmpty @LocalFile FileValue fileValue))。
        @BotCommand @CommandPkg(label = "[[File2TypeDemo.label]]", description = "[[File2TypeDemo.description]]", icon = "sample.svg", name = "file2TypeDemo") public class File2TypeDemo { private static Logger logger = LogManager.getLogger(File2TypeDemo.class); @Execute public void regexFile( @Idx(index = "1", type = AttributeType.FILE2) @Pkg(label = "[[File2TypeDemo.localFile.label]]") @NotEmpty @LocalFile FileValue fileValue)
  5. 构建并上传自定义软件包到 Control Room。 有关上传软件包的更多信息,请参阅 使用软件包 SDK
  6. 创建机器人
  7. 将该操作拖入画布,您就可以输入 regex 以查找一个文件。 您将看到一个与此图片类似的输入框。

    该图片显示变量 SDK 演示

打印 Regex - 文件夹名称和文件位置

使用从 Log4j 软件包获取的 记录器接口来提供被操作对象的信息。 在以下示例中,您将可以使用调试 方法记录 文件夹名称本地文件位置

@BotCommand @CommandPkg(label = "[[File2TypeDemo.label]]", description = "[[File2TypeDemo.description]]", icon = "sample.svg", name = "file2TypeDemo") public class File2TypeDemo { private static Logger logger = LogManager.getLogger(File2TypeDemo.class); @Execute public void regexFile( @Idx(index = "1", type = AttributeType.FILE2) @Pkg(label = "[[File2TypeDemo.localFile.label]]") @NotEmpty @LocalFile FileValue fileValue) { if (fileValue.isRegex()) { RegexFile regexFile = fileValue.getRegex(); logger.debug("folder name {}", regexFile.getFolder()); logger.debug("local file location {}", regexFile.getFileNamePattern().toString()); } else { logger.debug("Regex option is not selected"); } }