Extract last file index from attachment name

Hi team ,

In my usecase I want to check what is the last file index in that specific folder.For example in the above folder it is File3 - so the index is 3.For the next transaction run tomorrow , whenever this folder is accessed , the bot should check the index of the last file and create new file with (index + 1).
So the new file should be WEF12345_07092022_File4.xlsx ,WEF12345_07092022_File5.xlsx so on.

Everytime how do I check the index of the last file in any folder to add a new file with a (index +1)?

The format of the file name remains the same throughout - WEF12345_06092022_File(index)

Regards,
G

Hi @gokul1904 ,
you can use get file name after getting that you can do split operation on “_” in an array and extract the last value of the array.

And after getting the last value i.e. file3 you can perform regex only to get the number.

Thanks & Regards,
Shubham Dutta

1 Like

Thanks for the response.
But how do I grab the latest index of the latest file from all the files present in the folder.
In the above case I want to extract only 3, not 1 and 2.

Regards,
G

Hello @gokul1904

Check out this Regex Pattern to get the number.
image

You can use an Assign like this:
System.Text.RegularExpressions.Regex.Match(yourString,“\d+(?=\.)”).ToString

Or you can convert straight to an Integer variable like this:
Left:
Int_Test

Right:
Cint(System.Text.RegularExpressions.Regex.Match(Str_Test,“\d+(?=\.)”).ToString)
image

Note: You may need to get all the files in the folder then iterate over all of them using the above Regex to ensure you have the highest value.

Cheers

Steve

Hi @gokul1904 ,
in your case i assume highest number will be latest one. So you can give a condition of checking greatest number when you are getting the numbers and the highest you will be assigning in your case 3.

Or else if you want the latest file as per date creation and then do the opearations that can also be done.

Hey @gokul1904,

As it looks like all the file names are following the same pattern, so what you can do is you can get all the file name in an array and order it in ascending or descending order as per your requirement and get the first or last index value from the list and use this regex

\d+(?=.)

Thanks,
Sanjit

Hi @gokul1904 ,
I am attaching a code which will give you the latest xlsx file according to file creation time in a directory/Folder.
Please provide your folder path in the code.
NewestFileIinDirectoryAnyDay.xaml (6.0 KB)

So, you got the latest file now you can perform the operations which I mentioned before

Thanks,
Shubham Dutta

@gokul1904
why are you want to index from file name?
you just get file count form folder and add 1 to create new file name
for Example
you can get file count Directory.GetFiles(string path).count

Thanks

Hi @gokul1904

Array(String) Variable Type

ArrStr = Directory.GetFiles("C:\Users\Dell\Documents\UiPath\Test\Appraisal\","WEF12345"+DateTime.Now.ToString("ddMMyyyy")+"_File*.xlsx")

String Variable Type

OutIncrIndex = "C:\Users\Dell\Documents\UiPath\Test\Appraisal\WEF12345"+DateTime.Now.ToString("ddMMyyyy")+"_File"+CInt(ArrStr.count+1).ToString+".xlsx"

Have a look on the XAML file

FileNameIndex.xaml (5.8 KB)

image

Regards
Gokul

1 Like

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