Using a starting number with UniqueCounter

  • 1.3K Views
  • Last Post 11 June 2024
  • Topic Is Solved
patrick.loxhay posted this 24 October 2019

Hi

I have been using the %UNIQUECOUNTER% variable and find that it does not populate with a number unless a file already exists in the output folder location.  For example, if I had a file name that was Document_%UNIQUECOUNTER% it would write the first file name as Document_  .  I have tried the setting to dictate the starting number and length of the unique counter i.e %UNIQUECOUNTER(1+1)% but that did not make any difference.

Is there a way of getting the Unique Counter to have a starting number regardless of a file name already existing? 

 

  • Liked by
  • TimWagner
Order By: Standard | Newest | Votes
luigi.zurolo posted this 25 October 2019

Hi Patrick,

 

correct, this behavior has been standard for the simple reason that the unique counter has meaning when the file already exists and it starts counting to avoid to overwrite it.

However if you use the latest internal build available additional parameters have been added to the variable so that you can also customize this option, the counter now works like below:

%UNIQUECOUNTER% = initially not existing when file doesn't exist
%UNIQUECOUNTER3% = initially not existing when file doesn't exist, it is formatted by x (3) digits (e.g. 000)
%UNIQUECOUNTER3+7% = initially not existing when file doesn't exist, it is formatted by x (3) digits and starting from y (7) (e.g. 007)
%UNIQUECOUNTER3+7I% = (Inclusive) same as before but initially inclusive (so first file it will come directly with 007 rather than empty) -> this is your case
%UNIQUECOUNTER3+7+_% = existing or not initially according the usage of I which can be added or not but using the _ character as separation from the previous filename (e.g. _007). This is for cases when you don't want the extra char when the counter isn't used (example without I so counter not used initially) because if you put the _ in the filename then the initially filename will just end in _ being without the counter.

patrick.loxhay posted this 29 October 2019

Hi Luigi

Thanks for that information.  I tried it out and it works perfectly.  FYI, the manual (even the online one) has the syntax the wrong way around for the start number and the length.  This is the extract from the online manual :-

 

UNIQUECOUNTER(start+sizenumber)

Unique number which increases when name/file exists, in this case it start from specific number with size number format (e.g. UNIQUECOUNTER(3+4) -> 1st: 0003 2st: 0004 ....10st: 0010...)

 

regards

 

Patrick

luca.scarpati posted this 31 October 2019

Hi Patrick,

 

ok perfect and more than welcome.

Yes you are right for the manual, we already had it in our "to-do" list for the next version (the manual changes when there are official versionsinnocent).

Thanks for your feedback!

 

Best regards,

Luca

Anthony Dwyer posted this 02 February 2024

Hey everyone,

This is great and I have it working in Windows File System, but I can't get it to work with SharePoint?

Is there a way to get a "Unique Counter" file version using SharePoint.

I don't want to have my users trying to sort based on Date and Time in the file name if possible.

Thanks in advance.

Kind Regards

Anthony

Steph posted this 07 February 2024

Hello,

For this type of need, I generally use a script that reads a value from a text file, then increments it by 1 at the end of the script.

This works with every storage connectors

 

Steph

 

  • Liked by
  • luca.scarpati
  • Anthony Dwyer
Anthony Dwyer posted this 07 February 2024

Thanks for your reply Steph.

Can you provide a bit more informaiton on how I might apply a script in this situation?

J.Bruna posted this 08 February 2024

Thanks for the information, I didn't know that you could put modifiers on variables.

In my case I need the counter to be 0 for when there is no document with that name so that:

Original document name :   Invoice1234

If it does not exist in the destination rename it as Invoice1234_0

If it exist rename it as Invoice1234_1

  • Liked by
  • Anthony Dwyer
Steph posted this 08 February 2024

Thanks for your reply Steph.

Can you provide a bit more informaiton on how I might apply a script in this situation?

 

You need to add a script module before the storage module, in your case the sharepoint storage module.

The .vbs script to execute is something like this: (the variables and comments are in french but it works everywhere ;-) )

--------

Dim compteur_inc, oFich, objFSO, compteur, contenu

'Création de l'objet FSO nécessaire à l'ouverture du fichier txt

set fso = CreateObject("Scripting.FileSystemObject")

'Accès au fichier txt

set oFich = fspenTextFile("C:\CS_temp_compteur.txt",1,True) 

'lecture du contenu du fichier txt

contenu = oFich.ReadAll

'fermeture du fichier txt

oFich.Close

' Fermeture du fichier txt 

Set oFich = Nothing

Set fso = Nothing

'Transformation en entiers (string -> integer)

compteur = CInt(contenu)

compteur_inc = compteur + 1 'alors le compteur augmente de 1

'La variable à utiliser dans CS pour rappeler le compteur est %CMPT%

call metadata.setvalues("CMPT",compteur_inc)

'Création de l'objet FSO nécessaire à l'ouverture du fichier txt

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Accès au fichier txt

Set objFichierTexte = objFSpenTextFile("C:\CS_temp_compteur.txt",2,True)

'Ecriture dans le fichier du nouveau contenu

objFichierTexte.WriteLine(compteur_inc)

'Fermeture du fichier txt

objFichierTexte.Close

-------------------

 

In this script you need to change this line to define the location and the name of the text file (C:\CS_temp_compteur.txt):

set oFich = fspenTextFile("C:\CS_temp_compteur.txt",1,True) 

same thing here:

Set objFichierTexte = objFSpenTextFile("C:\CS_temp_compteur.txt",2,True)

 

In the txt file just type the start number you want - 1. (if you want to start at 1, type 0 in the text file)

 

Then use the variable %CMPT% in your storage module for whatever you want, this is the counter.

 

This script could certainly be optimized, but it works as it is

 

Steph

Steph posted this 08 February 2024

Beware of smileys !!

 fspenTextFile is in fact:

  • Liked by
  • Anthony Dwyer
  • luca.scarpati
luca.scarpati posted this 09 February 2024

Hi Guys,

 

your various messages are TOP!

Just a clarification when you write code as it is a text field that is inside a web page some emoticons can come out tongue-out...we suggest either using a "block quote" or better yet attaching your files.

 

Have a nice weekend!

Best regards,

Luca

Anthony Dwyer posted this 01 June 2024

Thanks Step ..... sorry for the long delay just circling back to this now.

I got this working great thanks for your help..... however.....

I was really hoping to increment based on unique file name similar to what is available with local file structure i.e %ORIGINALFILENAMEWITHOUTEXTENSION%-%UNIQUECOUNTER3+1I%.

For example using the %CMPT% variable form your original script at the of each unique file name.

Desired outcome same as local file structure but in sharepoint online.

  • File Name Invocie_23131-000.pdf
  • File Name Invocie_23131-001.pdf
  • File Name Invocie_23131-002.pdf
  • File Name asdasd-000.pdf
  • File Name asdasd-001.pdf
  • File Name asdasd-002.pdf
  • File Name Test-001.pdf

Is this possible?

Thanks in advance.

Regards

Anthony

luca.scarpati posted this 11 June 2024

Hi Anthony,

 

no and the answer is simple in Sharepoint you can't check the filename ... so the uniquecounter variable not works.

What you can do, however, is to have a local archive similar to your sharepoint so as to have another workflow which, given the initial filename, uploads the same document to Sharepoint...in two points:

  • you will have WFS: asdasd-002.pdf
  • you will have Sharepoint too: asdasd-002.pdf

Have a nice rest of your day!

 

Best regards,

Luca

Close