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.
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:
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:
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.
So at this point we could reference the lines of the file with the following variable references:
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.
In the post-execution for the Read_From_File operator we will use the following code:
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.
So now the process should look like this:
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:
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:
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:
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:
Now, take a look at the dataset.
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.
The process will run and stop again at the breakpoint. Take a look at the dataset again:
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.