How to bulk rename pdf files by its content

Hi

In my business scenario, i have pdf files in one folder. For eg Notice1, Notice2,Notice3 and Notice4 in folder path (D:\Uipath Practise\Pdfs)

  1. First Step: Used below logic in uipath:
    Sequence
    Assign FileList=System.IO.Directory.GetFiles(“D:\Uipath Practise\Pdfs”)

  2. Second Step: Used For Each Loop and Regex match operation.
    For each item in filelist Read PDF text and do match with pattern “(?<= bearing).*(?=has)”
    Based on above pattern logic, I am able to extract the notice number.

For eg:
Sample PDF Content:
Notice 1 File: This is to inform you that the LAN No bearing 1224352 has an outstanding amount of Rs.45000/- as on 29.11.20. Please pay immediately to avoid legal actions.

Notice 2 File: This is to inform you that the LAN No bearing 169891 has an outstanding amount of Rs.45000/- as on 29.11.20. Please pay immediately to avoid legal actions.

Extracted Content based on Regex Pattern.
Notice 1 file: 1224352
Notice 2 file: 169891

Thereafter I would like to rename the pdf files with the extracted notice number above. I am stuck here. I don’t know how to do the same. Please guide.

@Kumar_Shanmugam, use the “Move File” activity.
“From:” - current file path and name
“To:” - current file path + new file name, which will be the extracted notice number + “.pdf”

1 Like

Thanks Sumit for providing input.
I have gone through your suggestion but I don’t know how to do it for Notice 1, 2, 3 and so on in sequence. I need to ensure that.
Notice 1 pdf file should get renamed to its corresponding extracted notice number content 1224352.
Notice 2 pdf file should get renamed to its corresponding extracted notice number content 169891 and so on.

@Kumar_Shanmugam - Looks like you are already extracting the notice number using Regex right(may be using assign or is matches activity?)

So, say you are looping for each files as File, then your copy file activity with in the same loop would be

From : File

To: "RenamedFiles \ " + “RegexMatch.tostring” + “.pdf”
(Delete the space in before and after \ , i am not able to type like that)

is it possible to share the workflow?

Hi,

Please find attached the image of workflow.

First I am getting list of all files from directory
Assign Filelist (variable) = System.IO.Directory.GetFiles(“D:\Uipath Practise\Pdfs”)
Then for each item in filelist I am using Read Pdf text to read the content in the pdf files. Output PdfOutput (Variable)
Then i am using Regex match Pattern “(?<= bearing).*(?=has)” to extract the required content from PdfOutput Content. Output FilteredOutput (Variable)
Thereafter I am assigning Collection (Variable) = FilteredOutput(0).ToString

Now based on forum feedback I tried to use Move File activity. In this I am assigning from strpath (Variable)= “D:\Uipath Practise\Pdfs”+item.ToString+".pdf"
and To Destinationpath (Variable)= “D:\Uipath Practise\Pdfs”+Collection.ToString+".pdf"

When i run this workflow I am getting Runtime execution error.
Source: Body
Message: Object reference not set to an instance of an object.
Exception Type: System. NullReference. Exception.

@Kumar_Shanmugam…it might be because you are not getting anything from the Regex…

If not already tried , could you please print the output of the regex just to check whether it is working fine or not?

Also, if possible can you share your xaml and one pdf ? ( if you are just practicing and not company copies) …

Please take a look at below…I used regex inside a loop while solving one of the advvent challenges…thought of sharing with you…

Since I m the new user. I am not able to upload xaml file. However I have pasted the document link in below server.
xaml file.


PDF documents

@Kumar_Shanmugam … I will take a look a today and get back to you …

@Kumar_Shanmugam - Its fixed…You workflow was almost perfect and you were very close to crossing the finish line.

I made couple of fixes :

  1. For each Type should be string…

  2. Adjusted the Regex - Because it was capturing spaces…
    From: (?<= bearing).*(?=\has)
    To:
    image

  3. I removed the assign activity(not needed, just my thought) and directly used “FilteredOutput(0).tostring” in the code as shown below…

Here is the output:

image

Hope this solves your issue. please mark this as solution, if I solved your problem.

Great help from you at unexpected time. Really thankful. Thanks for resolving the same on priority.

1 Like

Is it possible for you to share your solved Xaml file.

@Kumar_Shanmugam - here you go …BulkRename_PDFs_KS.zip (446.5 KB)

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