Creating variables based on number of pdf files in a folder

I’m trying to figure out if I can have UiPath create variables for me based on 2 other variables.

I’m building a project to pull all the pdf’s in a folder containing certain words. From there I need to pull fields and make them into variables… The issue is that sometimes there could be 1, 2, 3, etc. or even none. Is there a way to have it create the variables for me? My thought would be to create a variable based on the file name and the field.

I’ve assigned the file name to a variable and also the field to a variable. But I think if there’s 2 or more files it will overwrite itself and I lose the data from the previous variables.

I need to pull the data from these fields and then use all of the data to input to a pdf field at the end.

Hi @Tim_Corwin

I’m afraid you cannot create variables dynamically (it would mean changing the xaml code in real time while it’s being executed). Even if you could do that that could be dangerous.

What I’d suggest is to use Dictionary<String, String> or <String, Object> if you your variables may differ in type.

Then you can simply assign the variables into the dictionary to the different keys.
In assign activity to initialize a new dictionary of <string, string>
dict = new Dictionary<String, String>

then in assign activity to assign key-value pairs
dict("file1_field1") = something1
dict("file1_field2") = something2
dict("file2_field1") = something3

then if you need to use the variables you can either loop through the dictionary or call it by the key.

Then you can use for example log activity with
dict("file1_field2")
and it will log
something2

I’m starting to get the dictionary set up but I’m still having an issue. How do you loop through the dictionary?

As explained above, I essentially have a form which would contain a Name and their Relationship. Sometimes the individual doesn’t require the form and other times it could be one or even a hundred. I’ve got the dictionary setup, but I think it’s still overwriting the value… or I’m not outputting it correctly.

I’m pulling the values from the PDF file and assigning them to a variable. I use that variable to add the two fields into dictionary. It then loops to the next form (if there is one). When I output it to the final pdf though it only gives me one set of the variables I pulled (1 name and 1 relationship). I want to be able to output all the names and relationships on a single pdf field.

@Tim_Corwin

Hey, it’s pretty easy. If you are assigning the key-value pairs correctly all you need to do is to use For Each loop with the TypeArguments set as KeyValuePair<String, String>.

To get the key you need to use currentItem.Key
To get the value you need to use currentItem.Value


image

I finally got it! Is there a way to manually write out the for each code into a “Type Into” activity? I got it to print out correclty to a log message as you showed me above. I now want to do the same thing, but have it output to a Type Into Activity. I’m not sure how to declare the type argument for it though so I’m not using the properties.

@Tim_Corwin

If you want to use one Type Into, you can simply replace the LogMessage with TypeInto and put the currentItem.Value inside the TypeInto activity.

If you have more than one TypeInto and you know conditions on when to write to which, you should ether use If, IfElse or Switch activities so depending on the key or value you use different TypeInto.

I actually got it! Thank you so much for the help.

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