Using Win2PDF & Microsoft’s Power Automate Desktop to Rename PDFs

Our last post — Get Into Your PDF Flow using Microsoft’s Power Automate Desktop Software — gave an overview of the Power Automate Desktop software and how it can be used to automate repetitive tasks with PDF files.

Over the next series of posts we’ll show some examples of how this can be done in a real-world context using Win2PDF.

First, let’s start off by using an example where we want to automatically rename a bunch of PDF invoices that are stored in a folder. And then suppose we not only want to rename the PDF files, but we want the new file names to be based on some content within the original files, such as the number following the word “INVOICE”.

In the following example (see Microsoft Power Automate for Desktop flow for renaming PDF based on File Contents for details), we’ll search through each PDF in the folder and rename the file based on an invoice number that follows the word “INVOICE”. Here’s what the desktop flow looks like:

Notice how the Win2PDF command line getcontentsearch feature is used. It allows us to search for the word “INVOICE” in the PDF file, and then return the following content (in this case, the INVOICE number) to the standard output. After trimming any extraneous spaces from this invoice number, the PDF file is renamed to be that invoice number.

You can use the graphical user interface to make your own flow, or you can copy the following into a new Power Automate Desktop flow if you want to use this as a starting point for your project:

# This flow searches the contents of PDF files in the selected folder for SearchText, and renames the PDF files based on the text immediately following SearchText.  Change the SearchText variable to the desired search field.  Note : the SearchText field is case sensitive.
SET SearchText TO $'''INVOICE :'''
Display.SelectFolder Description: $'''Select Folder to Rename''' IsTopMost: False SelectedFolder=> SelectedFolder ButtonPressed=> ButtonPressed3
Folder.GetFiles Folder: SelectedFolder FileFilter: $'''*.pdf''' IncludeSubfolders: False FailOnAccessDenied: True SortBy1: Folder.SortBy.NoSort SortDescending1: False SortBy2: Folder.SortBy.NoSort SortDescending2: False SortBy3: Folder.SortBy.NoSort SortDescending3: False Files=> Files
LOOP FOREACH CurrentItem IN Files
    # Search for %SearchText% field and return following text in CommandOutput using the following Win2PDF command line:
    # C:\Windows\System32\spool\drivers\x64\3\win2pdfd.exe getcontentsearch "%CurrentItem%" "" "%SearchText%"
    System.RunDOSCommand DOSCommandOrApplication: $'''C:\\Windows\\System32\\spool\\drivers\\x64\\3\\win2pdfd.exe getcontentsearch \"%CurrentItem%\" \"\" \"%SearchText%\"''' StandardOutput=> CommandOutput StandardError=> CommandErrorOutput ExitCode=> CommandExitCode
    IF CommandExitCode = 0 THEN
        Text.Trim Text: CommandOutput TrimOption: Text.TrimOption.Both TrimmedText=> TrimmedText
        File.Rename Files: CurrentItem NewName: $'''%SelectedFolder%\\%TrimmedText%''' KeepExtension: True IfFileExists: File.IfExists.Overwrite RenamedFiles=> RenamedFiles
    ELSE
        Display.ShowMessageWithTimeout Title: $'''Win2PDF Renamer''' Message: $'''Could not find \"INVOICE :\" field for file %CurrentItem%''' Icon: Display.Icon.None Buttons: Display.Buttons.OK DefaultButton: Display.DefaultButton.Button1 IsTopMost: False Timeout: 10 ButtonPressed=> ButtonPressed4
    END
END

This example shows one way to use Win2PDF’s automation features to create a solution unique to your workflow. It also uses features that were added in the latest Win2PDF 10.0.100 update, so make sure you are using the latest Win2PDF 10 Update when trying any of the Power Automate Desktop features.

Can you think of any similar cases that you might use? If so, drop us a line. We’ll have more examples coming soon.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s