How to Assign and Read a Text File?

Hi,

I wish to read a text file and write into a CSV file.

I have manage to do the flow within the image below and it works fine. But this method of reading and writing into a specific CSV is hardcoded.

I want to learn and know, how to use the Assign activity properly instead of hardcoding.
Example:
ReadDirecPath = Directory.GetFiles() ← What do I input inside GetFiles for and what else can I input here ?
I know i can use Assign to assign a variable to a string, but when it comes to data manipulation or transformation, I do not know what type/data/format i can use Assign activity for.

Can anyone explain or show me a link for guidance?

HI @Jackson_Hew

Please check the below threads:

Regards

You can put the folder path from where you want to read the files and convert them to CSV.

There are multiple ways to make it dynamic.

  1. You can take input from user using input dialogue
  2. You can get input from other workflows as well.

You can learn more about the basics from UiPath Academy.

Thanks,
Ashok :slight_smile:

HI @Jackson_Hew

You can pass the Folder path into a variable and then pass that variable in between the brackets.

ReadDirecPath should be an String() → [Array of String].
The readDirecPath is array of string because it contains all the files in that for and later use and for each activity and loop that ReadDirecPath and you can go through each file.

Please refer the below steps for better understanding:

Sequence.xaml (7.5 KB)

Regards

@Jackson_Hew

What you have asked in a very broad questions

May be lets break down

You use directory.getfiles when you know the folder and you need to access multiple files or a file with varying name to be dynamically retreived

Another way would be to prompt user to give the pth or if unattended and if pth changes may be give it as an asset and pss value via asset

Coming to generating and writing back i dont think anythign changes there if somethign changes then accordingly we need to code

Depending on what manipulations you need to do there are multiple options like string split,regex,conversions etc

Hope this helps

Cheers

Here’s how you can use the Assign activity to make the workflow more flexible:

  1. Create a variable for the text file path:
  • In UiPath Studio, go to the Variables panel and click Add Variable.
  • Name the variable TextFilePath.
  • Set the variable type to String.
  1. Assign the file path to the variable:
  • Drag an Assign activity onto the workflow designer.
  • In the To field, enter TextFilePath.
  • In the Value field, enter the path to your text file. You can use double quotes (") to specify the path. For example, @"C:\Users\username\Documents\myfile.txt".
  1. Use the variable in the Read Text File activity:
  • Replace the hardcoded path in the Filename field of the Read Text File activity with the TextFilePath variable. You can use the UiPath expression builder to access variables. Just type a dollar sign ($) followed by the variable name (e.g., ${TextFilePath}).

This way, you can change the text file path by modifying the value assigned to the TextFilePath variable. You don’t need to modify the workflow itself.

  • You can use similar techniques to create variables for the output CSV file path and other options in the workflow.
  • The Directory.GetFiles() method can be used to get a list of files in a directory. You can use the output of this method to dynamically select a text file to read. However, this is beyond the scope of the screenshot you provided.
  • For more information on using the Assign activity and UiPath expressions, you can refer to the UiPath documentation on UiPath Assign activity

[CheatSheet] - Filesystem APIs - News / Tutorials - UiPath Community Forum

@vrdabberu @ashokkarale @Anil_G @jinal.shah @ppr

Thanks for the feedback and the contents!

I will look into the materials shared.

May I ask why we need to declare folder paths using Assign instead of clicking/selecting the folder path?

image

Because this activity is used for any value assignments like assigning number to a number variable, assigning string/text to a string variables.

@Jackson_Hew

there is no specific need for it…but if you give absolute path of any file inside your project then it might be an issue

as when running from orchestrator the package will be downloaded to a different location and if you select a file in the project folder then the file you selected might not be used as expected as the package file will be in a different location

for example when you develop process the project resides mostly in documents folder but when running from orchestrator the project does not reside in documents rather in the user folder under .nuget

cheers

1 Like

Code with assigned paths is easier to understand and maintain. You can see the exact location you’re working with directly in the code. Clicking activities require you to look at the designer view to understand the target folder…

  • Assigning paths allows for better error handling. You can validate the path exists before using it in your workflow. Click activities might fail silently if the target element is not found.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.