UiPath Documentation
studiox
2023.10
false
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

StudioX user guide

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

教程:处理文件和文件夹

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

We will start by adding a File Exists activity to check if the folder where we want to download the attachments already exists. We will then add an If activity and inside it we will add a Create Folder activity to create the folder if it does not exist. If the folder does exist, we will add a Message Box activity to prompt the user to select how to proceed. Another If activity will follow with two separate sets of activities to execute depending on the user's choice - either delete the folder and recreate it or move the existing folder to another location. We will then add the activities to save the attachments from Outlook, and then we will create two folders inside the destination folder, one in which to create the text file with information about the saved files and one in which to move the files after we add the file info to the text file. To add today's date to the folder name, we will use a formula built-in to the Project Notebook.

  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. In the Activities panel, select the File tab, and then drag a Delete Folder activity and drop it inside the Then branch of the second If activity. A Delete Folder activity is added to the Designer panel.

    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. In the Activities panel, drag a Move Folder activity and drop it inside the Else branch of the second If activity below Create Folder. A Move Folder activity is added to the Designer panel.

    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. In the Activities panel, select the Mail tab, and then drag a Use Desktop Outlook App and drop it below the first If activity as the last activity in the project. The activity is added to the Designer panel.

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

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

    3. In the Activities panel, drag the For Each Email activity and drop it inside the Use Desktop Outlook App activity.

    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. In the Activities panel, drag the Save Email Attachments activity and drop it inside the For Each Email activity.

    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. In the Activities panel, drag a Create File activity and drop it below the last Create Folder.

    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. In the Activities panel, drag a Write Text File activity and drop it below the Create File activity. A Write Text File activity is added to the Designer panel.

    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. In the Activities panel, drag an Append Line activity and drop it inside the For Each File in Folder activity. An Append Line activity is added to the Designer panel.

    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. In the Activities panel, drag a Move File activity and drop it inside the For Each File in Folder activity below Append Line. A Move File activity is added to the Designer panel.

    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. Click Save in the StudioX ribbon to save the automation, then click Run to execute the automation.The attachments in the Outlook Search Folder are saved to the MailAttachments folder, a text file with information about the files is created in the Output subfolder, and then the files are moved to the Processed subfolder. If you run the project again, because the folder was already created during the first run, you are prompted to select what to do with the existing folder before the other activities are executed. Download example

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新