Steps to create a process that will read lines in a text file and pass each line separately to other operators

Document ID : KB000049376
Last Modified Date : 14/02/2018
Show Technical Document Details

Description:

This tech doc will walk through the creation of a process to read lines of text from a text file and then pass those lines of text individually to other operators in the process. In this example the text file contained server names. We may want to perform some operation on each of the servers listed in this file.

Solution:

Start with the "Read From File" operator and make sure that is configured correctly. For this example there is a file called test.txt located on the C drive of the ITPAM orchestrator. Place the value of the file name in the "Source File Name" parameter of the Read_From_File Operator as follows:

Figure 1

Check the "Return file contents in a string array" to force ITPAM to separate out the lines in the text file for you. Fill out the working directory, user id and password for the machine containing the test.txt file (the orchestrator machine) as follows:

Figure 2

Save this process and run it. Right click on the Read_from_File operator and select "Dataset". Then click on the Operation Results. You will see the following. Notice the DatasetVariable has the 3 lines from the text file in an array.

Figure 3

So at this point we could reference the lines of the file with the following variable references:

Process.Read_from_File_1.DatasetVariable[0]
Process.Read_from_File_1.DatasetVariable[1]
Process.Read_from_File_1.DatasetVariable[2]

But in this example we want to reference each of these server names later in an operator. To do this, we will create a loop that will reference a different line of the file for each run of the loop and therefore a different servername.

We start by defining two dataset variables or parameters. The first will be i which will be our server counter, and the second will be current_servername so that in this example we can see what the current servername is in each loop run.

Figure 4

In the post-execution for the Read_From_File operator we will use the following code:

Process.line_count=Process.Read_from_File_1.DatasetVariable.length;
Process.i=0;

The first line defines a variable called "line_count" that is set to the length of the results from the Read_from_File operator. This is the same as the number of lines in the text file. The second line initializes the i parameter to be 0.

Then we will add a loop operator and 2 calculation operators inside the loop. The loop parameter "Repeat Count" will be set to the Process.line_count which we set above in the Read_from_File_1 post-execution.

Figure 5

So now the process should look like this:

Figure 6

So now to set the source code for each of the calculation operators inside the loop.

For the first calculation operator which is now named "Set_Current_Servername", we will use the following source code:

Process.current_servername=Process.Read_from_File_1.DatasetVariable[i];

So we are giving the process variable "current_servername" the value of the first line from the file or the first item in the result array. Notice the i variable is used here so that each time the i variable changes, so will the server name.

For the second calculation operator which is now named "Increase_loop_counter", we will use the following source code:

Process.i++;

This simply increases the i variable by 1.

And finally for demonstration purposes, we will set a breakpoint on the "Increase_loop _counter". To do that, click on the "Increase_loop_counter" operator and then click on the hand icon in the toolbar. You should now see the hand next to your "Increase_loop_counter" operator as follows:

Figure 7

Now we will run the process. The first 2 operators should run and then when it hits the breakpoint it should stop. So it will look like this:

Figure 8

Now, take a look at the dataset.

Figure 9

You can see that the i parameter has been initialized to 0, the line_count returned 3 and the current_servername was set to the first item in the array.

Now, back on the Main Editor tab, click the Resume Process button in the toolbar. Figure 10

The process will run and stop again at the breakpoint. Take a look at the dataset again:

Figure 11

Notice the current_servername has changed to the second line in the file.

You can resume the process again and see the 3rd servername appear. Then resume again and the loop will complete because the line_count has been reached.

So in a real world example you would not use the breakpoint and the calculation operator called "Set_Current_Servername" might be replaced with and operator to get the disk space back from this server. So one run of this process would return disk space from each server in the text file.