Assign: length ('-1') must be a non-negative value. (Parameter 'length') Actual value was -1

Hi! I am trying to change the folders of pdf files according to the years. at the point to fetch the last digits i stuck
Can anyone plz help me out?


Hi @Sawaira_Nawaz

Could you be more specific, Give the proper input and required output.

I have 1 folder having pdf files ending with random year like 2018,19,20,21
I want to move all these files to their specific folders i.e “Invoice 2018” for files ending with 2018.
here I have a simples project where I am using below code atatched.
Main.xaml (11.5 KB)

Okay @Sawaira_Nawaz

If every file name ends with 2018, 2019, 2020 like this, then you can use Split function and take the last word.

In your workflow, modify the below changes,
→ Inside for each delete the two assign activities, and use the below two assign activities.

- Assign -> FileName = Path.GetFileNameWithoutExtension(Invoice)

- Assign -> FileName = FileName.Split(" ").Last

→ After these two assign continue the same flow with Switch activity, in the switch Expression just pass the FileName variable.

Check the below workflow file I have made changes,
Main.xaml (11.6 KB)

Hope it helps!!

You should just use…

Path.GetFilenameWithoutExtension(PathAndFilename)

It did not created new folder and default value for not updating folder prompted

Could you be more specific… @Sawaira_Nawaz

Invoice1_2018.pdf (56.7 KB)
Invoice1_2019.pdf (56.7 KB)
Invoice1_2020.pdf (56.7 KB)
Invoice1_2021.pdf (56.7 KB)

These 4 files having saperate year name lie in one folder named “all invoices”. I want to move these files to saperate folders corresponding to the file names.
Can you plz do this for me?

Okay @Sawaira_Nawaz

Don’t make the code complicate, change the code as below,
→ Use the for each file in folder activity to iterate the each file in the folder. Give the folder path in the Infolder field. Output : CurrentFile
→ Inside for each insert the Assign activity and create a variable called FileName to store the file name and use split functions.

- Assign -> FileName = System.IO.Path.GetFileNameWithoutExtension(CurrentFile.ToString).Split("_").Last

→ After assign activity insert the Switch activity and give the Cint(FileName) in the Expression Field.
→ Then create different cases inside each case insert Move file activity.

Check the below workflow for better understanding,
Regex_Practice.xaml (17.8 KB)

It’s works for me, download the workflow and change the folder path based on your local folders.

Hope it helps!!

It might have some VB version issues.
I did not find any best site to learn VB for UiPath Studio Project.
Can you please refer any site?