Rename Files in Folder Changing First Few Characters to Follow Pattern

Hello Community!

Happy New Year!

I am trying to rename files in a folder- call it ‘Section I Documents’ where the roman numeral I tells the bot the files should start with roman numeral I

The folder will always have a roman numeral, like I, II, III, IV, V in it’s title and I need the files in that folder to begin with the roman numeral of that Section folder.

Roman Numeral I Section files should always begin I followed by _ → I_
II Section files follow the same pattern II → II_

Example Section I Documents
First file name = 1. Shopping_cart
New file name should be = I_1 Shopping_cart
Another example might be = 1a. Shopping_cart
New file name should be = I_1_a Shopping_cart

Thanks in advance for your help in guiding me how to do this!

Hi @Jessica_Moseley

I had a doubt on this

Another example might be = 1a. Shopping_cart
New file name should be = I_1_a Shopping_cart

Do the file name should be I_1a Shopping_cart?

Good point. I’ll clarify. There might be a letter after the number and there needs to be an underscore between the two ex. I_1_a and the period is removed.

@Jessica_Moseley - It would be great if you share/show some samples of the real folder names with Roman Letters??

Section_I_Documents Folder and Files.png

@Jessica_Moseley - Can I assume if you max value of X(10) or XV(15) , i.e 10-15 folder? or do you have more than that?

or is it always follows the below pattern?

(SomeText)(Space)(RomanLetter)(Space)(SomeText)

Max value is VI.

This is an effort to rename files when they don’t match the naming convention. If the file starts with a roman numeral, it doesn’t need to be changed. If the file name starts with a number then I want to rename it using the rules in the above thread.

  1. Business Cards in the Section I Documents folder → I_1 Business Cards
    add the roman numeral I, underscore, the number, and period is removed
    1a. Shopping Cart → I_1_a Shopping Cart
    add the roman numeral I, underscore, the number, underscore, the letter, and period is removed

@Jessica_Moseley - Thanks for the info. I have started considering all the roman values so that you dont have to change the code in future…I will share the xaml soon.

Thank you!

@Jessica_Moseley - I am almost done, just need to insert underscore for 1b 1c cases…so thinking of the options…for the timebeing keep this output and explore the options…I will work on this tonight again…

XAML: Regex_RenameFiles_Roman.zip (38.6 KB)

Output:

image image

Thank you!

The letter could be any of the alphabet.

@Jessica_Moseley - Phew…this was tricky…I was able to do it finally with help of Regex…Here is the output…

Regx:

RegEx Explanation: (^\S{2,}) matches any non-whitespace character - So this group will hold either 1. or 1a. because after this there is a space in the file name so it stops. This is group1. $1 in the screenshot

.* → matches any character (except for line terminators) = This is our Group2. $2 in the screenshot.

Code: I am checking the count for the group1 if its length (1. = 2, 1a. = 3 also has alphabets, 100.=4, 10a. = 4 also has alphabets 10. = 3, etc )

Based on this length I am carefully inserting the _ as per requirement.

XAML: Regex_RenameFiles_Roman.zip (39.1 KB)

I have placed the sample files outside the folder, move it inside the folders and run the job. Hope this helps.

This looks brilliant! Thank you!

I am working on version 18.4.6 and cannot upgrade it. The Regex matches sequence is saying Missing or invalid activity.

My UiPath.System.Activities by UiPath is version 18.4.2. I tried updating to versions 19.2.0 and 10.10.1 and it only made the problem worse.
UiPath.UIAutomation.Activities v18.4.5

Do you know how I might resolve this issue?

@Jessica_Moseley…thanks…

Okay, I have to remove that Matches and swap it with the regular command and loop through it…

I will try that and send the output if it works…

@Jessica_Moseley - Here you go…Replaced the Regex Matches with simple Assign activity , as shown below…and used the same variable so that I don’t have make lot of modifications…

Updated XAML: Regex_RenameFiles_Roman_Updated.zip (39.6 KB)

Impressive. It works beautifully! Thank you!!

Happy New Year!

1 Like

@Jessica_Moseley - Glad to help you…Actually I am very happy with this project, I made me think multiple ooptions and also gave me an opportunity to explore…

Happy New Year…!!! :tada: :confetti_ball: :sparkles:

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