Check if ID exists, if yes increment / count last digit up "+1"

Hi Forum,

In a workflow I extract some Data and an ID which I want to save locally.
The ID-name needs to be the file name, but in case that the ID/file-name already exists ( for example ID = 123ABC1, last number is a digit), I want to save it as ID = 123ABC2.
The robot checks again, if that ID already exists in the filepath, if yes it chooses ID = 123ABC3 etc.

It´s adding up +1 for each loop if the name already exists.

Anyone has an idea how to solve that?
Thanks a lot!

This would be the way I would go about this:

  1. Declare a boolean ‘fileStored’
  2. Create While activity to check if the file is stored (fileStored = false)
  3. Create If activity with File.Exists(path)
    If true:
    Assign activity counter = Convert.ToInt32(ID.Substring(ID.Length-1))
    Assign activity counter = counter + 1
    Assign activity ID = ID.Substring(0,ID.Length-1) + counter.ToString

If false:
Activity to store your file (whichever one is appropriate)
Assign fileStored = True

1 Like

@evangemert thanks a lot!
looks good so far.
But is there also a way that I count up from 0/nothing to 1?
If I use ID = 123ABC1, I get 123ABC2, but If I have 123ABC, I get the error "Assign : Input string was not in a correct format.

attached my workflow
Counter.xaml (6.1 KB)

True, it only works when the last number is a digit, from your question I understood that that is the case. If it’s not the case you’ll need to build in an extra check. You can go about that several ways, but I like using regular expressions. You’d need to add the following to the true part of the if activity:

If activity with System.Text.RegularExpressions.Regex.IsMatch(ID, “\d$”) (this checks if the last character of the string is a digit)
Add the 3 assign activities you already have to the True part and add another assign activity to the False part: ID = ID + “1”

1 Like

@evangemert

thanks a lot, you saved my day! :slight_smile:
the way how to use Regex in an If-condition, I didn´t even know about it!

1 Like