Text Files in Power Automate Desktop

 

Text Files in Power Automate Desktop

One of the most common tasks in automation is reading from and writing to text files. Whether you're processing log files, storing data for later use, or generating reports, working with text files is an essential skill when building automations in Power Automate Desktop (PAD).

In this blog post, we'll guide you through the process of reading from and writing to text files in Power Automate Desktop. We'll also cover some practical examples to help you understand how to integrate file handling into your automation flows.


What Are Text Files in Power Automate Desktop?

A text file is simply a file that contains plain text, as opposed to a file that contains formatted data, like an Excel or Word document. Text files are widely used to store and transfer data due to their simplicity and universal compatibility.

Power Automate Desktop allows you to interact with text files using the following key actions:

  • Read from Text File: This action is used to read the contents of a text file.
  • Write to Text File: This action lets you write data into a text file, either by creating a new file or updating an existing one.

Common Use Cases for Reading and Writing Text Files

Here are a few scenarios where reading and writing text files can be useful:

  • Processing log files: Read error logs to extract useful information or analyze patterns.
  • Storing reports: Write data or summaries to a text file after processing it in an automation.
  • Data transfer: Extract and store information in a text format for integration with other systems.
  • Configuration files: Manage application configurations stored in plain text files.

Basic Example: Reading and Writing to a Text File

Let’s walk through a simple example where we’ll:

  1. Read data from a text file.
  2. Manipulate the data.
  3. Write the manipulated data back to the same text file.

Step 1: Read Data from a Text File

First, let's assume you have a text file called data.txt that looks like this:

John Alice Bob Charlie

We want to read this file and process each name.

  1. Add the “Read from Text File” action to your flow.
    • In the File Path field, specify the location of your text file (e.g., C:\Users\JohnDoe\Documents\data.txt).
    • Store the contents of the text file in a variable, let’s say TextFileContent.

The Read from Text File action will read the entire file content as a single string and save it in the TextFileContent variable.

Step 2: Manipulate the Data

Next, let’s say we want to add "Hello, " to each name in the file. Here’s how you can do that:

  1. Split the content of the TextFileContent variable into individual lines (names) using the Split Text action. Specify the delimiter as a newline (\n).

    • This will turn the TextFileContent into a list of names.
  2. Loop through each name and prepend "Hello, " to it.

    • Use the For Each loop to iterate through the list of names.
    • Inside the loop, use the Set Variable action to modify each name. For example, for each name, set NameWithGreeting to "Hello, " + current item.

Here’s how it might look:

plaintext
For Each Name in TextFileContent: Set NameWithGreeting = "Hello, " + Name

Now, the NameWithGreeting variable will hold each name with a greeting.

Step 3: Write Data Back to the Text File

After processing the names, we want to write the modified content back to the same text file.

  1. Add the “Write to Text File” action.
    • In the File Path field, specify the path to the data.txt file (same as before).
    • In the Text to Write field, you can join the modified names into a single string with newlines between them. Use the Join Text action to join the names in NameWithGreeting with a newline delimiter.

Example:

plaintext
Join NameWithGreeting into a single string with "\n" as delimiter Write to Text File: "data.txt"

This will overwrite the existing file with the updated content.


Advanced Example: Appending Data to a Text File

Let’s now explore how to append new data to an existing text file without overwriting the old data.

For this example, assume you have a file log.txt where you store system logs. You want to add a new log entry every time your automation runs.

Step 1: Read Existing Log Data (Optional)

If you want to review or manipulate the existing log data, you can first read the content of the file using the Read from Text File action.

  1. Use the Read from Text File action to store the existing log data in a variable like ExistingLogData.

Step 2: Append New Data

Next, let’s say you want to append a new log entry (like a timestamp or status message) to the log file. You can create a new entry and add it to the existing content.

  1. Use the Get Current Date and Time action to get the current timestamp.
  2. Combine the timestamp with the message you want to log (e.g., "Process completed successfully").
  3. Use the Append Text to File action to add the new log entry to the log.txt file.

For example:

plaintext
Get Current Date and Time Set NewLogEntry = "Timestamp: " + CurrentDateTime + " - Process completed successfully" Append Text to File: "log.txt"

This will add the new log entry to the end of the log.txt file without overwriting the previous data.


Key Actions for Reading and Writing Text Files in PAD

Here’s a quick summary of the most commonly used actions for working with text files in Power Automate Desktop:

  1. Read from Text File:

    • Reads the entire content of a text file and stores it in a variable.
    • Example use: Extract data from log files or configuration files.
  2. Write to Text File:

    • Writes a string of text to a specified file, overwriting the existing file content.
    • Example use: Save processed data or generate reports.
  3. Append Text to File:

    • Adds text to the end of a text file without overwriting the existing content.
    • Example use: Add new log entries or append new data to a file.
  4. Split Text:

    • Splits a text string into a list of substrings based on a delimiter (e.g., newline or comma).
    • Example use: Break a file’s content into individual lines or entries for further processing.
  5. Join Text:

    • Combines a list of text items into a single string with a specified delimiter.
    • Example use: Combine multiple lines or records into a single string before writing them to a file.

Conclusion

Reading from and writing to text files is a fundamental skill in Power Automate Desktop that allows you to manage data, generate reports, and interact with other systems. Whether you're working with log files, storing temporary data, or handling configurations, PAD provides the necessary tools to handle text files efficiently.

In this post, we've covered the basic actions for reading, writing, and appending to text files. We've also shown you a couple of practical examples to help you understand how to use these actions in your automations.

Now that you’re familiar with working with text files in Power Automate Desktop, give it a try in your own workflows. It’s an essential skill that will come in handy in many automation scenarios!