UiPath Documentation
studiox
2023.10
false

StudioX 用户指南

上次更新日期 2026年4月30日

教程:处理文件和文件夹

在本教程中,我们将创建一个自动化项目,将 Outlook 搜索文件夹中所有电子邮件的附件保存到名称中包含今天日期的文件夹中,并创建一个文本文件,其中将复制所有文件名。自动化程序将首先检查用于保存文件的文件夹是否已经存在,如果是这种情况,它将询问用户是否应删除并重新创建该文件夹,或者是否应在下载附件之前将内容移动到另一个文件夹中。

我们将首先添加“存在文件”活动,以检查要下载附件的文件夹是否已经存在。然后添加一个“If 条件”活动,并在其中添加一个“创建文件夹”活动,以便在文件夹不存在时创建该文件夹。如果该文件夹确实存在,我们将添加一个“消息框”活动,以提示用户选择如何继续。根据用户的选择,系统将随后推出另一个包含两组要执行的独立活动的“IF 条件”活动,这两组独立活动分别为删除文件夹并重新创建,及将现有文件夹移动到其他位置。然后,我们将添加活动以保存 Outlook 中的附件,然后在目标文件夹中创建两个文件夹,其中一个用于创建文本文件,其中包含已保存文件的相关信息,另一个用于在添加后移动文件将文件信息添加到文本文件。要将今天的日期添加到文件夹名称中,我们将使用项目笔记本中内置的公式。

  1. 在 Outlook 中创建搜索文件夹。

    1. 打开 Outlook,然后右键单击“文件夹”窗格中的“搜索文件夹”,然后选择“新建搜索文件夹”
    2. 在“新建搜索文件夹”窗口中,选择“创建自定义搜索文件夹” > “选择”
    3. 在“自定义搜索文件夹”窗口的“名称”框中输入“上周的附件”,然后选择“条件”
    4. 在“搜索文件夹条件”窗口的“消息”选项卡中,从“时间”旁边的下拉菜单中选择“已接收”和“上周”
    5. From the More Choices tab, select Only items with, and then one or more attachments from the adjacent drop-down menu.
    6. 单击“确定”三次以关闭窗口并保存搜索文件夹。
  2. 设置项目。

  3. 检查用于保存附件的文件夹是否存在。

    1. 在“活动”面板中,选择“文件”选项卡,然后双击“存在文件夹”。“存在文件夹”活动随即添加到“设计器”面板中。
    2. 在“文件夹存在”活动中:
      • Click Plus 文档图像 on the right side of the Folder path field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD. Click Save in the Text Builder.

      • Click Plus 文档图像 on the right side of the Save result field, and then select Save for Later Use. Give the value the name Folder 'MailAttachments' Exists and click OK

        您已指明要检查是否存在具有今天日期的文件夹 MailAttachments,并且希望保存此验证的输出结果以供以后在自动化中使用。

  4. 添加在文件夹存在和不存在时要执行的活动。

    1. 在“活动”面板中,选择“常用”选项卡,然后将“IF 条件”活动拖放到“存在文件夹”活动下方。“IF 条件”活动随即添加到“设计器”面板中。

    2. 在 If 活动中:

      • 单击“显示 Else”以添加“Else”分支。
      • Select 文档图像 Open in Advanced Editor next to the Condition field to open the Condition Builder.
    3. 在条件生成器中,添加条件“如果文件夹存在”:

      • Click Plus 文档图像 on the right side of the field on the left, and then select Use Saved Value > Folder 'MailAttachments' Exists to select the output of the Folder Exists activity as the value to evaluate in the condition.

      • 从中间的下拉菜单中,选择“为 True”,然后单击“保存”

        如果文件夹存在,将执行您在 If 活动的 Then 分支中添加的活动。如果文件夹不存在,将执行添加到 Else 分支的活动。

    4. 在“活动”面板中,选择“文件”选项卡,然后将“创建文件夹”活动拖放到 If 活动的“Else”分支中。“创建文件夹”活动随即添加到“设计器”面板中。

    5. In the Create Folder activity, click Plus 文档图像 on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD. Click Save in the Text Builder.

      您已指明,如果文件夹 MailAttachments 尚不存在,您要创建该文件夹,并且要在名称中包含今天的日期。

    6. 在“活动”面板中,选择“常用”选项卡,然后将“消息框”活动拖放到 If 活动的“Else”分支中。“消息框”活动随即添加到“设计器”面板中。

    7. Click Plus 文档图像 on the right side of the Message Box activity, and then select Text. In the Text Builder, enter a message to display to the user, for example:

      An older MailAttachments_ folder already exists. Do you want to delete the old one and create a new one? If you choose "No", the old one will be moved to a backup folder and a new one will be created.. To add the current date to the folder name, place the mouse cursor after MailAttachments_, and then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell]. Click Save in the Text Builder.

    8. 选择已添加到“设计器”面板中的“消息框”活动,然后打开其“属性”面板:

      • 从“按钮”下拉菜单中,选择“是/否”

      • Click Plus 文档图像 on the right side of the ChosenButton field, then select Save for later use and give a name for the value, for example ChosenButton from Message Box FolderAlreadyExists.

        您已表明要通过显示带有两个按钮选项“是”和“否”的消息框来提示用户选择是否应删除该文件夹(如果该文件夹已存在)。用户的选择将被保存以供以后在自动化中使用。

  5. 添加当用户选择删除任何具有相同名称的现有文件夹或移动该文件夹时要执行的活动。

    1. 在“活动”面板中,选择“常用”选项卡,然后将另一个“IF 条件”活动拖放到第一个“IF 条件”活动分支的“消息框”下方。“IF 条件”活动随即添加到“设计器”面板中。

    2. 在 If 活动中:

      • 单击“显示 Else”以添加“Else”分支。
      • Select 文档图像 Open in Advanced Editor next to the Condition field to open the Condition Builder.
    3. 在条件生成器中,添加条件“如果用户点击‘是’”:

      • Click Plus 文档图像 on the right side of the field on the left, and then select Use Saved Value > ChosenButton from Message Box FolderAlreadyExists. This selects the user's choice (the button clicked in the Message Box activity) as the value to evaluate in the condition.

      • 从中间的下拉菜单中,选择“等于”

      • Click Plus 文档图像 on the right side of the field on the right, and then select Text. In the Text Builder, enter Yes, and then click Save twice.

        如果用户点击“是”,将执行您在 If 活动的 Then 分支中添加的活动。如果用户点击“否”,将执行添加到 Else 分支的活动。

    4. “活动”面板中,选择“文件”选项卡,然后将“删除文件夹” 活动拖放到第二个“IF 条件”活动的“Then”分支中。“删除文件夹”活动随即添加到“设计器”面板中。

    5. In the Delete Folder activity, click Plus 文档图像 on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD. Click Save in the Text Builder.

      您已指明要删除名称中包含今天日期的现有文件夹 MailAttachments。

    6. 在“活动”面板中,将“创建文件夹”活动拖放到“删除文件夹”下面第二个“If”活动的 True 分支内。“创建文件夹”活动随即添加到“设计器”面板中。

    7. In the Create Folder activity, click Plus 文档图像 on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD. Click Save in the Text Builder.

      您已指明要新建名称中包含今天日期的“MailAttachments”文件夹。

    8. 在“活动”面板中,将“创建文件夹”活动拖放到第二个“IF 条件”活动的“Else”分支内。“创建文件夹”活动随即添加到“设计器”面板中。

    9. In the Create Folder activity, click Plus 文档图像 on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\old_MailAttachments, and then click Save.

      您已指明要创建一个名为 old_MailAttachments 的新文件夹。

    10. “活动”面板中,将“移动文件夹”活动拖放到“创建文件夹”下方第二个“IF 条件”活动的“Else”分支内。“移动文件夹”活动随即添加到“设计器”面板中。

    11. 在“移动文件夹”活动中:

      • Click Plus 文档图像 on the right side of the From field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD. Click Save in the Text Builder.

      • Click Plus 文档图像 on the right side of the To field, and then select Text. In the Text Builder, enter C:\old_MailAttachments, and then click Save.

        您已指明要将名称中包含今天日期的现有文件夹 MailAttachments 移动到名为 old_MailAttachments 的文件夹中。

  6. 保存 Outlook 搜索文件夹中的附件。

    1. 在“活动”面板中,选择“邮件”选项卡,然后将“使用桌面 Outlook App”活动拖放到第一个“IF 条件”活动下方,作为项目中的最后一个活动。该活动随即添加到“设计器”面板中。

    2. 在活动中,“帐户”字段中已经选择默认的电子邮件帐户。如果您要使用其他帐户,请从下拉菜单中选择。

      在“引用为”字段中,保留默认值 Outlook 作为在自动化中引用该帐户时使用的名称。

    3. 在“活动”面板中,将“遍历电子邮件”活动拖放到“使用桌面 Outlook App”活动中。

    4. In the For Each Email activity, click Plus 文档图像 on the right side of the In emails from field, and then select Outlook > [Search Folders] > [Last week's attachments] to select the search folder created in the account Outlook. Your selection is displayed in the In emails from field as "Last week's attachments".

      您已指明要遍历搜索文件夹“Last week's attachments”中的电子邮件。

    5. 在“活动”面板中,将“保存电子邮件附件” 活动拖放到“遍历电子邮件”活动中。

    6. 在“保存电子邮件附件”活动中:

      • Click Plus 文档图像 on the right side of the From message field, and then select CurrentMail to indicate that the email whose attachments you want to save is the current email you are iterating through.

      • Click Plus 文档图像 on the right side of the Save to folder field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD. Click Save in the Text Builder.

        您已指明要将附件保存到名称中包含今天日期的文件夹“MailAttachments”。

  7. 创建文本文件,在其中添加有关文件的信息,创建子文件夹,在其中保存文本文件并移动附件。

    1. 在“活动”面板中,选择“文件”选项卡,然后将“创建文件夹”活动拖放到“使用桌面 Outlook App”下面,作为该项目中最后一个活动。“创建文件夹”活动随即添加到“设计器”面板中。

    2. In the Create Folder activity, click Plus 文档图像 on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell], and then enter \Output. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD\Output. Click Save in the Text Builder.

      您已指明要在 MailAttachments 文件夹中创建一个名为 Output 的子文件夹。

    3. “活动”面板中,将“创建文件”活动拖放到最后一个“创建文件夹”下方。

    4. In the Create File activity, click Plus 文档图像 on the right side of the File location field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the Plus 文档图像 menu on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell], and then enter \Output\AttachmentsDetails.txt. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD\Output\AttachmentsDetails.txt. Click Save in the Text Builder.

      您已指明要在 Output 子文件夹中创建一个名为 AttachmentsDetails.txt 的文件。

    5. “活动”面板中,将“写入文本文件”活动拖放到“创建文件”活动下方。“写入文本文件”活动随即添加到“设计器”面板中。

    6. 在“写入文本文件”活动中:

      • Click Plus 文档图像 on the right side of the Text field, and then select Text. In the Text Builder, enter Files received in the last week:. Click Save in the Text Builder.

      • Click Plus 文档图像 on the right side of the Write to filename field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell], and then enter \Output\AttachmentsDetails.txt. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD\Output\AttachmentsDetails.txt. Click Save in the Text Builder.

        您已指明要在文件 AttachmentsDetails.txt 中输入文本“上周收到的文件:”。

    7. 在“活动”面板中,将“创建文件夹”活动拖放到“写入文本文件”活动下方。“创建文件夹”活动随即添加到“设计器”面板中。

    8. In the Create Folder activity, click Plus 文档图像 on the right side of the Folder name field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell], and then enter \Processed. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD\Processed. Click Save in the Text Builder.

      您已指明要在“MailAttachments”文件夹中创建一个名为“Processed”的子文件夹。

  8. 遍历文件,将有关每个文件的信息添加到文本文件,然后将每个文件移动到 Processed 子文件夹。

    1. 在“活动”面板中,将“遍历文件夹中的文件”活动拖放到最后一个“创建文件夹”活动下方。“遍历文件夹中的文件”活动随即添加到“设计器”面板中。

    2. In the For Each File in Folder activity, click Plus 文档图像 on the right side of the Folder field, and then select Text. In the Text Builder, enter C:\MailAttachments_, and then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell]. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD\Output. Click Save in the Text Builder.

      您已指明要迭代访问“MailAttachments”文件夹中的文件。

    3. “活动”面板中,将“附加行”活动拖放到“遍历文件夹中的文件”活动中。“附加行”活动随即添加到“设计器”面板中。

    4. 在“附加范围”活动中:

      • Click Plus 文档图像 on the right side of the Text field, and then select Text. In the Text Builder, enter FileName:, then, from the Plus 文档图像 on the right side of the Text Builder, select CurrentFile > Name, then enter | LastModifiedDate:, then, from the Plus 文档图像 on the right side of the Text Builder, select CurrentFile > Last Modified Date . The text should look like this: FileName: [CurrentFile] Name | LastModifiedDate: [CurrentFile] LastModifiedDate. Click Save in the Text Builder.

      • Click Plus 文档图像 on the right side of the Write to filename field, and then select Text. In the Text Builder, enter C:\MailAttachments_, then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell], and then enter \Output\AttachmentsDetails.txt. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD\Output\AttachmentsDetails.txt. Click Save in the Text Builder.

        您已指明要在当前遍历访问的当前文件的 AttachmentsDetails.txt 文件中输入文件名和文件的修改时间。

    5. “活动”面板中,将“移动文件”活动拖放到“遍历文件夹中的文件”活动中的“附加行”下方。“移动文件”活动随即添加到“设计器”面板中。

    6. 在“移动文件”活动中:

      • Click Plus 文档图像 on the right side of the From field, and then select CurrentFile > Full Path.

      • Click Plus 文档图像 on the right side of the To field, and then select Text, In the Text Builder, enter C:\MailAttachments_, then, from the Plus 文档图像 on the right side of the Text Builder, select Notebook > Date [Sheet] > YYYYMMDD [Cell], and then enter \Processed. The text should look like this: C:\MailAttachments_[Notes] Date!YYYYMMDD\Processed. Click Save in the Text Builder.

        您已指明要将当前遍历访问的文件移动到“Processed”子文件夹中。

    7. 在 StudioX 功能区中单击“保存” ,以保存自动化,然后单击“运行”来执行该自动化。Outlook 搜索文件夹中的附件将保存到 MailAttachments 文件夹中,系统在 "Output" 子文件夹中创建一个包含文件相关信息的文本文件,然后文件将移至“Processed”子文件夹。如果再次运行项目,则由于该文件夹是在首次运行期间创建的,因此在执行其他活动之前,系统会提示您选择如何处理现有文件夹。下载示例

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新