From the course: Complete Guide to UiPath RPA Development

Open and read a text file

- [Instructor] Knowing how to open and read a text file with UiPath is a foundational building block of RPA processes. There are many different kinds of text files in business processes, anything from a normal text document, to a CSV file, to even the ability to convert a PDF into a text file and then read that text representation of the PDF if you want to work with it in a different way. So let's create a new project and have a quick exercise of opening and reading a text file. I'll click on Process, give my process a name of ReadTextFile. I'll put it in my normal place of C:\DevelopmentUiPath, but you can put it wherever you want. I'll leave it as Windows and VB, and click on Create. As usual, UiPath Studio gives us a main sequence to start with. And I could just as easily delete that and use a flow chart if I wanted to. But for now, a sequence is fine. I'll click on Activities, and type in the word text. Notice, we have a bunch of options here, but down here under System, File, we have Read Text File. I'll click on that and drag it over and drop it in the center of my sequence. And right on the activity itself, you can see it's asking for a path inside of double quotes. So I could click on this Browse for file icon and search my hard drive for a file, but what I'm going to do first is go to my C drive, create a new folder called robot-files, and go inside that folder, right-click and create a new text document called open-me.txt. And if you don't see the TXT extension, that means your system isn't set up in a way to display file extensions. And I believe that's important to do. So if you don't know how to do that, I would recommend checking out my RPA Tech Primer course because I talk about topics like that in there. Briefly, what you need to do is go into View, click on Options, and Change Folder and Search Options. That's how you can get the .TXT to display. So I'm going to double-click on this and type in "This is text from a text file." And Control + S to save and close that up. So now I can either type in this path manually or I can use this folder icon to click on my C drive, double-click into robot-files, and double-click on this text file. So now as you see, I've got a fully qualified file path to open-me.txt surrounded by double quotations. We haven't talked about variables yet, but when this activity reads this file, it needs to put the information somewhere. I'll be talking about variables in the next section, but in the meantime, just follow me. Click in Output to, and right-click and select Create Variable. Type F-I-L-E, File, Contents, with no space, and hit return. So you just created a variable. And if you click down here in the Variables tab, you see the FileContents variable is a string. And the scope that that variable lives in is this thing called Main Sequence, which is right there. And we'll see it in a future lecture, but what's kind of neat is if I rename this to The process and hit return, notice that the scope of this variable is updated automatically. That's very cool. I'll click on Variables to close it. And now when I run this automation, it's going to start in this sequence. It's going to invoke this Read Text File activity, which will store the contents of the text file into this variable called FileContents. And then building upon the previous lecture, you already know that we could use a log message or a message box to display it however we want. So let's actually do both. I'll click on Activities, click on the X here, and notice we have these recent activities. So I'll click and drag log message under here. I'll click and drag message box under here. And when I click on Read Text File and select and Control + C that variable, I could either Control + V to paste that there, or if I simply wanted to open some quotes and type in the "The file contents are" colon, space, and then right arrow, space plus, notice that the FileContents variable is sitting there waiting for me. And you can see the type of it is string on that little flyout menu right there. So if I double-lick on that, and click here to select Info, this string is going to be written into the output area when we run the bot. I can use Control + A, Control + C to copy that and Control + V to paste it here. So we'll see the same thing in log message and the message box. Notice that the Read Text File activity is complaining a bit. It's saying that absolute paths are not recommended. Use a relative path instead that is not including a drive letter, yada yada. And all they're trying to do there is warn you that paths can be fragile. But if you know that path is always going to be there, for example, if the requirements say that a specific output folder should be created, then you're fine. Honestly, the way we're usually going to work is to not have explicit absolute paths like this. We would have a path like this in an orchestrator asset. And, again, we would use a Get Asset activity and assign this to a variable so that we could easily change it in Orchestrator if it ever did change. That way we're not embedding the literal string path into our robot project. But for now, this is fine. You can ignore this warning. So let's test it. I'll go ahead and click on DESIGN, Run. UiPath should disappear and come back. Actually, it shows me the message box. It says, "The file contents are: This is a text from a text file." That's good. And if I click on Output down here, you can see we get the results as expected. So that's how easy it is to read a text file. And, hopefully, you're seeing how elegant it is that UiPath allows us to simply drag and drop these activities into our canvas area here to build up automations. Of course, we need some error handling as well, but I'll talk about that in a future video.

Contents